From 0a5833e6ec299db994218d17d2f0bf2791b2070f Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 28 Nov 2022 17:58:02 +0100 Subject: [PATCH] improved mqtt removed wrong "inverter type can't be detected!" messages repaired NTP and static IP #459 MQTT status about availability and produce are retain messages now --- src/config/settings.h | 41 +++++++++++-------------------------- src/defines.h | 2 +- src/hm/hmSystem.h | 2 +- src/publisher/pubMqtt.h | 11 ++++++---- src/utils/helper.cpp | 29 ++++++++++++++++++++++++++ src/utils/helper.h | 19 +++++++++++++++++ src/web/html/index.html | 4 ++-- src/web/web.cpp | 45 +++++++++++++++++------------------------ src/web/web.h | 9 --------- src/web/webApi.cpp | 15 +++++++------- src/web/webApi.h | 6 ------ 11 files changed, 96 insertions(+), 87 deletions(-) create mode 100644 src/utils/helper.cpp create mode 100644 src/utils/helper.h diff --git a/src/config/settings.h b/src/config/settings.h index 376dea091..c37c19fae 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -10,6 +10,7 @@ #include #include #include "../utils/dbg.h" +#include "../utils/helper.h" #include "../defines.h" /** @@ -223,25 +224,6 @@ class settings { return saveSettings(); } - String ip2Str(uint8_t ip[]) { - return String(ip[0]) + F(".") - + String(ip[1]) + F(".") - + String(ip[2]) + F(".") - + String(ip[3]); - } - - void ip2Arr(uint8_t ip[], const char *ipStr) { - char *tmp = new char[strlen(ipStr)]; - strncpy(tmp, ipStr, strlen(ipStr)); - char *p = strtok(tmp, "."); - uint8_t i = 0; - while(NULL != p) { - ip[i++] = atoi(p); - p = strtok(NULL, "."); - } - delete[] tmp; - } - private: void loadDefaults(bool wifi = true) { DPRINTLN(DBG_INFO, F("loadDefaults")); @@ -288,25 +270,26 @@ class settings { void jsonWifi(JsonObject obj, bool set = false) { if(set) { + char buf[16]; obj[F("ssid")] = mCfg.sys.stationSsid; obj[F("pwd")] = mCfg.sys.stationPwd; obj[F("dev")] = mCfg.sys.deviceName; obj[F("adm")] = mCfg.sys.adminPwd; - obj[F("ip")] = ip2Str(mCfg.sys.ip.ip); - obj[F("mask")] = ip2Str(mCfg.sys.ip.mask); - obj[F("dns1")] = ip2Str(mCfg.sys.ip.dns1); - obj[F("dns2")] = ip2Str(mCfg.sys.ip.dns2); - obj[F("gtwy")] = ip2Str(mCfg.sys.ip.gateway); + ah::ip2Char(mCfg.sys.ip.ip, buf); obj[F("ip")] = String(buf); + ah::ip2Char(mCfg.sys.ip.mask, buf); obj[F("mask")] = String(buf); + ah::ip2Char(mCfg.sys.ip.dns1, buf); obj[F("dns1")] = String(buf); + ah::ip2Char(mCfg.sys.ip.dns2, buf); obj[F("dns2")] = String(buf); + ah::ip2Char(mCfg.sys.ip.gateway, buf); obj[F("gtwy")] = String(buf); } else { snprintf(mCfg.sys.stationSsid, SSID_LEN, "%s", obj[F("ssid")].as()); snprintf(mCfg.sys.stationPwd, PWD_LEN, "%s", obj[F("pwd")].as()); snprintf(mCfg.sys.deviceName, DEVNAME_LEN, "%s", obj[F("dev")].as()); snprintf(mCfg.sys.adminPwd, PWD_LEN, "%s", obj[F("adm")].as()); - ip2Arr(mCfg.sys.ip.ip, obj[F("ip")]); - ip2Arr(mCfg.sys.ip.mask, obj[F("mask")]); - ip2Arr(mCfg.sys.ip.dns1, obj[F("dns1")]); - ip2Arr(mCfg.sys.ip.dns2, obj[F("dns2")]); - ip2Arr(mCfg.sys.ip.gateway, obj[F("gtwy")]); + ah::ip2Arr(mCfg.sys.ip.ip, obj[F("ip")].as()); + ah::ip2Arr(mCfg.sys.ip.mask, obj[F("mask")].as()); + ah::ip2Arr(mCfg.sys.ip.dns1, obj[F("dns1")].as()); + ah::ip2Arr(mCfg.sys.ip.dns2, obj[F("dns2")].as()); + ah::ip2Arr(mCfg.sys.ip.gateway, obj[F("gtwy")].as()); } } diff --git a/src/defines.h b/src/defines.h index 930e4243b..62f2cc420 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 43 +#define VERSION_PATCH 44 //------------------------------------- typedef struct { diff --git a/src/hm/hmSystem.h b/src/hm/hmSystem.h index 53b6e85d2..5338d1a00 100644 --- a/src/hm/hmSystem.h +++ b/src/hm/hmSystem.h @@ -70,7 +70,7 @@ class HmSystem { break; } } - else + else if(p->config->serial.u64 != 0ULL) DPRINTLN(DBG_ERROR, F("inverter type can't be detected!")); p->init(); diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 2a42501a7..89d8633b8 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -111,7 +111,8 @@ class PubMqtt { } void payloadEventListener(uint8_t cmd) { - mSendList.push(cmd); + if(mClient.connected()) // prevent overflow if MQTT broker is not reachable but set + mSendList.push(cmd); } void setSubscriptionCb(subscriptionCb cb) { @@ -185,6 +186,7 @@ class PubMqtt { void onWifiConnect(const WiFiEventStationModeGotIP& event) { DPRINTLN(DBG_VERBOSE, F("MQTT connecting")); mClient.connect(); + mEnReconnect = true; } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { @@ -197,6 +199,7 @@ class PubMqtt { case SYSTEM_EVENT_STA_GOT_IP: DPRINTLN(DBG_VERBOSE, F("MQTT connecting")); mClient.connect(); + mEnReconnect = true; break; case SYSTEM_EVENT_STA_DISCONNECTED: @@ -360,15 +363,15 @@ class PubMqtt { (status == MQTT_STATUS_AVAIL_NOT_PROD) ? "not " : "", (status == MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "" : "producing" ); - publish(topic, val); + publish(topic, val, true); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name); snprintf(val, 40, "%d", status); - publish(topic, val); + publish(topic, val, true); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->config->name); snprintf(val, 40, "%i", iv->getLastTs(rec) * 1000); - publish(topic, val); + publish(topic, val, true); } // data diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp new file mode 100644 index 000000000..8108ca3eb --- /dev/null +++ b/src/utils/helper.cpp @@ -0,0 +1,29 @@ +//----------------------------------------------------------------------------- +// 2022 Ahoy, https://github.com/lumpapu/ahoy +// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ +//----------------------------------------------------------------------------- + +#include "helper.h" + +namespace ah { + void ip2Arr(uint8_t ip[], const char *ipStr) { + memset(ip, 0, 4); + char *tmp = new char[strlen(ipStr)+1]; + strncpy(tmp, ipStr, strlen(ipStr)+1); + char *p = strtok(tmp, "."); + uint8_t i = 0; + while(NULL != p) { + ip[i++] = atoi(p); + p = strtok(NULL, "."); + } + delete[] tmp; + } + + // note: char *str needs to be at least 16 bytes long + void ip2Char(uint8_t ip[], char *str) { + if(0 == ip[0]) + str[0] = '\0'; + else + snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + } +} diff --git a/src/utils/helper.h b/src/utils/helper.h new file mode 100644 index 000000000..49351cc9c --- /dev/null +++ b/src/utils/helper.h @@ -0,0 +1,19 @@ +//----------------------------------------------------------------------------- +// 2022 Ahoy, https://ahoydtu.de +// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ +//----------------------------------------------------------------------------- + +#ifndef __HELPER_H__ +#define __HELPER_H__ + +#include +#include +#include +#include + +namespace ah { + void ip2Arr(uint8_t ip[], const char *ipStr); + void ip2Char(uint8_t ip[], char *str); +} + +#endif /*__HELPER_H__*/ diff --git a/src/web/html/index.html b/src/web/html/index.html index 96c92feb6..e0474c08e 100644 --- a/src/web/html/index.html +++ b/src/web/html/index.html @@ -196,9 +196,9 @@

