#include #include #include #include #include "WebServer.h" #include #include #include "Pushover.h" #include #include const char* M5version = "M5alarm Version 1.1"; const IPAddress apIP(192, 168, 4, 1); const char* apSSID = "M5STACK_SETUP"; boolean settingMode; String ssidList; String wifi_ssid; String wifi_password; IPAddress wifi_ip(0,0,0,0); IPAddress wifi_nm(0,0,0,0); IPAddress wifi_gw(0,0,0,0); bool soundOn = true; uint8_t music[1000]; int s_stat[100]; int s_push[100]; int s_xxx[100]; int s_yyy[100]; String s_name[100]; int a_page = 0; int a_br = 100; uint32_t SaveMillis = 0; uint32_t WaitMillis = 300000; uint32_t TftMillis = 0; uint32_t DarkMillis = 30000; bool i_push = false; bool i_beep = false; bool i_millis = false; bool i_do = false; String po_app_token = ""; String po_usr_token = ""; int i_rssi = 0; int m_rssi = 0; int i_menu = 0; String URLuser = ""; String URLpass = ""; String URLauth = ""; bool URLconn = "true"; IPAddress XThost(0,0,0,0); int a_area1, a_area2, a_area = 0; bool area = true; bool a_pop = false; WebServer webServer(80); Preferences preferences; void setup() { for(int i = 0; i < 100; i++) { s_stat[i]=0; s_push[i]=0; s_xxx[i]=0; s_yyy[i]=0; s_name[i]=""; } m5.begin(); M5.Lcd.fillScreen(WHITE); M5.Lcd.drawJpgFile(SD, "/m5_logo.jpg"); M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(BLACK); M5.Lcd.setCursor(50, 210); M5.Lcd.printf(M5version); delay(5000); M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextSize(1); M5.Lcd.setTextColor(WHITE, BLACK); M5.Lcd.setCursor(0, 0); preferences.begin("wifi-config"); delay(10); if (restoreConfig()) { if (checkConnection()) { settingMode = false; startWebServer(); mdns_init(); mdns_hostname_set("m5s"); ArduinoOTA.setHostname("m5s"); ArduinoOTA.setPassword("admin"); ArduinoOTA.begin(); po_app_token = preferences.getString("APP_KEY"); po_usr_token = preferences.getString("USR_KEY"); if (preferences.getString("DM_VAR").length() != 0) DarkMillis = preferences.getString("DM_VAR").toInt(); if (preferences.getString("WM_VAR").length() != 0) WaitMillis = preferences.getString("WM_VAR").toInt(); if (preferences.getString("BR_VAR").length() != 0) a_br = preferences.getString("BR_VAR").toInt(); if (preferences.getString("PO_VAR").length() != 0) a_pop = preferences.getString("PO_VAR").toInt(); if (preferences.getString("XT_IP1").length() != 0) XThost[0] = preferences.getString("XT_IP1").toInt(); if (preferences.getString("XT_IP2").length() != 0) XThost[1] = preferences.getString("XT_IP2").toInt(); if (preferences.getString("XT_IP3").length() != 0) XThost[2] = preferences.getString("XT_IP3").toInt(); if (preferences.getString("XT_IP4").length() != 0) XThost[3] = preferences.getString("XT_IP4").toInt(); URLuser = preferences.getString("XT_USER"); URLpass = preferences.getString("XT_PASS"); if (preferences.getString("XT_AREA") == "1") area = true; else area = false; readFile(SD, "/sensoren.txt"); readFileXY(SD, "/nn00xxxyyy.txt"); if (a_pop) { M5.Lcd.println(); M5.Lcd.println("Connecting to LupusXT"); M5.Lcd.println("Please wait ..."); readJSONsensor(); readJSONalarm(); if (URLconn) M5.Lcd.println("Connection OK"); else M5.Lcd.println("Connection failed!"); } webServer.on("/Lupus", handleLupus); webServer.on("/APIkey", handlePushOver); webServer.on("/XTauth", handleXTauth); webServer.on("/xt", handleXT); TFTout(); return; } } settingMode = true; setupMode(); mdns_init(); mdns_hostname_set("m5s"); } void loop() { if (!settingMode) { if (M5.BtnA.wasReleased()) {if (a_page == 2 && i_menu != 0) buttonAm(); else buttonA();} if (M5.BtnA.wasPressed() && i_menu == 0 && a_pop) { M5.Lcd.setBrightness(a_br); M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(WHITE, BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.println("Connecting to LupusXT"); M5.Lcd.println("Please wait ..."); readJSONsensor(); if (!URLconn) { M5.Lcd.println("Connection failed!"); delay (1000); } } if (M5.BtnB.wasReleased()) {if (a_page == 2) buttonBm(); else buttonB();} if (M5.BtnC.wasReleased()) {if (a_page == 2 && i_menu != 0) buttonCm(); else buttonC();} if (M5.BtnC.wasPressed() && i_menu == 0 && a_pop) { M5.Lcd.setBrightness(a_br); M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(WHITE, BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.println("Connecting to LupusXT"); M5.Lcd.println("Please wait ..."); readJSONalarm(); if (!URLconn) { M5.Lcd.println("Connection failed!"); delay (1000); } } if (a_page == 2) p_rssi(); if (millis() > (SaveMillis + WaitMillis) && i_millis == true) { for (int i = 1; i < 100; i++) { if (s_stat[i] > 1 && s_push[i] > 0 && s_push[i] < 4) i_do = true; } if (i_beep && i_do) beep(3000); if (i_push && i_do) { Pushover po = Pushover(po_app_token,po_usr_token); po.setMessage("Sensor(en) zu lange offen!"); po.send(); } i_beep = false; i_push = false; i_millis = false; i_do = false; } if (millis() > (TftMillis + DarkMillis) && DarkMillis > 0) { M5.Lcd.setBrightness(0); TftMillis = millis(); } } webServer.handleClient(); ArduinoOTA.handle(); delay(1); M5.update(); } boolean restoreConfig() { wifi_ssid = preferences.getString("WIFI_SSID"); wifi_password = preferences.getString("WIFI_PASSWD"); if (preferences.getString("WIFI_IP1").length() != 0) wifi_ip[0] = preferences.getString("WIFI_IP1").toInt(); if (preferences.getString("WIFI_IP2").length() != 0) wifi_ip[1] = preferences.getString("WIFI_IP2").toInt(); if (preferences.getString("WIFI_IP3").length() != 0) wifi_ip[2] = preferences.getString("WIFI_IP3").toInt(); if (preferences.getString("WIFI_IP4").length() != 0) wifi_ip[3] = preferences.getString("WIFI_IP4").toInt(); if (preferences.getString("WIFI_NM1").length() != 0) wifi_nm[0] = preferences.getString("WIFI_NM1").toInt(); if (preferences.getString("WIFI_NM2").length() != 0) wifi_nm[1] = preferences.getString("WIFI_NM2").toInt(); if (preferences.getString("WIFI_NM3").length() != 0) wifi_nm[2] = preferences.getString("WIFI_NM3").toInt(); if (preferences.getString("WIFI_NM4").length() != 0) wifi_nm[3] = preferences.getString("WIFI_NM4").toInt(); if (preferences.getString("WIFI_GW1").length() != 0) wifi_gw[0] = preferences.getString("WIFI_GW1").toInt(); if (preferences.getString("WIFI_GW2").length() != 0) wifi_gw[1] = preferences.getString("WIFI_GW2").toInt(); if (preferences.getString("WIFI_GW3").length() != 0) wifi_gw[2] = preferences.getString("WIFI_GW3").toInt(); if (preferences.getString("WIFI_GW4").length() != 0) wifi_gw[3] = preferences.getString("WIFI_GW4").toInt(); Serial.print("WIFI-SSID: "); M5.Lcd.print("WIFI-SSID: "); Serial.println(wifi_ssid); M5.Lcd.println(wifi_ssid); Serial.print("WIFI-IP: "); M5.Lcd.print("WIFI-IP: "); Serial.println(wifi_ip); M5.Lcd.println(wifi_ip); Serial.print("WIFI-NM: "); M5.Lcd.print("WIFI-NM: "); Serial.println(wifi_nm); M5.Lcd.println(wifi_nm); Serial.print("WIFI-GW: "); M5.Lcd.print("WIFI-GW: "); Serial.println(wifi_gw); M5.Lcd.println(wifi_gw); WiFi.begin(wifi_ssid.c_str(), wifi_password.c_str()); WiFi.config(wifi_ip,wifi_gw,wifi_nm,wifi_gw); if(wifi_ssid.length() > 0) { return true; } else { return false; } } boolean checkConnection() { int count = 0; Serial.print("Waiting for Wi-Fi connection"); M5.Lcd.print("Waiting for Wi-Fi connection"); while ( count < 30 ) { if (WiFi.status() == WL_CONNECTED) { Serial.println(); M5.Lcd.println(); Serial.println("Connected!"); M5.Lcd.println("Connected!"); return (true); } delay(500); Serial.print("."); M5.Lcd.print("."); count++; } Serial.println("Timed out."); M5.Lcd.println("Timed out."); return false; } void startWebServer() { if (settingMode) { Serial.print("Starting Web Server at "); M5.Lcd.print("Starting Web Server at "); Serial.println(WiFi.softAPIP()); M5.Lcd.println(WiFi.softAPIP()); webServer.on("/settings", []() { String s = "

