From 479ac5d3439112e548a5e164989be012f3a224d1 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:29:03 -0400 Subject: [PATCH] [WebUI] Completion of WiFi and MQTT Configuration (#1603) * Configure WiFi - 1/2 working * WiFi and MQTT Configuration * Refactor MQTTtoSYS * Firmware Upgrade is Wired --- main/ZwebUI.ino | 412 +++++++++++++++++++++++++++++++++++---- main/config_WebContent.h | 12 +- main/config_WebUI.h | 2 +- main/main.ino | 33 ++-- 4 files changed, 399 insertions(+), 60 deletions(-) diff --git a/main/ZwebUI.ino b/main/ZwebUI.ino index 3bd8535..32f6658 100644 --- a/main/ZwebUI.ino +++ b/main/ZwebUI.ino @@ -179,6 +179,68 @@ size_t strchrspn(const char* str1, int character) { return ret; } +int WifiGetRssiAsQuality(int rssi) { + int quality = 0; + + if (rssi <= -100) { + quality = 0; + } else if (rssi >= -50) { + quality = 100; + } else { + quality = 2 * (rssi + 100); + } + return quality; +} + +char* GetTextIndexed(char* destination, size_t destination_size, uint32_t index, const char* haystack) { + // Returns empty string if not found + // Returns text of found + char* write = destination; + const char* read = haystack; + + index++; + while (index--) { + size_t size = destination_size - 1; + write = destination; + char ch = '.'; + while ((ch != '\0') && (ch != '|')) { + ch = pgm_read_byte(read++); + if (size && (ch != '|')) { + *write++ = ch; + size--; + } + } + if (0 == ch) { + if (index) { + write = destination; + } + break; + } + } + *write = '\0'; + return destination; +} + +const char kUnescapeCode[] = "&><\"\'\\"; +const char kEscapeCode[] PROGMEM = "&|>|<|"|'|\"; + +String HtmlEscape(const String unescaped) { + char escaped[10]; + size_t ulen = unescaped.length(); + String result; + result.reserve(ulen); // pre-reserve the required space to avoid mutiple reallocations + for (size_t i = 0; i < ulen; i++) { + char c = unescaped[i]; + char* p = strchr(kUnescapeCode, c); + if (p != nullptr) { + result += GetTextIndexed(escaped, sizeof(escaped), p - kUnescapeCode, kEscapeCode); + } else { + result += c; + } + } + return result; +} + # endif // ESP32 void AddLogData(uint32_t loglevel, const char* log_data, const char* log_data_payload = nullptr, const char* log_data_retained = nullptr) { @@ -439,14 +501,142 @@ void handleCN() { /** * @brief /WI - Configure WiFi Page - * + * T: handleWI: uri: /wi, args: 4, method: 1 + * T: handleWI Arg: 0, s1=SSID + * T: handleWI Arg: 1, p1=xxxxxx + * T: handleWI Arg: 3, save= */ void handleWI() { WEBUI_TRACE_LOG(F("handleWI: uri: %s, args: %d, method: %d" CR), server.uri(), server.args(), server.method()); + String WiFiScan = ""; if (server.args()) { for (uint8_t i = 0; i < server.args(); i++) { WEBUI_TRACE_LOG(F("handleWI Arg: %d, %s=%s" CR), i, server.argName(i).c_str(), server.arg(i).c_str()); } + if (server.hasArg("scan")) { + bool limitScannedNetworks = true; + int n = WiFi.scanNetworks(); + + WEBUI_TRACE_LOG(F("handleWI scan: found %d" CR), n); + if (0 == n) { + // WSContentSend_P(PSTR(D_NO_NETWORKS_FOUND)); + // limitScannedNetworks = false; // in order to show D_SCAN_FOR_WIFI_NETWORKS + } else { + //sort networks + int indices[n]; + for (uint32_t i = 0; i < n; i++) { + indices[i] = i; + } + + // RSSI SORT + for (uint32_t i = 0; i < n; i++) { + for (uint32_t j = i + 1; j < n; j++) { + if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) { + std::swap(indices[i], indices[j]); + } + } + } + + uint32_t networksToShow = n; + if ((limitScannedNetworks) && (networksToShow > MAX_WIFI_NETWORKS_TO_SHOW)) { + networksToShow = MAX_WIFI_NETWORKS_TO_SHOW; + } + + for (uint32_t i = 0; i < n; i++) { + if (-1 == indices[i]) { + continue; + } + String cssid = WiFi.SSID(indices[i]); + uint32_t cschn = WiFi.channel(indices[i]); + for (uint32_t j = i + 1; j < n; j++) { + if ((cssid == WiFi.SSID(indices[j])) && (cschn == WiFi.channel(indices[j]))) { + WEBUI_TRACE_LOG(F("handleWI scan: duplicate %s" CR), WiFi.SSID(indices[j]).c_str()); + indices[j] = -1; // set dup aps to index -1 + } + } + } + + //display networks in page + for (uint32_t i = 0; i < networksToShow; i++) { + if (-1 == indices[i]) { + continue; + } // skip dups + int32_t rssi = WiFi.RSSI(indices[i]); + WEBUI_TRACE_LOG(F("D_LOG_WIFI D_SSID %s, D_BSSID %s, D_CHANNEL %d, D_RSSI %d" CR), + WiFi.SSID(indices[i]).c_str(), WiFi.BSSIDstr(indices[i]).c_str(), WiFi.channel(indices[i]), rssi); + int quality = WifiGetRssiAsQuality(rssi); + String ssid_copy = WiFi.SSID(indices[i]); + if (!ssid_copy.length()) { + ssid_copy = F("no_name"); + } + + WiFiScan += "
" + HtmlEscape(ssid_copy) + " (" + WiFi.channel(indices[i]) + ") " + quality + "% (" + rssi + " dBm)
"; + } + } + WEBUI_TRACE_LOG(F("handleWI scan: results %s" CR), WiFiScan.c_str()); + + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); + + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Configure WiFi").c_str()); + String response = String(buffer); + response += String(wifi_script); + response += String(script); + response += String(style); + // wifi_ssid, wifi_password, gateway_name + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_wifi_body, jsonChar, gateway_name, WiFiScan.c_str(), WiFi.SSID(), WiFi.psk()); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); + return; + + } else if (server.hasArg("save")) { + StaticJsonDocument jsonBuffer; + JsonObject WEBtoSYS = jsonBuffer.to(); + bool update = false; + if (server.hasArg("s1")) { + WEBtoSYS["wifi_ssid"] = server.arg("s1"); + if (strncmp((char*)WiFi.SSID().c_str(), server.arg("s1").c_str(), parameters_size)) { + update = true; + } + } + if (server.hasArg("p1")) { + WEBtoSYS["wifi_pass"] = server.arg("p1"); + if (strncmp((char*)WiFi.psk().c_str(), server.arg("p1").c_str(), parameters_size)) { + update = true; + } + } + if (update) { + String topic = String(mqtt_topic) + String(gateway_name) + String(subjectMQTTtoSYSset); + String output; + serializeJson(WEBtoSYS, output); + Log.notice(F("[WebUI] MQTTtoSYS %s" CR), output.c_str()); + Log.warning(F("[WebUI] Save WiFi and Restart" CR)); + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Save WiFi and Restart").c_str()); + String response = String(buffer); + response += String(restart_script); + response += String(script); + response += String(style); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, reset_body, jsonChar, gateway_name, "Save WiFi and Restart"); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); + + delay(2000); // Wait for web page to be sent before + MQTTtoSYS((char*)topic.c_str(), WEBtoSYS); + return; + } else { + Log.warning(F("[WebUI] No changes" CR)); + } + } } char jsonChar[100]; serializeJson(modules, jsonChar, measureJson(modules) + 1); @@ -455,9 +645,11 @@ void handleWI() { snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Configure WiFi").c_str()); String response = String(buffer); + response += String(wifi_script); response += String(script); response += String(style); - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_wifi_body, jsonChar, gateway_name, "", "", "", "", "", ""); + // wifi_ssid, wifi_password, gateway_name + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_wifi_body, jsonChar, gateway_name, WiFiScan.c_str(), WiFi.SSID(), WiFi.psk()); response += String(buffer); snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); response += String(buffer); @@ -466,12 +658,13 @@ void handleWI() { /** * @brief /MQ - Configure MQTT Page - * T: handleMQ Arg: 0, mh=192.168.1.17 + * T: handleMQ: uri: /mq, args: 8, method: 1 + * T: handleMQ Arg: 0, mh=192.168.1.11 * T: handleMQ Arg: 1, ml=1883 - * T: handleMQ Arg: 2, mu=your_username - * T: handleMQ Arg: 3, mp=your_password + * T: handleMQ Arg: 2, mu=1234 + * T: handleMQ Arg: 3, mp= * T: handleMQ Arg: 4, sc=on - * T: handleMQ Arg: 5, msc=suds + * T: handleMQ Arg: 5, h= * T: handleMQ Arg: 6, mt=home/ * T: handleMQ Arg: 7, save= */ @@ -482,28 +675,94 @@ void handleMQ() { WEBUI_TRACE_LOG(F("handleMQ Arg: %d, %s=%s" CR), i, server.argName(i).c_str(), server.arg(i).c_str()); } if (server.hasArg("save")) { + StaticJsonDocument jsonBuffer; + JsonObject WEBtoSYS = jsonBuffer.to(); + bool update = false; + if (server.hasArg("mh")) { - strncpy(mqtt_server, server.arg("mh").c_str(), parameters_size); + WEBtoSYS["mqtt_server"] = server.arg("mh"); + if (strncmp(mqtt_server, server.arg("mh").c_str(), parameters_size)) { + update = true; + } } + if (server.hasArg("ml")) { - strncpy(mqtt_port, server.arg("ml").c_str(), 6); + WEBtoSYS["mqtt_port"] = server.arg("ml"); + if (strncmp(mqtt_port, server.arg("ml").c_str(), 6)) { + update = true; + } } + if (server.hasArg("mu")) { - strncpy(mqtt_user, server.arg("mu").c_str(), parameters_size); + WEBtoSYS["mqtt_user"] = server.arg("mu"); + if (strncmp(mqtt_user, server.arg("mu").c_str(), parameters_size)) { + update = true; + } } + if (server.hasArg("mp")) { - strncpy(mqtt_pass, server.arg("mp").c_str(), parameters_size); + WEBtoSYS["mqtt_pass"] = server.arg("mp"); + if (strncmp(mqtt_pass, server.arg("mp").c_str(), parameters_size)) { + update = true; + } } - mqtt_secure = server.hasArg("sc"); - if (server.hasArg("msc")) { - mqtt_cert = server.arg("msc"); // String + + // SC - Secure Connection argument is only present when true + WEBtoSYS["mqtt_secure"] = server.hasArg("sc"); + if (mqtt_secure != server.hasArg("sc")) { + update = true; } + + if (!update) { + Log.warning(F("[WebUI] clearing" CR)); + for (JsonObject::iterator it = WEBtoSYS.begin(); it != WEBtoSYS.end(); ++it) { + WEBtoSYS.remove(it); + } + } + + if (server.hasArg("h")) { + WEBtoSYS["gateway_name"] = server.arg("h"); + if (strncmp(gateway_name, server.arg("h").c_str(), parameters_size)) { + update = true; + } + } + if (server.hasArg("mt")) { - strncpy(mqtt_topic, server.arg("mt").c_str(), parameters_size); + WEBtoSYS["mqtt_topic"] = server.arg("mt"); + if (strncmp(mqtt_topic, server.arg("mt").c_str(), parameters_size)) { + update = true; + } } + # ifndef ESPWifiManualSetup - saveMqttConfig(); - connectMQTT(); + if (update) { + Log.warning(F("[WebUI] Save MQTT and Restart" CR)); + + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Save MQTT and Restart").c_str()); + String response = String(buffer); + response += String(restart_script); + response += String(script); + response += String(style); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, reset_body, jsonChar, gateway_name, "Save MQTT and Restart"); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); + + delay(2000); // Wait for web page to be sent before + String topic = String(mqtt_topic) + String(gateway_name) + String(subjectMQTTtoSYSset); + String output; + serializeJson(WEBtoSYS, output); + Log.notice(F("[WebUI] MQTTtoSYS %s" CR), output.c_str()); + MQTTtoSYS((char*)topic.c_str(), WEBtoSYS); + return; + } else { + Log.warning(F("[WebUI] No changes" CR)); + } # endif } } @@ -518,7 +777,7 @@ void handleMQ() { response += String(script); response += String(style); // mqtt server (mh), mqtt port (ml), mqtt username (mu), mqtt password (mp), secure connection (sc), server certificate (msc), topic (mt) - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, mqtt_server, mqtt_port, mqtt_user, mqtt_pass, (mqtt_secure ? "checked" : ""), mqtt_cert, mqtt_topic); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, mqtt_server, mqtt_port, mqtt_user, mqtt_pass, (mqtt_secure ? "checked" : ""), gateway_name, mqtt_topic); response += String(buffer); snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); response += String(buffer); @@ -537,7 +796,8 @@ void handleLO() { for (uint8_t i = 0; i < server.args(); i++) { WEBUI_TRACE_LOG(F("handleLO Arg: %d, %s=%s" CR), i, server.argName(i).c_str(), server.arg(i).c_str()); } - if (server.hasArg("save") && server.hasArg("lo")) { + if (server.hasArg("save") && server.hasArg("lo") && server.arg("lo").toInt() != Log.getLevel()) { + Log.fatal(F("[WebUI] Log level changed to: %d" CR), server.arg("lo").toInt()); Log.setLevel(server.arg("lo").toInt()); } } @@ -770,25 +1030,82 @@ void handleUP() { for (uint8_t i = 0; i < server.args(); i++) { WEBUI_TRACE_LOG(F("handleUP Arg: %d, %s=%s" CR), i, server.argName(i).c_str(), server.arg(i).c_str()); } - } else { - char jsonChar[100]; - serializeJson(modules, jsonChar, measureJson(modules) + 1); + StaticJsonDocument jsonBuffer; + JsonObject WEBtoSYS = jsonBuffer.to(); - char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + if (server.hasArg("o")) { + WEBtoSYS["url"] = server.arg("o"); + WEBtoSYS["version"] = "test"; + WEBtoSYS["password"] = ota_pass; - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Firmware Upgrade").c_str()); - String response = String(buffer); - response += String(script); - response += String(style); - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, upgrade_body, jsonChar, gateway_name, "http://upgrade/"); - response += String(buffer); - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); - response += String(buffer); - server.send(200, "text/html", response); + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Updating Firmware and Restart").c_str()); + String response = String(buffer); + response += String(restart_script); + response += String(script); + response += String(style); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, reset_body, jsonChar, gateway_name, "Updating Firmware and Restart"); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); + + delay(2000); // Wait for web page to be sent before + String topic = String(mqtt_topic) + String(gateway_name) + String(subjectMQTTtoSYSupdate); + String output; + serializeJson(WEBtoSYS, output); + Log.notice(F("[WebUI] MQTTtoSYSupdate %s" CR), output.c_str()); + MQTTHttpsFWUpdate((char*)topic.c_str(), WEBtoSYS); + return; + } else if (server.hasArg("le")) { + uint32_t le = server.arg("le").toInt(); + if (le != 0) { + WEBtoSYS["version"] = (le == 1 ? "latest" : (le == 2 ? "dev" : "unknown")); + WEBtoSYS["password"] = ota_pass; + + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Updating Firmware and Restart").c_str()); + String response = String(buffer); + response += String(restart_script); + response += String(script); + response += String(style); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, reset_body, jsonChar, gateway_name, "Updating Firmware and Restart"); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); + + delay(2000); // Wait for web page to be sent before + String topic = String(mqtt_topic) + String(gateway_name) + String(subjectMQTTtoSYSupdate); + String output; + serializeJson(WEBtoSYS, output); + Log.notice(F("[WebUI] MQTTtoSYSupdate %s" CR), output.c_str()); + MQTTHttpsFWUpdate((char*)topic.c_str(), WEBtoSYS); + return; + } + } } -} + char jsonChar[100]; + serializeJson(modules, jsonChar, measureJson(modules) + 1); -uint32_t logIndex = 0; + char buffer[WEB_TEMPLATE_BUFFER_MAX_SIZE]; + + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, header_html, (String(gateway_name) + " - Firmware Upgrade").c_str()); + String response = String(buffer); + response += String(script); + response += String(style); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, upgrade_body, jsonChar, gateway_name, "https://github.com/1technophile/OpenMQTTGateway/releases/"); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); +} /** * @brief /CS - Serial Console and Command Line @@ -1067,7 +1384,7 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { strlcpy(message->title, strtok(topic, "/"), WEBUI_TEXT_WIDTH); free(topic); - WEBUI_TRACE_LOG(F("[ webUIPubPrint ] switch %s " CR), message->title); + // WEBUI_TRACE_LOG(F("[ webUIPubPrint ] switch %s " CR), message->title); switch (webUIHash(message->title)) { case webUIHash("SYStoMQTT"): { // {"uptime":456356,"version":"lilygo-rtl_433-test-A-v1.1.1-25-g574177d[lily-cloud]","freemem":125488,"mqttport":"1883","mqttsecure":false,"freestack":3752,"rssi":-36,"SSID":"The_Beach","BSSID":"64:A5:C3:69:C3:38","ip":"192.168.1.239","mac":"4C:75:25:A8:D5:D8","actRec":3,"mhz":433.92,"RTLRssiThresh":-98,"RTLRssi":-108,"RTLAVGRssi":-107,"RTLCnt":121707,"RTLOOKThresh":90,"modules":["LILYGO_OLED","CLOUD","rtl_433"]} @@ -1111,7 +1428,7 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { # ifdef ZgatewayRTL_433 case webUIHash("RTL_433toMQTT"): { - if (!strncmp(data["model"], "status", 6) && data.containsKey("id")) { + if (strncmp(data["model"], "status", 6)) { // Does not contain "status" // {"model":"Acurite-Tower","id":2043,"channel":"B","battery_ok":1,"temperature_C":5.3,"humidity":81,"mic":"CHECKSUM","protocol":"Acurite 592TXR Temp/Humidity, 5n1 Weather Station, 6045 Lightning, 3N1, Atlas","rssi":-81,"duration":121060} // Line 1 @@ -1120,10 +1437,12 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { // Line 2 - String id = data["id"]; - String channel = data["channel"]; - String line2 = "id: " + id + " channel: " + channel; - line2.toCharArray(message->line2, WEBUI_TEXT_WIDTH); + if (data["id"] || data["channel"]) { + String id = data["id"]; + String channel = data["channel"]; + String line2 = "id: " + id + " channel: " + channel; + line2.toCharArray(message->line2, WEBUI_TEXT_WIDTH); + } // Line 3 @@ -1165,10 +1484,14 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { // Line 4 - String rssi = data["rssi"]; - String battery_ok = data["battery_ok"]; + String line4 = ""; + if (data["battery_ok"]) { + line4 = "batt: " + data["battery_ok"].as(); + } else { + line4 = "pulses: " + data["pulses"].as(); + } - String line4 = "batt: " + battery_ok + " rssi: " + rssi; + line4 += " rssi: " + data["rssi"].as(); line4.toCharArray(message->line4, WEBUI_TEXT_WIDTH); // Queue completed message @@ -1179,6 +1502,9 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { } else { // Log.notice(F("[ WebUI ] Queued %s" CR), message->title); } + } else { + Log.error(F("[ WebUI ] rtl_433 not displaying %s" CR), message->title); + free(message); } break; } @@ -1559,7 +1885,7 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { Log.error(F("[ WebUI ] webUIQueue full, discarding signal %s" CR), message->title); free(message); } else { - Log.notice(F("[ WebUI ] Queued %s" CR), message->title); + // Log.notice(F("[ WebUI ] Queued %s" CR), message->title); } } else { WEBUI_TRACE_LOG(F("[ WebUI ] incomplete messaage %s" CR), topicori); diff --git a/main/config_WebContent.h b/main/config_WebContent.h index 2a511c9..ff52208 100644 --- a/main/config_WebContent.h +++ b/main/config_WebContent.h @@ -61,7 +61,9 @@ const char restart_script[] = "setTimeout(function() { location.href = '.'; }, 1 const char information_script[] = "function i() { var s, o = \"
%s
\"; s = o.replace(/}1/g, \"\").replace(/}2/g, \"\"); eb('i').innerHTML = s; } wl(i);"; -const char console_script[] = " var sn = 0, id = 0, ft, ltm = 2345; function l(p) { var c, o = ''; clearTimeout(lt); clearTimeout(ft); t = eb('t1'); if (p == 1) { c = eb('c1'); o = '&c1=' + encodeURIComponent(c.value); c.value = ''; t.scrollTop = 99999; sn = t.scrollTop; } if (t.scrollTop >= sn) { if (x != null) { x.abort(); } x = new XMLHttpRequest(); x.onreadystatechange = function() { if (x.readyState == 4 && x.status == 200) { var z, d; d = x.responseText.split(/}1/); id = d.shift(); if (d.shift() == 0) { t.value = ''; } z = d.shift(); if (z.length > 0) { t.value += z; } t.scrollTop = 99999; sn = t.scrollTop; clearTimeout(ft); lt = setTimeout(l, ltm); } }; x.open('GET', 'cs?c2=' + id + o, true); x.send(); ft = setTimeout(l, 20000); } else { lt = setTimeout(l, ltm); } return false; } wl(l); var hc = [], cn = 0; function h() { eb('c1').addEventListener('keydown', function(e) { var b = eb('c1'), c = e.keyCode; if (38 == c || 40 == c) { b.autocomplete = 'off'; } 38 == c ? (++cn > hc.length && (cn = hc.length), b.value = hc[cn - 1] || '') : 40 == c ? (0 > --cn && (cn = 0), b.value = hc[cn - 1] || '') : 13 == c && (hc.length > 19 && hc.pop(), hc.unshift(b.value), cn = 0) }); } wl(h);"; +const char console_script[] = "var sn = 0, id = 0, ft, ltm = 2345; function l(p) { var c, o = ''; clearTimeout(lt); clearTimeout(ft); t = eb('t1'); if (p == 1) { c = eb('c1'); o = '&c1=' + encodeURIComponent(c.value); c.value = ''; t.scrollTop = 99999; sn = t.scrollTop; } if (t.scrollTop >= sn) { if (x != null) { x.abort(); } x = new XMLHttpRequest(); x.onreadystatechange = function() { if (x.readyState == 4 && x.status == 200) { var z, d; d = x.responseText.split(/}1/); id = d.shift(); if (d.shift() == 0) { t.value = ''; } z = d.shift(); if (z.length > 0) { t.value += z; } t.scrollTop = 99999; sn = t.scrollTop; clearTimeout(ft); lt = setTimeout(l, ltm); } }; x.open('GET', 'cs?c2=' + id + o, true); x.send(); ft = setTimeout(l, 20000); } else { lt = setTimeout(l, ltm); } return false; } wl(l); var hc = [], cn = 0; function h() { eb('c1').addEventListener('keydown', function(e) { var b = eb('c1'), c = e.keyCode; if (38 == c || 40 == c) { b.autocomplete = 'off'; } 38 == c ? (++cn > hc.length && (cn = hc.length), b.value = hc[cn - 1] || '') : 40 == c ? (0 > --cn && (cn = 0), b.value = hc[cn - 1] || '') : 13 == c && (hc.length > 19 && hc.pop(), hc.unshift(b.value), cn = 0) }); } wl(h);"; + +const char wifi_script[] = "function c(l) {eb('s1').value = l.innerText || l.textContent; eb('p1').focus(); }"; const char script[] = "function jd() { var t = 0, i = document.querySelectorAll('input,button,textarea,select'); while (i.length >= t) { if (i[t]) { i[t]['name'] = (i[t].hasAttribute('id') && (!i[t].hasAttribute('name'))) ? i[t]['id'] : i[t]['name']; } t++; } } wl(jd); "; @@ -81,13 +83,15 @@ const char console_body[] = body_header "

