From 56cfffbcd81d268ca62f6de26a8c11eca5d0381e Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 12 Nov 2022 15:24:12 +0100 Subject: [PATCH] fix mqtt #407 #398 #391 --- src/app.cpp | 2 +- src/defines.h | 2 +- src/web/mqtt.h | 29 ++++++++++++++--------------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 2080bb23..c032c178 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -43,7 +43,7 @@ void app::setup(uint32_t timeout) { #ifndef AP_ONLY setupMqtt(); if(mMqttActive) - mPayload.addListener(std::bind(&MqttType::payloadEventListener, mMqtt, std::placeholders::_1)); + mPayload.addListener(std::bind(&MqttType::payloadEventListener, &mMqtt, std::placeholders::_1)); #endif setupLed(); diff --git a/src/defines.h b/src/defines.h index 389e4144..0e76eb8a 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 34 +#define VERSION_PATCH 35 //------------------------------------- typedef struct { diff --git a/src/web/mqtt.h b/src/web/mqtt.h index 6b731ffe..cc3de390 100644 --- a/src/web/mqtt.h +++ b/src/web/mqtt.h @@ -65,8 +65,7 @@ class mqtt { reconnect(); mClient->loop(); - if(!mSendList.empty()) - sendIvData(); + sendIvData(); } } @@ -216,19 +215,19 @@ class mqtt { return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : stateClasses[deviceFieldAssignment[pos].stateClsId]; } - void sendIvData() { + void sendIvData(void) { + if(mSendList.empty()) + return; + isConnected(true); // really needed? See comment from HorstG-57 #176 char topic[32 + MAX_NAME_LENGTH], val[32]; float total[4]; bool sendTotal = false; bool totalIncomplete = false; - snprintf(val, 32, "%ld", millis() / 1000); + snprintf(val, 40, "%ld", millis() / 1000); sendMsg("uptime", val); - if(mSendList.empty()) - return; - while(!mSendList.empty()) { memset(total, 0, sizeof(float) * 4); for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { @@ -250,20 +249,20 @@ class mqtt { status = MQTT_STATUS_AVAIL_NOT_PROD; } snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available_text", iv->name); - snprintf(val, 32, "%s%s%s%s", - (MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "not yet " : "", + snprintf(val, 40, "%s%s%s%s", + (status == MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "not yet " : "", "available and ", - (MQTT_STATUS_AVAIL_NOT_PROD) ? "not " : "", - (MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "" : "producing" + (status == MQTT_STATUS_AVAIL_NOT_PROD) ? "not " : "", + (status == MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "" : "producing" ); sendMsg(topic, val); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->name); - snprintf(val, 32, "%d", status); + snprintf(val, 40, "%d", status); sendMsg(topic, val); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->name); - snprintf(val, 48, "%i", iv->getLastTs(rec) * 1000); + snprintf(val, 40, "%i", iv->getLastTs(rec) * 1000); sendMsg(topic, val); } @@ -271,7 +270,7 @@ class mqtt { if(iv->isAvailable(*mUtcTimestamp, rec)) { for (uint8_t i = 0; i < rec->length; i++) { snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]); - snprintf(val, 10, "%.3f", iv->getValue(i, rec)); + snprintf(val, 40, "%.3f", iv->getValue(i, rec)); sendMsg(topic, val); // calculate total values for RealTimeRunData_Debug @@ -320,7 +319,7 @@ class mqtt { break; } snprintf(topic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]); - snprintf(val, 10, "%.3f", total[i]); + snprintf(val, 40, "%.3f", total[i]); sendMsg(topic, val); } }