From 8ce24df3046112f790eb4db6157cc39ab3e5249d Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:25:17 +0200 Subject: [PATCH 01/12] use unique MQTT client ID. Without this (and multiple running deevices with same ID), they disconnect each other! --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index 430f9b18..beab679c 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -26,7 +26,9 @@ void ClassFlowMQTT::SetInitialParameter(void) topicUptime = ""; topicFreeMem = ""; - clientname = "watermeter"; + + clientname = "AIOTED-" + getMac(); + OldValue = ""; flowpostprocessing = NULL; user = ""; From f09067a142ddb760feb4b83fc510c8944c580544 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:34:15 +0200 Subject: [PATCH 02/12] renamed mainerrortopic with lwt, only send it once on connecting, minor fixes and cleanups --- .../jomjol_flowcontroll/ClassFlowMQTT.cpp | 97 ++++++++----------- .../jomjol_flowcontroll/ClassFlowMQTT.h | 2 +- 2 files changed, 39 insertions(+), 60 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index beab679c..e34d26f2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -22,7 +22,7 @@ void ClassFlowMQTT::SetInitialParameter(void) topicRate = ""; topicTimeStamp = ""; maintopic = ""; - mainerrortopic = ""; + lwt = ""; topicUptime = ""; topicFreeMem = ""; @@ -121,66 +121,51 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) { maintopic = zerlegt[1]; } + else { // Main topic not set, use the hostname + maintopic = hostname; + } } #ifdef __HIDE_PASSWORD - ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str()); #else - ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str()); #endif if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0)) { ESP_LOGD(TAG, "InitMQTTInit"); - mainerrortopic = maintopic + "/connection"; + lwt = maintopic + "/connection"; #ifdef __HIDE_PASSWORD - ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str()); #else - ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str()); #endif - if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive)) + if (!MQTTInit(uri, clientname, user, password, lwt, keepAlive)) { // Failed MQTTenable = false; return true; // We need to return true despite we failed, else it will retry 5x and then reboot! } } - // Try sending mainerrortopic. If it fails, re-run init - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + // Try sending LWT. If it fails, re-run init + if (!MQTTPublish(lwt, "connected", SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); - if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive)) { // Failed MQTTenable = false; return false; } + + // Try again sending LWT and quit if it fails + if (!MQTTPublish(lwt, "connected", SetRetainFlag)) + { // Failed + MQTTenable = false; + return false; + } } - // Try again and quit if it fails - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - MQTTenable = false; - return false; - } - - - - - /* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish connection status!"); - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - }*/ - - /* if(!MQTTPublish(_LWTContext, "", 1)) - { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish LWT!"); - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - }*/ - - MQTTenable = true; return true; } @@ -194,24 +179,6 @@ string ClassFlowMQTT::GetMQTTMainTopic() bool ClassFlowMQTT::doFlow(string zwtime) { - // Try sending mainerrortopic. If it fails, re-run init - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); - if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) - { // Failed - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - } - } - - // Try again and quit if it fails - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - } - std::string result; std::string resulterror = ""; std::string resultraw = ""; @@ -221,16 +188,28 @@ bool ClassFlowMQTT::doFlow(string zwtime) string zw = ""; string namenumber = ""; - // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - //{ // Failed, skip other topics - // return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - //} - zw = maintopic + "/" + "uptime"; char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - MQTTPublish(zw, uptimeStr, SetRetainFlag); + // Try sending uptime. If it fails, re-run init + if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) + { // Failed + LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } + + // Try again and quit if it fails + if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } + } + zw = maintopic + "/" + "freeMem"; char freeheapmem[11]; sprintf(freeheapmem, "%zu", esp_get_free_heap_size()); diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h index 3a990c7d..a2805a4b 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h @@ -17,7 +17,7 @@ protected: bool MQTTenable; int keepAlive; - std::string maintopic, mainerrortopic; + std::string maintopic, lwt; void SetInitialParameter(void); public: From 86c515bcb84f2f727faf5d5a2120a7451dad8efc Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:52:06 +0200 Subject: [PATCH 03/12] removed \r\n in logs --- .../jomjol_controlGPIO/server_GPIO.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/code/components/jomjol_controlGPIO/server_GPIO.cpp b/code/components/jomjol_controlGPIO/server_GPIO.cpp index e54dcaa2..c21adde5 100644 --- a/code/components/jomjol_controlGPIO/server_GPIO.cpp +++ b/code/components/jomjol_controlGPIO/server_GPIO.cpp @@ -110,7 +110,7 @@ void GpioPin::init() // if (_interruptType != GPIO_INTR_DISABLE) { // ohne GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X, wenn das genutzt wird, dann soll auch der Handler hier nicht initialisiert werden, da das dann über SmartLED erfolgt. if ((_interruptType != GPIO_INTR_DISABLE) && (_interruptType != GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)) { //hook isr handler for specific gpio pin - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::init add isr handler for GPIO %d\r\n", _gpio); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::init add isr handler for GPIO %d", _gpio); gpio_isr_handler_add(_gpio, gpio_isr_handler, (void*)&_gpio); } @@ -131,7 +131,7 @@ bool GpioPin::getValue(std::string* errorText) void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* errorText) { - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::setValue %d\r\n", value); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::setValue %d", value); if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM) && (_mode != GPIO_PIN_MODE_BUILT_IN_FLASH_LED)) { (*errorText) = "GPIO is not in output mode"; @@ -154,7 +154,7 @@ void GpioPin::publishState() { } bool GpioPin::handleMQTT(std::string, char* data, int data_len) { - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::handleMQTT data %.*s\r\n", data_len, data); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::handleMQTT data %.*s", data_len, data); std::string dataStr(data, data_len); dataStr = toLower(dataStr); @@ -321,7 +321,7 @@ bool GpioHandler::readConfig() if (mainTopicMQTT.length() > 0) { mainTopicMQTT = mainTopicMQTT + "/GPIO"; - ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n"); + ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found"); } bool registerISR = false; @@ -333,9 +333,9 @@ bool GpioHandler::readConfig() // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2)) // { // std::string gpioStr = pieces_match[1]; - ESP_LOGD(TAG_SERVERGPIO, "conf param %s\r\n", toUpper(zerlegt[0]).c_str()); + ESP_LOGD(TAG_SERVERGPIO, "conf param %s", toUpper(zerlegt[0]).c_str()); if (toUpper(zerlegt[0]) == "MAINTOPICMQTT") { -// ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n"); +// ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found"); // mainTopicMQTT = zerlegt[1]; } else if ((zerlegt[0].rfind("IO", 0) == 0) && (zerlegt.size() >= 6)) { @@ -420,7 +420,7 @@ bool GpioHandler::readConfig() void GpioHandler::clear() { - ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear\r\n"); + ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear"); if (gpioMap != NULL) { for(std::map::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) { @@ -541,7 +541,7 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req) void GpioHandler::flashLightEnable(bool value) { - ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s\r\n", value ? "true" : "false"); + ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s", value ? "true" : "false"); if (gpioMap != NULL) { for(std::map::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) @@ -552,9 +552,9 @@ void GpioHandler::flashLightEnable(bool value) it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str); if (resp_str == "") { - ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s\r\n", (int)it->first, (value ? "on" : "off")); + ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s", (int)it->first, (value ? "on" : "off")); } else { - ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s\r\n", (int)it->first, resp_str.c_str()); + ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s", (int)it->first, resp_str.c_str()); } } else { From 455962bd947ed881bff3ee12c0e73fa8b6f816e9 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:53:24 +0200 Subject: [PATCH 04/12] . --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index e34d26f2..df6283c2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -192,7 +192,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - // Try sending uptime. If it fails, re-run init + // Try sending ptime. If it fails, re-run init if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); From bc7a0e46b73748bccceec6fd9d11dfbbd560abac Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:54:28 +0200 Subject: [PATCH 05/12] . --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index df6283c2..e34d26f2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -192,7 +192,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - // Try sending ptime. If it fails, re-run init + // Try sending uptime. If it fails, re-run init if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); From 43b65d6b2e0b5fd0b2315a3f55ceb7b91697192f Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 11:12:56 +0200 Subject: [PATCH 06/12] updated log messages --- code/components/jomjol_mqtt/interface_mqtt.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/components/jomjol_mqtt/interface_mqtt.cpp b/code/components/jomjol_mqtt/interface_mqtt.cpp index 48a88f97..dc92269e 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.cpp +++ b/code/components/jomjol_mqtt/interface_mqtt.cpp @@ -11,7 +11,6 @@ static const char *TAG_INTERFACEMQTT = "interface_mqtt"; std::map>* connectFunktionMap = NULL; std::map>* subscribeFunktionMap = NULL; -bool debugdetail = true; // #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883" @@ -27,11 +26,11 @@ bool MQTTPublish(std::string _key, std::string _content, int retained_flag){ std::string zw; msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag); if (msg_id < 0) { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Failed to publish '" + _key + "'!"); + LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Failed to publish topic '" + _key + "'!"); return false; } - zw = "MQTT - sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; - if (debugdetail) LogFile.WriteToFile(ESP_LOG_INFO, zw); + zw = "MQTT - Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")"; + LogFile.WriteToFile(ESP_LOG_DEBUG, zw); return true; } @@ -109,7 +108,7 @@ bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st .keepalive = _keepalive }; - LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init"); + LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init (client ID: " + _clientid + ")"); if (_user.length() && _password.length()){ mqtt_cfg.username = _user.c_str(); @@ -139,7 +138,7 @@ bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st /* if(!MQTTPublish(_LWTContext, "", 1)) { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish LWT!"); + LogFile.WriteToFile("MQTT - Could not publish LWT!"); return false; }*/ } From dabe66819da784db95dc40f4af54cbf65e75a94e Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:25:17 +0200 Subject: [PATCH 07/12] use unique MQTT client ID. Without this (and multiple running deevices with same ID), they disconnect each other! --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index 430f9b18..beab679c 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -26,7 +26,9 @@ void ClassFlowMQTT::SetInitialParameter(void) topicUptime = ""; topicFreeMem = ""; - clientname = "watermeter"; + + clientname = "AIOTED-" + getMac(); + OldValue = ""; flowpostprocessing = NULL; user = ""; From 25ae9a045f26eb709d1b948293da3abe669586ca Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:34:15 +0200 Subject: [PATCH 08/12] renamed mainerrortopic with lwt, only send it once on connecting, minor fixes and cleanups --- .../jomjol_flowcontroll/ClassFlowMQTT.cpp | 97 ++++++++----------- .../jomjol_flowcontroll/ClassFlowMQTT.h | 2 +- 2 files changed, 39 insertions(+), 60 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index beab679c..e34d26f2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -22,7 +22,7 @@ void ClassFlowMQTT::SetInitialParameter(void) topicRate = ""; topicTimeStamp = ""; maintopic = ""; - mainerrortopic = ""; + lwt = ""; topicUptime = ""; topicFreeMem = ""; @@ -121,66 +121,51 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) { maintopic = zerlegt[1]; } + else { // Main topic not set, use the hostname + maintopic = hostname; + } } #ifdef __HIDE_PASSWORD - ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str()); #else - ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str()); #endif if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0)) { ESP_LOGD(TAG, "InitMQTTInit"); - mainerrortopic = maintopic + "/connection"; + lwt = maintopic + "/connection"; #ifdef __HIDE_PASSWORD - ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str()); #else - ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); + ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str()); #endif - if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive)) + if (!MQTTInit(uri, clientname, user, password, lwt, keepAlive)) { // Failed MQTTenable = false; return true; // We need to return true despite we failed, else it will retry 5x and then reboot! } } - // Try sending mainerrortopic. If it fails, re-run init - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + // Try sending LWT. If it fails, re-run init + if (!MQTTPublish(lwt, "connected", SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); - if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive)) { // Failed MQTTenable = false; return false; } + + // Try again sending LWT and quit if it fails + if (!MQTTPublish(lwt, "connected", SetRetainFlag)) + { // Failed + MQTTenable = false; + return false; + } } - // Try again and quit if it fails - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - MQTTenable = false; - return false; - } - - - - - /* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish connection status!"); - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - }*/ - - /* if(!MQTTPublish(_LWTContext, "", 1)) - { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish LWT!"); - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - }*/ - - MQTTenable = true; return true; } @@ -194,24 +179,6 @@ string ClassFlowMQTT::GetMQTTMainTopic() bool ClassFlowMQTT::doFlow(string zwtime) { - // Try sending mainerrortopic. If it fails, re-run init - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); - if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) - { // Failed - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - } - } - - // Try again and quit if it fails - if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - { // Failed - MQTTenable = false; - return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - } - std::string result; std::string resulterror = ""; std::string resultraw = ""; @@ -221,16 +188,28 @@ bool ClassFlowMQTT::doFlow(string zwtime) string zw = ""; string namenumber = ""; - // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) - //{ // Failed, skip other topics - // return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - //} - zw = maintopic + "/" + "uptime"; char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - MQTTPublish(zw, uptimeStr, SetRetainFlag); + // Try sending uptime. If it fails, re-run init + if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) + { // Failed + LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } + + // Try again and quit if it fails + if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } + } + zw = maintopic + "/" + "freeMem"; char freeheapmem[11]; sprintf(freeheapmem, "%zu", esp_get_free_heap_size()); diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h index 3a990c7d..a2805a4b 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h @@ -17,7 +17,7 @@ protected: bool MQTTenable; int keepAlive; - std::string maintopic, mainerrortopic; + std::string maintopic, lwt; void SetInitialParameter(void); public: From 9d73c5475e8e8fd757e3fc7d6c275061bcddfa7b Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:52:06 +0200 Subject: [PATCH 09/12] removed \r\n in logs --- .../jomjol_controlGPIO/server_GPIO.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/code/components/jomjol_controlGPIO/server_GPIO.cpp b/code/components/jomjol_controlGPIO/server_GPIO.cpp index e54dcaa2..c21adde5 100644 --- a/code/components/jomjol_controlGPIO/server_GPIO.cpp +++ b/code/components/jomjol_controlGPIO/server_GPIO.cpp @@ -110,7 +110,7 @@ void GpioPin::init() // if (_interruptType != GPIO_INTR_DISABLE) { // ohne GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X, wenn das genutzt wird, dann soll auch der Handler hier nicht initialisiert werden, da das dann über SmartLED erfolgt. if ((_interruptType != GPIO_INTR_DISABLE) && (_interruptType != GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)) { //hook isr handler for specific gpio pin - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::init add isr handler for GPIO %d\r\n", _gpio); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::init add isr handler for GPIO %d", _gpio); gpio_isr_handler_add(_gpio, gpio_isr_handler, (void*)&_gpio); } @@ -131,7 +131,7 @@ bool GpioPin::getValue(std::string* errorText) void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* errorText) { - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::setValue %d\r\n", value); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::setValue %d", value); if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM) && (_mode != GPIO_PIN_MODE_BUILT_IN_FLASH_LED)) { (*errorText) = "GPIO is not in output mode"; @@ -154,7 +154,7 @@ void GpioPin::publishState() { } bool GpioPin::handleMQTT(std::string, char* data, int data_len) { - ESP_LOGD(TAG_SERVERGPIO, "GpioPin::handleMQTT data %.*s\r\n", data_len, data); + ESP_LOGD(TAG_SERVERGPIO, "GpioPin::handleMQTT data %.*s", data_len, data); std::string dataStr(data, data_len); dataStr = toLower(dataStr); @@ -321,7 +321,7 @@ bool GpioHandler::readConfig() if (mainTopicMQTT.length() > 0) { mainTopicMQTT = mainTopicMQTT + "/GPIO"; - ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n"); + ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found"); } bool registerISR = false; @@ -333,9 +333,9 @@ bool GpioHandler::readConfig() // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2)) // { // std::string gpioStr = pieces_match[1]; - ESP_LOGD(TAG_SERVERGPIO, "conf param %s\r\n", toUpper(zerlegt[0]).c_str()); + ESP_LOGD(TAG_SERVERGPIO, "conf param %s", toUpper(zerlegt[0]).c_str()); if (toUpper(zerlegt[0]) == "MAINTOPICMQTT") { -// ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n"); +// ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found"); // mainTopicMQTT = zerlegt[1]; } else if ((zerlegt[0].rfind("IO", 0) == 0) && (zerlegt.size() >= 6)) { @@ -420,7 +420,7 @@ bool GpioHandler::readConfig() void GpioHandler::clear() { - ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear\r\n"); + ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear"); if (gpioMap != NULL) { for(std::map::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) { @@ -541,7 +541,7 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req) void GpioHandler::flashLightEnable(bool value) { - ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s\r\n", value ? "true" : "false"); + ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s", value ? "true" : "false"); if (gpioMap != NULL) { for(std::map::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) @@ -552,9 +552,9 @@ void GpioHandler::flashLightEnable(bool value) it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str); if (resp_str == "") { - ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s\r\n", (int)it->first, (value ? "on" : "off")); + ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s", (int)it->first, (value ? "on" : "off")); } else { - ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s\r\n", (int)it->first, resp_str.c_str()); + ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s", (int)it->first, resp_str.c_str()); } } else { From 99e03dde06f0f24ec453482a99895565bc251991 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:53:24 +0200 Subject: [PATCH 10/12] . --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index e34d26f2..df6283c2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -192,7 +192,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - // Try sending uptime. If it fails, re-run init + // Try sending ptime. If it fails, re-run init if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); From 41abc19a97aee2d59fcb230f2d3cc109eae60db3 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 10:54:28 +0200 Subject: [PATCH 11/12] . --- code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index df6283c2..e34d26f2 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -192,7 +192,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) char uptimeStr[11]; sprintf(uptimeStr, "%ld", (long)getUpTime()); - // Try sending ptime. If it fails, re-run init + // Try sending uptime. If it fails, re-run init if (!MQTTPublish(zw, uptimeStr, SetRetainFlag)) { // Failed LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); From 1aa23aa14a0d96620722f1b7b99e8bcb8b78bdf0 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Wed, 26 Oct 2022 11:12:56 +0200 Subject: [PATCH 12/12] updated log messages --- code/components/jomjol_mqtt/interface_mqtt.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/components/jomjol_mqtt/interface_mqtt.cpp b/code/components/jomjol_mqtt/interface_mqtt.cpp index 48a88f97..dc92269e 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.cpp +++ b/code/components/jomjol_mqtt/interface_mqtt.cpp @@ -11,7 +11,6 @@ static const char *TAG_INTERFACEMQTT = "interface_mqtt"; std::map>* connectFunktionMap = NULL; std::map>* subscribeFunktionMap = NULL; -bool debugdetail = true; // #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883" @@ -27,11 +26,11 @@ bool MQTTPublish(std::string _key, std::string _content, int retained_flag){ std::string zw; msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag); if (msg_id < 0) { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Failed to publish '" + _key + "'!"); + LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Failed to publish topic '" + _key + "'!"); return false; } - zw = "MQTT - sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; - if (debugdetail) LogFile.WriteToFile(ESP_LOG_INFO, zw); + zw = "MQTT - Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")"; + LogFile.WriteToFile(ESP_LOG_DEBUG, zw); return true; } @@ -109,7 +108,7 @@ bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st .keepalive = _keepalive }; - LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init"); + LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init (client ID: " + _clientid + ")"); if (_user.length() && _password.length()){ mqtt_cfg.username = _user.c_str(); @@ -139,7 +138,7 @@ bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st /* if(!MQTTPublish(_LWTContext, "", 1)) { - LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not publish LWT!"); + LogFile.WriteToFile("MQTT - Could not publish LWT!"); return false; }*/ }