Support this project:

parseStat(obj["statistics"]); parseIv(obj["inverter"]); parseWarnInfo(obj["warnings"], obj["infos"]); - document.getElementById("refresh").innerHTML = obj["refresh_interval"]; + document.getElementById("refresh").innerHTML = 10; if(exeOnce) { - window.setInterval("getAjax('/api/index', parse)", obj["refresh_interval"] * 1000); + window.setInterval("getAjax('/api/index', parse)", 10000); exeOnce = false; } } diff --git a/src/web/web.cpp b/src/web/web.cpp index d302eee34..e7425e76a 100644 --- a/src/web/web.cpp +++ b/src/web/web.cpp @@ -11,6 +11,7 @@ #include "web.h" #include "../utils/ahoyTimer.h" +#include "../utils/helper.h" #include "html/h/index_html.h" #include "html/h/login_html.h" @@ -343,28 +344,16 @@ void web::showSave(AsyncWebServerRequest *request) { // static ip - if(request->arg("ipAddr") != "") { - request->arg("ipAddr").toCharArray(buf, SSID_LEN); - ip2Arr(mConfig->sys.ip.ip, buf); - if(request->arg("ipMask") != "") { - request->arg("ipMask").toCharArray(buf, SSID_LEN); - ip2Arr(mConfig->sys.ip.mask, buf); - } - if(request->arg("ipDns1") != "") { - request->arg("ipDns1").toCharArray(buf, SSID_LEN); - ip2Arr(mConfig->sys.ip.dns1, buf); - } - if(request->arg("ipDns2") != "") { - request->arg("ipDns2").toCharArray(buf, SSID_LEN); - ip2Arr(mConfig->sys.ip.dns2, buf); - } - if(request->arg("ipGateway") != "") { - request->arg("ipGateway").toCharArray(buf, SSID_LEN); - ip2Arr(mConfig->sys.ip.gateway, buf); - } - } - else - memset(&mConfig->sys.ip.ip, 0, 4); + request->arg("ipAddr").toCharArray(buf, 20); + ah::ip2Arr(mConfig->sys.ip.ip, buf); + request->arg("ipMask").toCharArray(buf, 20); + ah::ip2Arr(mConfig->sys.ip.mask, buf); + request->arg("ipDns1").toCharArray(buf, 20); + ah::ip2Arr(mConfig->sys.ip.dns1, buf); + request->arg("ipDns2").toCharArray(buf, 20); + ah::ip2Arr(mConfig->sys.ip.dns2, buf); + request->arg("ipGateway").toCharArray(buf, 20); + ah::ip2Arr(mConfig->sys.ip.gateway, buf); // inverter @@ -437,12 +426,14 @@ void web::showSave(AsyncWebServerRequest *request) { String addr = request->arg("mqttAddr"); addr.trim(); addr.toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN); - request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN); - if(request->arg("mqttPwd") != "{PWD}") - request->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN); - request->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN); - mConfig->mqtt.port = request->arg("mqttPort").toInt(); } + else + mConfig->mqtt.broker[0] = '\0'; + request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN); + if(request->arg("mqttPwd") != "{PWD}") + request->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN); + request->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN); + mConfig->mqtt.port = request->arg("mqttPort").toInt(); // serial console if(request->arg("serIntvl") != "") { diff --git a/src/web/web.h b/src/web/web.h index 849f1caa6..7560e0399 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -64,15 +64,6 @@ class web { void onSerial(AsyncWebServerRequest *request); void onSystem(AsyncWebServerRequest *request); - void ip2Arr(uint8_t ip[], char *ipStr) { - char *p = strtok(ipStr, "."); - uint8_t i = 0; - while(NULL != p) { - ip[i++] = atoi(p); - p = strtok(NULL, "."); - } - } - #ifdef ENABLE_JSON_EP void showJson(void); #endif diff --git a/src/web/webApi.cpp b/src/web/webApi.cpp index 82a9efda1..4987e147f 100644 --- a/src/web/webApi.cpp +++ b/src/web/webApi.cpp @@ -332,13 +332,12 @@ void webApi::getSerial(JsonObject obj) { //----------------------------------------------------------------------------- void webApi::getStaticIp(JsonObject obj) { - if(mConfig->sys.ip.ip[0] != 0) { - obj[F("ip")] = ip2String(mConfig->sys.ip.ip); - obj[F("mask")] = ip2String(mConfig->sys.ip.mask); - obj[F("dns1")] = ip2String(mConfig->sys.ip.dns1); - obj[F("dns2")] = ip2String(mConfig->sys.ip.dns2); - obj[F("gateway")] = ip2String(mConfig->sys.ip.gateway); - } + char buf[16]; + ah::ip2Char(mConfig->sys.ip.ip, buf); obj[F("ip")] = String(buf); + ah::ip2Char(mConfig->sys.ip.mask, buf); obj[F("mask")] = String(buf); + ah::ip2Char(mConfig->sys.ip.dns1, buf); obj[F("dns1")] = String(buf); + ah::ip2Char(mConfig->sys.ip.dns2, buf); obj[F("dns2")] = String(buf); + ah::ip2Char(mConfig->sys.ip.gateway, buf); obj[F("gateway")] = String(buf); } @@ -397,7 +396,7 @@ void webApi::getIndex(JsonObject obj) { JsonArray warn = obj.createNestedArray(F("warnings")); if(!mApp->mSys->Radio.isChipConnected()) warn.add(F("your NRF24 module can't be reached, check the wiring and pinout")); - if(!mApp->mqttIsConnected()) + if((!mApp->mqttIsConnected()) && (String(mConfig->mqtt.broker).length() > 0)) warn.add(F("MQTT is not connected")); JsonArray info = obj.createNestedArray(F("infos")); diff --git a/src/web/webApi.h b/src/web/webApi.h index de37d9ba2..8b539e058 100644 --- a/src/web/webApi.h +++ b/src/web/webApi.h @@ -63,12 +63,6 @@ class webApi { return (int)(value * 1000 + 0.5) / 1000.0; } - String ip2String(uint8_t ip[]) { - char str[16]; - snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - return String(str); - } - AsyncWebServer *mSrv; app *mApp;