Wi-Fi Settings

Please enter your password by selecting the SSID.

"; s += "

Password: "; s += "
IP: "; s += "."; s += "."; s += "."; s += "
NM: "; s += "."; s += "."; s += "."; s += "
GW: "; s += "."; s += "."; s += "."; s += "
"; webServer.send(200, "text/html", makePage("Wi-Fi Settings", s)); }); webServer.on("/setap", []() { String ssid = urlDecode(webServer.arg("ssid")); Serial.print("SSID: "); M5.Lcd.print("SSID: "); Serial.println(ssid); M5.Lcd.println(ssid); String pass = urlDecode(webServer.arg("pass")); Serial.print("Password: "); M5.Lcd.print("Password: "); Serial.println(pass); M5.Lcd.println(pass); Serial.println("Writing SSID to EEPROM..."); M5.Lcd.println("Writing SSID to EEPROM..."); Serial.println("Writing to nvr..."); M5.Lcd.println("Writing to nvr..."); preferences.putString("WIFI_SSID", ssid); preferences.putString("WIFI_PASSWD", pass); preferences.putString("WIFI_IP1", webServer.arg("ip1")); preferences.putString("WIFI_IP2", webServer.arg("ip2")); preferences.putString("WIFI_IP3", webServer.arg("ip3")); preferences.putString("WIFI_IP4", webServer.arg("ip4")); preferences.putString("WIFI_NM1", webServer.arg("nm1")); preferences.putString("WIFI_NM2", webServer.arg("nm2")); preferences.putString("WIFI_NM3", webServer.arg("nm3")); preferences.putString("WIFI_NM4", webServer.arg("nm4")); preferences.putString("WIFI_GW1", webServer.arg("gw1")); preferences.putString("WIFI_GW2", webServer.arg("gw2")); preferences.putString("WIFI_GW3", webServer.arg("gw3")); preferences.putString("WIFI_GW4", webServer.arg("gw4")); Serial.println("Write nvr done!"); M5.Lcd.println("Write nvr done!"); String s = "