MQTT Base Topic


" body_footer_config_menu; +const char config_mqtt_body[] = body_header "
 MQTT parameters 

MQTT Server

MQTT Port

MQTT Username


MQTT Secure Connection

Gateway Name

MQTT Base Topic


" body_footer_config_menu; const char config_logging_body[] = body_header "
OpenMQTTGateway Logging

log level


" body_footer_config_menu; diff --git a/main/config_WebUI.h b/main/config_WebUI.h index dbf6026..e6fa5fc 100644 --- a/main/config_WebUI.h +++ b/main/config_WebUI.h @@ -58,7 +58,7 @@ /*------------------- End of Compiler Directives ----------------------*/ -// Structure for queueing OMG messages to the display +#define MAX_WIFI_NETWORKS_TO_SHOW 10 /* Structure for queueing OMG messages to the display. diff --git a/main/main.ino b/main/main.ino index a996ea7..527c0fb 100644 --- a/main/main.ino +++ b/main/main.ino @@ -2355,7 +2355,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding client.disconnect(); WiFi.disconnect(true); - Log.warning(F("Attempting connection to new AP" CR)); + Log.warning(F("Attempting connection to new AP %s" CR), (const char*)SYSdata["wifi_ssid"]); WiFi.begin((const char*)SYSdata["wifi_ssid"], (const char*)SYSdata["wifi_pass"]); # if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower)) setESP32WifiPorotocolTxPower(); @@ -2373,6 +2373,21 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding ESPRestart(); } + bool disconnectClient = false; // Trigger client.disconnet if a user/password change doesn't + + if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) { + if (SYSdata.containsKey("mqtt_topic")) { + strncpy(mqtt_topic, SYSdata["mqtt_topic"], parameters_size); + } + if (SYSdata.containsKey("gateway_name")) { + strncpy(gateway_name, SYSdata["gateway_name"], parameters_size); + } +# ifndef ESPWifiManualSetup + saveMqttConfig(); +# endif + disconnectClient = true; // trigger reconnect in loop using the new topic/name + } + # ifdef MQTTsetMQTT if (SYSdata.containsKey("mqtt_user") && SYSdata.containsKey("mqtt_pass")) { bool update_server = false; @@ -2399,6 +2414,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding # if defined(ZgatewayBT) && defined(ESP32) stopProcessing(); # endif + disconnectClient = false; client.disconnect(); update_server = true; if (secure_connect != mqtt_secure) { @@ -2422,6 +2438,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding # if defined(ZgatewayBT) && defined(ESP32) stopProcessing(); # endif + disconnectClient = false; client.disconnect(); } @@ -2465,17 +2482,9 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding ESPRestart(); } # endif - if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) { - if (SYSdata.containsKey("mqtt_topic")) { - strncpy(mqtt_topic, SYSdata["mqtt_topic"], parameters_size); - } - if (SYSdata.containsKey("gateway_name")) { - strncpy(gateway_name, SYSdata["gateway_name"], parameters_size); - } -# ifndef ESPWifiManualSetup - saveMqttConfig(); -# endif - client.disconnect(); // reconnects in loop using the new topic/name + + if (disconnectClient) { + client.disconnect(); } #endif