From 1c367fd32526c65ec74a7655304b19edf5e42913 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Thu, 6 Apr 2023 20:51:14 -0400 Subject: [PATCH] [WebUI] Add Configure MQTT and Logging (#1592) * Configure MQTT and Loggin * Configure MQTT speed up * Lint --- main/User_config.h | 5 ++ main/ZwebUI.ino | 178 +++++++++++++++++++++++++++------------ main/config_WebContent.h | 14 +-- main/main.ino | 10 ++- 4 files changed, 144 insertions(+), 63 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 8f2e4f6245..2137fdc481 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -678,6 +678,11 @@ CRGB leds2[FASTLED_IND_NUM_LEDS]; char mqtt_topic[parameters_size + 1] = Base_Topic; char gateway_name[parameters_size + 1] = Gateway_Name; +void connectMQTT(); +#ifndef ESPWifiManualSetup +void saveMqttConfig(); +#endif + unsigned long uptime(); bool cmpToMainTopic(const char*, const char*); void pub(const char*, const char*, bool); diff --git a/main/ZwebUI.ino b/main/ZwebUI.ino index 593301c409..3bd8535709 100644 --- a/main/ZwebUI.ino +++ b/main/ZwebUI.ino @@ -466,7 +466,14 @@ void handleWI() { /** * @brief /MQ - Configure MQTT Page - * + * T: handleMQ Arg: 0, mh=192.168.1.17 + * T: handleMQ Arg: 1, ml=1883 + * T: handleMQ Arg: 2, mu=your_username + * T: handleMQ Arg: 3, mp=your_password + * T: handleMQ Arg: 4, sc=on + * T: handleMQ Arg: 5, msc=suds + * T: handleMQ Arg: 6, mt=home/ + * T: handleMQ Arg: 7, save= */ void handleMQ() { WEBUI_TRACE_LOG(F("handleMQ: uri: %s, args: %d, method: %d" CR), server.uri(), server.args(), server.method()); @@ -474,7 +481,33 @@ void handleMQ() { for (uint8_t i = 0; i < server.args(); i++) { 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")) { + if (server.hasArg("mh")) { + strncpy(mqtt_server, server.arg("mh").c_str(), parameters_size); + } + if (server.hasArg("ml")) { + strncpy(mqtt_port, server.arg("ml").c_str(), 6); + } + if (server.hasArg("mu")) { + strncpy(mqtt_user, server.arg("mu").c_str(), parameters_size); + } + if (server.hasArg("mp")) { + strncpy(mqtt_pass, server.arg("mp").c_str(), parameters_size); + } + mqtt_secure = server.hasArg("sc"); + if (server.hasArg("msc")) { + mqtt_cert = server.arg("msc"); // String + } + if (server.hasArg("mt")) { + strncpy(mqtt_topic, server.arg("mt").c_str(), parameters_size); + } +# ifndef ESPWifiManualSetup + saveMqttConfig(); + connectMQTT(); +# endif + } } + char jsonChar[100]; serializeJson(modules, jsonChar, measureJson(modules) + 1); @@ -484,8 +517,42 @@ void handleMQ() { String response = String(buffer); response += String(script); response += String(style); - // mqtt server, mqtt port, client id, mqtt username, mqtt password, topic, Full Topic - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, mqtt_server, mqtt_port, gateway_name, mqtt_user, mqtt_pass, mqtt_topic, ""); + // 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); + response += String(buffer); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); + response += String(buffer); + server.send(200, "text/html", response); +} + +/** + * @brief /LO - Configure Logging Page + * T: handleLO: uri: /lo, args: 2, method: 1 + * T: handleLO Arg: 0, lo=5 + * T: handleLO Arg: 1, save= + */ +void handleLO() { + WEBUI_TRACE_LOG(F("handleLO: uri: %s, args: %d, method: %d" CR), server.uri(), server.args(), server.method()); + if (server.args()) { + 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")) { + Log.setLevel(server.arg("lo").toInt()); + } + } + + 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 Logging").c_str()); + String response = String(buffer); + response += String(script); + response += String(style); + int logLevel = Log.getLevel(); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_logging_body, jsonChar, gateway_name, (logLevel == 0 ? "selected" : ""), (logLevel == 1 ? "selected" : ""), (logLevel == 2 ? "selected" : ""), (logLevel == 3 ? "selected" : ""), (logLevel == 4 ? "selected" : ""), (logLevel == 5 ? "selected" : ""), (logLevel == 6 ? "selected" : "")); response += String(buffer); snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); response += String(buffer); @@ -837,12 +904,13 @@ void WebUISetup() { server.on("/up", handleUP); // Firmware Upgrade server.on("/cn", handleCN); // Configuration - server.on("/wi", handleWI); // Configuration Wifi - server.on("/mq", handleMQ); // Configuration MQTT + server.on("/wi", handleWI); // Configure Wifi + server.on("/mq", handleMQ); // Configure MQTT # if defined(ZgatewayCloud) - server.on("/cl", handleCL); // Cloud configuration + server.on("/cl", handleCL); // Configure Cloud server.on("/tk", handleTK); // Store Device Token # endif + server.on("/lo", handleLO); // Configure Logging server.on("/rt", handleRT); // Reset configuration ( Erase and Restart ) server.begin(); @@ -1043,72 +1111,74 @@ void webUIPubPrint(const char* topicori, JsonObject& data) { # ifdef ZgatewayRTL_433 case webUIHash("RTL_433toMQTT"): { - // {"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} + if (!strncmp(data["model"], "status", 6) && data.containsKey("id")) { + // {"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 + // Line 1 - strlcpy(message->line1, data["model"], WEBUI_TEXT_WIDTH); + strlcpy(message->line1, data["model"], WEBUI_TEXT_WIDTH); - // Line 2 + // Line 2 - String id = data["id"]; - String channel = data["channel"]; - String line2 = "id: " + id + " channel: " + channel; - line2.toCharArray(message->line2, WEBUI_TEXT_WIDTH); + String id = data["id"]; + String channel = data["channel"]; + String line2 = "id: " + id + " channel: " + channel; + line2.toCharArray(message->line2, WEBUI_TEXT_WIDTH); - // Line 3 + // Line 3 - String line3 = ""; + String line3 = ""; - if (data.containsKey("temperature_C")) { - float temperature_C = data["temperature_C"]; - char temp[5]; + if (data.containsKey("temperature_C")) { + float temperature_C = data["temperature_C"]; + char temp[5]; - if (displayMetric) { - dtostrf(temperature_C, 3, 1, temp); - line3 = "temp: " + (String)temp + "°C "; - } else { - dtostrf(convertTemp_CtoF(temperature_C), 3, 1, temp); - line3 = "temp: " + (String)temp + "°F "; + if (displayMetric) { + dtostrf(temperature_C, 3, 1, temp); + line3 = "temp: " + (String)temp + "°C "; + } else { + dtostrf(convertTemp_CtoF(temperature_C), 3, 1, temp); + line3 = "temp: " + (String)temp + "°F "; + } } - } - float humidity = data["humidity"]; - if (data.containsKey("humidity") && humidity <= 100 && humidity >= 0) { - char hum[5]; - dtostrf(humidity, 3, 1, hum); - line3 += "hum: " + (String)hum + "% "; - } - if (data.containsKey("wind_avg_km_h")) { - float wind_avg_km_h = data["wind_avg_km_h"]; - char wind[6]; + float humidity = data["humidity"]; + if (data.containsKey("humidity") && humidity <= 100 && humidity >= 0) { + char hum[5]; + dtostrf(humidity, 3, 1, hum); + line3 += "hum: " + (String)hum + "% "; + } + if (data.containsKey("wind_avg_km_h")) { + float wind_avg_km_h = data["wind_avg_km_h"]; + char wind[6]; - if (displayMetric) { - dtostrf(wind_avg_km_h, 3, 1, wind); - line3 += "wind: " + (String)wind + "km/h "; - } else { - dtostrf(convert_kmph2mph(wind_avg_km_h), 3, 1, wind); - line3 += "wind: " + (String)wind + "mp/h "; + if (displayMetric) { + dtostrf(wind_avg_km_h, 3, 1, wind); + line3 += "wind: " + (String)wind + "km/h "; + } else { + dtostrf(convert_kmph2mph(wind_avg_km_h), 3, 1, wind); + line3 += "wind: " + (String)wind + "mp/h "; + } } - } - line3.toCharArray(message->line3, WEBUI_TEXT_WIDTH); + line3.toCharArray(message->line3, WEBUI_TEXT_WIDTH); - // Line 4 + // Line 4 - String rssi = data["rssi"]; - String battery_ok = data["battery_ok"]; + String rssi = data["rssi"]; + String battery_ok = data["battery_ok"]; - String line4 = "batt: " + battery_ok + " rssi: " + rssi; - line4.toCharArray(message->line4, WEBUI_TEXT_WIDTH); + String line4 = "batt: " + battery_ok + " rssi: " + rssi; + line4.toCharArray(message->line4, WEBUI_TEXT_WIDTH); - // Queue completed message + // Queue completed message - if (xQueueSend(webUIQueue, (void*)&message, 0) != pdTRUE) { - Log.error(F("[ WebUI ] webUIQueue full, discarding signal %s" CR), message->title); - free(message); - } else { - // Log.notice(F("[ WebUI ] Queued %s" CR), message->title); + if (xQueueSend(webUIQueue, (void*)&message, 0) != pdTRUE) { + Log.error(F("[ WebUI ] webUIQueue full, discarding signal %s" CR), message->title); + free(message); + } else { + // Log.notice(F("[ WebUI ] Queued %s" CR), message->title); + } } break; } diff --git a/main/config_WebContent.h b/main/config_WebContent.h index 7d68c9a26f..cc22bda6c1 100644 --- a/main/config_WebContent.h +++ b/main/config_WebContent.h @@ -31,7 +31,7 @@ // TODO: Create a script to generate these from WebPack #define body_footer_main_menu "

" -#define body_footer_config_menu "

"; +#define body_footer_config_menu "

" #define body_header "

%s

%s

" #define button_upgrade "

" @@ -45,7 +45,7 @@ #else # define configure_3 #endif -#define configure_4 +#define configure_4 "

" #define configure_5 #define configure_6 #define configure_7 @@ -75,19 +75,21 @@ const char reset_body[] = body_header "
%s
< const char config_cloud_body[] = body_header "
 Cloud Configuration 



" body_footer_config_menu; -const char token_body[] = body_header "div style='text-align:center;'>Link Cloud Account

Cloud was successfully linked

"; +const char token_body[] = body_header "
Link Cloud Account

Cloud was successfully linked

"; const char console_body[] = body_header "



" body_footer_main_menu; const char information_body[] = body_header "
" body_footer_main_menu; -const char upgrade_body[] = body_header "
 Upgrade by web server 

OTA Url




 Upgrade by file upload 



" body_footer_main_menu; +const char upgrade_body[] = body_header "

 Upgrade by web server 

OTA Url




 Upgrade by file upload 



" body_footer_main_menu; const char config_wifi_body[] = body_header "

%s
BELL634
MRC

Scan for all WiFi Networks

 Wifi parameters 

WiFi Network ()


WiFi Network 2 ()


Hostname%s

CORS Domain


" body_footer_config_menu; -// mqtt server, mqtt port, client id, mqtt username, mqtt password, topic, Full Topic +// mqtt server (mh), mqtt port (ml), mqtt username (mu), mqtt password (mp), secure connection (sc), server certificate (msc), topic (mt) -const char config_mqtt_body[] = body_header "
 MQTT parameters 

Host ()

Port (1883)

Client

User " MQTT_USER "


Topic

Full Topic


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

MQTT Server

MQTT Port

MQTT Username


MQTT Secure Connection

MQTT Server Certificate

MQTT Base Topic


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

log level


" body_footer_config_menu; const char footer[] = "

%s
"; diff --git a/main/main.ino b/main/main.ino index 68ba879e6c..a3299909ed 100644 --- a/main/main.ino +++ b/main/main.ino @@ -585,6 +585,9 @@ void delayWithOTA(long waitMillis) { # endif # endif ArduinoOTA.handle(); +# if defined(ZwebUI) && defined(ESP32) + WebUILoop(); +# endif delay(waitStep); } #else @@ -1589,6 +1592,9 @@ void loop() { if ((Ethernet.hardwareStatus() != EthernetW5100 && Ethernet.linkStatus() == LinkON) || (Ethernet.hardwareStatus() == EthernetW5100)) { //we are able to detect disconnection only on w5200 and w5500 #endif failure_number_ntwk = 0; +#if defined(ZwebUI) && defined(ESP32) + WebUILoop(); +#endif if (client.loop()) { // MQTT client is still connected InfoIndicatorON(); failure_number_ntwk = 0; @@ -1744,9 +1750,7 @@ void loop() { launchRTL_433Discovery(publishDiscovery); # endif #endif -#if defined(ZwebUI) && defined(ESP32) - WebUILoop(); -#endif + } else { // MQTT disconnected connected = false;