Setup complete.

device will be connected to \""; s += ssid; s += "\" after the restart."; webServer.send(200, "text/html", makePage("Wi-Fi Settings", s)); delay(3000); ESP.restart(); }); webServer.onNotFound([]() { String s = "

AP mode

Wi-Fi Settings

"; webServer.send(200, "text/html", makePage("AP mode", s)); }); } else { Serial.print("Starting Web Server at "); M5.Lcd.print("Starting Web Server at "); Serial.println(WiFi.localIP()); M5.Lcd.println(WiFi.localIP()); webServer.on("/", []() { String s = "

STA mode

Reset Wi-Fi Settings

"; webServer.send(200, "text/html", makePage("STA mode", s)); }); webServer.on("/reset", []() { preferences.remove("WIFI_SSID"); preferences.remove("WIFI_PASSWD"); preferences.remove("WIFI_IP1"); preferences.remove("WIFI_IP2"); preferences.remove("WIFI_IP3"); preferences.remove("WIFI_IP4"); preferences.remove("WIFI_NM1"); preferences.remove("WIFI_NM2"); preferences.remove("WIFI_NM3"); preferences.remove("WIFI_NM4"); preferences.remove("WIFI_GW1"); preferences.remove("WIFI_GW2"); preferences.remove("WIFI_GW3"); preferences.remove("WIFI_GW4"); String s = "

Wi-Fi settings was reset.

Please reset device.

"; webServer.send(200, "text/html", makePage("Reset Wi-Fi Settings", s)); delay(3000); ESP.restart(); }); } webServer.begin(); } void setupMode() { WiFi.mode(WIFI_MODE_STA); WiFi.disconnect(); delay(100); int n = WiFi.scanNetworks(); delay(100); Serial.println(""); M5.Lcd.println(""); for (int i = 0; i < n; ++i) { ssidList += ""; } delay(100); WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); WiFi.softAP(apSSID); WiFi.mode(WIFI_MODE_AP); startWebServer(); Serial.print("Starting Access Point at \""); M5.Lcd.print("Starting Access Point at \""); Serial.print(apSSID); M5.Lcd.print(apSSID); Serial.println("\""); M5.Lcd.println("\""); } String makePage(String title, String contents) { String s = ""; s += ""; s += ""; s += title; s += ""; s += contents; s += ""; return s; } String urlDecode(String input) { String s = input; s.replace("%20", " "); s.replace("+", " "); s.replace("%21", "!"); s.replace("%22", "\""); s.replace("%23", "#"); s.replace("%24", "$"); s.replace("%25", "%"); s.replace("%26", "&"); s.replace("%27", "\'"); s.replace("%28", "("); s.replace("%29", ")"); s.replace("%30", "*"); s.replace("%31", "+"); s.replace("%2C", ","); s.replace("%2E", "."); s.replace("%2F", "/"); s.replace("%2C", ","); s.replace("%3A", ":"); s.replace("%3A", ";"); s.replace("%3C", "<"); s.replace("%3D", "="); s.replace("%3E", ">"); s.replace("%3F", "?"); s.replace("%40", "@"); s.replace("%5B", "["); s.replace("%5C", "\\"); s.replace("%5D", "]"); s.replace("%5E", "^"); s.replace("%5F", "-"); s.replace("%60", "`"); return s; } void tone_volume(uint16_t frequency, uint32_t duration) { float interval=0.001257*float(frequency); float phase=0; for (int i=0;i<1000;i++) { music[i]=127+126*sin(phase); phase+=interval; } music[999]=0; int remains=duration; for (int i=0;i 0) DarkMillis -= 30000; break; case (2): if (WaitMillis / 60000 > 5) WaitMillis -= 300000; break; case (3): if (a_br / 25 > 1) a_br -= 25; break; case (4): if (a_pop) a_pop = false; else a_pop = true; break; } TFTout(); } void buttonBm() { if (++i_menu > 4) i_menu = 0; TFTout(); } void buttonCm() { switch (i_menu) { case (1): if (DarkMillis / 1000 < 300) DarkMillis += 30000; break; case (2): if (WaitMillis / 60000 < 60) WaitMillis += 300000; break; case (3): if (a_br / 25 < 10) a_br += 25; break; case (4): if (a_pop) a_pop = false; else a_pop = true; break; } TFTout(); } void handlePushOver() { String s = "

PushOver settings

Please enter key.

"; s += "
"; s += "
app-key: " + po_app_token; s += "

"; s += "
usr-key: " + po_usr_token; s += "

"; s += "
"; webServer.send(200, "text/html", makePage("Key Settings", s)); webServer.on("/setKEY", []() { po_app_token = webServer.arg("appkey"); preferences.putString("APP_KEY", po_app_token); po_usr_token = webServer.arg("usrkey"); preferences.putString("USR_KEY", po_usr_token); String s = "

Key settings complete.

"; s += "
app-key: " + po_app_token; s += "
usr-key: " + po_usr_token; webServer.send(200, "text/html", makePage("setkey", s)); }); } void handleXTauth() { String s = "

Lupus XT Authentication

"; s += "
"; s += "
XT-IP: "; s += "."; s += "."; s += "."; s += "
XT-User: "; s += "
XT-Password: "; s += "
XT-Area: "; s += "
"; webServer.send(200, "text/html", makePage("XTauth Settings", s)); webServer.on("/setAUTH", []() { preferences.putString("XT_IP1", webServer.arg("xt1")); preferences.putString("XT_IP2", webServer.arg("xt2")); preferences.putString("XT_IP3", webServer.arg("xt3")); preferences.putString("XT_IP4", webServer.arg("xt4")); XThost[0] = webServer.arg("xt1").toInt(); XThost[1] = webServer.arg("xt2").toInt(); XThost[2] = webServer.arg("xt3").toInt(); XThost[3] = webServer.arg("xt4").toInt(); URLuser = urlDecode(webServer.arg("user")); URLpass = urlDecode(webServer.arg("pass")); preferences.putString("XT_USER", URLuser); preferences.putString("XT_PASS", URLpass); if (webServer.arg("area") == "1") area = true; if (webServer.arg("area") == "2") area = false; if (area) preferences.putString("XT_AREA", "1"); else preferences.putString("XT_AREA", "2"); String s = "

Key settings complete.

"; s += "
XT User: " + URLuser; s += "
Area: " + String(area); webServer.send(200, "text/html", makePage("setAUTH", s)); }); } void handleLupus() { String s = "

Lupus XT Status

"; if (webServer.argName(0) == "XT") { String XT = webServer.arg(0); if (XT.length() == 10) { s += "

XT-Value: " + XT + "

"; int i = XT.substring(0,2).toInt(); s_stat[i] = XT.substring(2,3).toInt(); s_push[i] = XT.substring(3,4).toInt(); s_xxx[i] = XT.substring(4,7).toInt(); s_yyy[i] = XT.substring(7,10).toInt(); if (s_xxx[i] < 10) s_xxx[i] = 10; if (s_xxx[i] > 309) s_xxx[i] = 309; if (s_yyy[i] < 10) s_yyy[i] = 10; if (s_yyy[i] > 199) s_yyy[i] = 199; s += "

Sensor: " + String(i) + "

"; s += "

Status: " + String(s_stat[i]) + "

"; s += "

Push: " + String(s_push[i]) + "

"; s += "

xxx: " + String(s_xxx[i]) + "

"; s += "

yyy: " + String(s_yyy[i]) + "

"; if (i == 0) buttonC(); else buttonA(); if (i == 0 && s_push[i] > 0) beep(250*s_push[i]); if (i > 0 && s_stat[i] > 1 && s_push[i] == 1) { SaveMillis = millis(); i_beep = true; i_millis = true; } if (i > 0 && s_stat[i] > 1 && s_push[i] == 2) { SaveMillis = millis(); i_push = true; i_millis = true; } if (i > 0 && s_stat[i] > 1 && s_push[i] == 3) { SaveMillis = millis(); i_beep = true; i_push = true; i_millis = true; } } else { s += "

Value error: " + XT + "

"; } } else { s += "

Argument error: " + webServer.argName(0) + "

"; } webServer.send(200, "text/html", makePage("Status-Display", s)); } void TFTout() { int color = WHITE; M5.Lcd.setBrightness(a_br); TftMillis = millis(); switch (a_page) { case (0): menu(); break; case (1): M5.Lcd.fillScreen(BLACK); menu(); for (int i = 0; i <= 200; i +=39) M5.Lcd.drawLine(4, i, 316, i, WHITE); for (int i = 4; i <= 319; i +=39) M5.Lcd.drawLine(i, 0, i, 195, WHITE); M5.Lcd.drawJpgFile(SD, "/haus.jpg"); menu(); for(int i = 1; i < 100; i++) { if(s_stat[i] == 1) M5.Lcd.fillCircle(s_xxx[i], s_yyy[i], 10, GREEN); if(s_stat[i] > 1) M5.Lcd.fillCircle(s_xxx[i], s_yyy[i], 10, RED); } break; case (2): M5.Lcd.fillScreen(BLACK); if (i_menu == 0) { menu(); if (DarkMillis != (preferences.getString("DM_VAR").toInt())) preferences.putString("DM_VAR", String(DarkMillis)); if (WaitMillis != (preferences.getString("WM_VAR").toInt())) preferences.putString("WM_VAR", String(WaitMillis)); if (a_br != (preferences.getString("BR_VAR").toInt())) preferences.putString("BR_VAR", String(a_br)); if (a_pop != (preferences.getString("PO_VAR").toInt())) preferences.putString("PO_VAR", String(a_pop)); } else menu_einst(); m_rssi = 0; p_rssi(); if (i_menu == 1) color = YELLOW; else color = WHITE; M5.Lcd.setTextColor(BLACK, color); M5.Lcd.fillRect(0, 30, 320, 35, color); M5.Lcd.setCursor(10, 40); M5.Lcd.printf("Screensaver (sec): "); M5.Lcd.print(String(DarkMillis / 1000)); if (i_menu == 2) color = YELLOW; else color = WHITE; M5.Lcd.setTextColor(BLACK, color); M5.Lcd.fillRect(0, 70, 320, 35, color); M5.Lcd.setCursor(10, 80); M5.Lcd.printf("Push Message (min): "); M5.Lcd.print(String(WaitMillis / 60000)); if (i_menu == 3) color = YELLOW; else color = WHITE; M5.Lcd.setTextColor(BLACK, color); M5.Lcd.fillRect(0, 110, 320, 35, color); M5.Lcd.setCursor(10, 120); M5.Lcd.printf("Display Brightness: "); M5.Lcd.print(String(a_br / 25)); if (i_menu == 4) color = YELLOW; else color = WHITE; M5.Lcd.setTextColor(BLACK, color); M5.Lcd.fillRect(0, 150, 320, 35, color); M5.Lcd.setCursor(10, 160); M5.Lcd.printf("POP from Lupus XT2: "); M5.Lcd.print(String(a_pop)); break; case (3): M5.Lcd.fillScreen(BLACK); menu(); M5.Lcd.setTextSize(3); if (s_stat[0] == 7 || s_stat[0] == 8 || s_stat[0] == 9) s_stat[0] -= 6; switch (s_stat[0]) { case (0): M5.Lcd.fillCircle(160, 104, 100, GREEN); M5.Lcd.setTextColor(BLACK, GREEN); M5.Lcd.setCursor(105, 95); M5.Lcd.printf("DISARM"); break; case (1): M5.Lcd.fillCircle(160, 104, 100, YELLOW); M5.Lcd.setTextColor(BLACK, YELLOW); M5.Lcd.setCursor(105, 95); M5.Lcd.printf("HOME 1"); break; case (2): M5.Lcd.fillCircle(160, 104, 100, YELLOW); M5.Lcd.setTextColor(BLACK, YELLOW); M5.Lcd.setCursor(105, 95); M5.Lcd.printf("HOME 2"); break; case (3): M5.Lcd.fillCircle(160, 104, 100, YELLOW); M5.Lcd.setTextColor(BLACK, YELLOW); M5.Lcd.setCursor(105, 95); M5.Lcd.printf("HOME 3"); break; case (4): M5.Lcd.fillCircle(160, 104, 100, RED); M5.Lcd.setTextColor(BLACK, RED); M5.Lcd.setCursor(130, 95); M5.Lcd.printf("ARM"); break; case (5): M5.Lcd.fillCircle(160, 104, 100, MAROON ); M5.Lcd.setTextColor(WHITE, MAROON); M5.Lcd.setCursor(130, 95); M5.Lcd.printf("ARM"); break; case (6): M5.Lcd.fillCircle(160, 104, 100, DARKGREEN); M5.Lcd.setTextColor(WHITE, DARKGREEN); M5.Lcd.setCursor(105, 95); M5.Lcd.printf("DISARM"); break; } break; } } void menu() { M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(WHITE, BLUE); M5.Lcd.fillRect(15, 210, 90, 29, BLUE); M5.Lcd.setCursor(25, 219); M5.Lcd.printf("Sensor"); M5.Lcd.fillRect(115, 210, 90, 29, BLUE); M5.Lcd.setCursor(125, 219); M5.Lcd.printf("Einst."); M5.Lcd.fillRect(215, 210, 90, 29, BLUE); M5.Lcd.setCursor(225, 219); M5.Lcd.printf("Status"); } void menu_einst() { M5.Lcd.setTextSize(2); M5.Lcd.setTextColor(WHITE, BLUE); M5.Lcd.fillRect(15, 210, 90, 29, BLUE); M5.Lcd.setCursor(25, 219); M5.Lcd.printf(" <-"); M5.Lcd.fillRect(115, 210, 90, 29, BLUE); M5.Lcd.setCursor(125, 219); M5.Lcd.printf(" Wahl"); M5.Lcd.fillRect(215, 210, 90, 29, BLUE); M5.Lcd.setCursor(225, 219); M5.Lcd.printf(" ->"); } void p_rssi() { if (WiFi.status() == WL_CONNECTED) { int rssi = WiFi.RSSI(); if (rssi > -55) i_rssi=5; else if (rssi <= -55 & rssi > -65) i_rssi=4; else if (rssi <= -65 & rssi > -75) i_rssi=3; else if (rssi <= -75 & rssi > -85) i_rssi=2; else if (rssi <= -85 & rssi > -95) i_rssi=1; else i_rssi=0; } else i_rssi=4; if (i_rssi != m_rssi) { M5.Lcd.fillRect(300, 0, 319, 20, BLACK); if (i_rssi > 0) M5.Lcd.fillRect(300, 8, 2, 4, WHITE); if (i_rssi > 1) M5.Lcd.fillRect(304, 6, 2, 8, WHITE); if (i_rssi > 2) M5.Lcd.fillRect(308, 4, 2, 12, WHITE); if (i_rssi > 3) M5.Lcd.fillRect(312, 2, 2, 16, WHITE); if (i_rssi > 4) M5.Lcd.fillRect(316, 0, 2, 20, WHITE); m_rssi = i_rssi; } } void handleXT() { if (a_pop) readJSONsensor(); if (a_pop) readJSONalarm(); const String webPageXTtop = R"=====( LUPUS XT STATUS

  Lupus XT Status

)====="; const String umbruch = R"=====( )====="; String webPageXT = webPageXTtop; switch (s_stat[0]) { case (0): webPageXT = webPageXT + "

Alarmanlage: Disarm

"; break; case (1): webPageXT = webPageXT + "

Alarmanlage: Home 1

"; break; case (2): webPageXT = webPageXT + "

Alarmanlage: Home 2

"; break; case (3): webPageXT = webPageXT + "

Alarmanlage: Home 3

"; break; case (4): webPageXT = webPageXT + "

Alarmanlage: Arm

"; break; case (5): webPageXT = webPageXT + "

Alarmanlage: Delay Eingang

"; break; case (6): webPageXT = webPageXT + "

Alarmanlage: Delay Ausgang

"; break; } webPageXT = webPageXT + umbruch; for(int i = 1; i < 100; i++){ if (s_stat[i] > 1) webPageXT = webPageXT + "

"; if (s_stat[i] == 1) webPageXT = webPageXT + "

"; if (s_stat[i] > 0) { if (s_name[i].length() < 3) webPageXT = webPageXT + "Sensor " + String(i); else webPageXT = webPageXT + s_name[i]; webPageXT = webPageXT + "

"; webPageXT = webPageXT + umbruch; } } webPageXT = webPageXT + "
"; webServer.send(200, "text/html", webPageXT); webPageXT = ""; } void readFile(fs::FS &fs, const char * path) { int ch = 0; int i = 1; char sen; File file = fs.open(path); if(!file) return; while(file.available()) { ch = file.read(); if (ch == 0x0D && file.available()) { ch = file.read(); if (ch == 0x0A) { s_name[i].trim(); i++; } } else { sen = ch; s_name[i] += sen; } } file.close(); } void readFileXY(fs::FS &fs, const char * path) { int ch = 0; int i = 0; char sen; String xy = ""; File file = fs.open(path); if(!file) return; while(file.available()) { ch = file.read(); if (ch == 0x0D && file.available()) { ch = file.read(); if (ch == 0x0A) { xy.trim(); if (xy.length() == 10) { i = xy.substring(0,2).toInt(); if (i != 0) { s_xxx[i] = xy.substring(4,7).toInt(); s_yyy[i] = xy.substring(7,10).toInt(); if (s_xxx[i] < 10) s_xxx[i] = 10; if (s_xxx[i] > 309) s_xxx[i] = 309; if (s_yyy[i] < 10) s_yyy[i] = 10; if (s_yyy[i] > 199) s_yyy[i] = 199; } } xy = ""; } } else { sen = ch; xy += sen; } } file.close(); } void readJSONsensor() { URLauth = base64::encode(URLuser + ":" + URLpass); String url1 = "/action/deviceListGet"; Serial.print(XThost); Serial.println(url1); WiFiClient client; if (!client.connect(XThost, 80)) { Serial.println("connection failed"); URLconn = false; return; } URLconn = true; client.print("GET " + url1 + " HTTP/1.1\r\n" + "Host: " + XThost + "\r\n" + "Authorization: Basic " + URLauth + "\r\n" + "Content-Type: application/json\r\n" + "Connection: close\r\n\r\n"); delay(500); int z = 0; char c; String input = ""; bool anfang = false; while(client.available()){ c = client.read(); if (String(c) == "[" || anfang) { if (c != 0x0D && c != 0x0A && c != 0x09) { input += c; anfang = true; } } } input = input.substring(0, input.length()-1); DynamicJsonBuffer jsonBuffer(99999); JsonArray& root = jsonBuffer.parseArray(input); for (int i = 0; i<=root.size()-1; i++){ int field_type = root[i]["type"]; if (field_type == 4) { int field_zone = root[i]["zone"]; String field_name = root[i]["name"]; int field_status_ex = root[i]["status_ex"]; s_name[field_zone] = field_name; s_stat[field_zone] = field_status_ex + 1; z++; } } jsonBuffer.clear(); Serial.println(String(z) + " Sensoren eingelesen"); } void readJSONalarm() { URLauth = base64::encode(URLuser + ":" + URLpass); String url2 = "/action/panelCondGet"; Serial.print(XThost); Serial.println(url2); WiFiClient client; if (!client.connect(XThost, 80)) { Serial.println("connection failed"); URLconn = false; return; } URLconn = true; client.print("GET " + url2 + " HTTP/1.1\r\n" + "Host: " + XThost + "\r\n" + "Authorization: Basic " + URLauth + "\r\n" + "Content-Type: application/json\r\n" + "Connection: close\r\n\r\n"); delay(500); char c; int i = 0; String conv = ""; while(client.available()){ c = client.read(); if (String(c) == "{") i++; if (i == 3) { for (int j=1; j<12; j++) c = client.read(); conv = c; a_area1 = conv.toInt(); i++; } if (i == 5) { for (int j=1; j<12; j++) c = client.read(); conv = c; a_area2 = conv.toInt(); i++; } } Serial.println("Area1: " + String(a_area1)); Serial.println("Area2: " + String(a_area2)); if (area) a_area = a_area1 ; else a_area = a_area2; switch (a_area) { case (0): s_stat[0]=0; break; case (1): s_stat[0]=4; break; case (2): s_stat[0]=1; break; case (3): s_stat[0]=2; break; case (4): s_stat[0]=3; break; } }