From 58d992c7713d761ceacf54656d3fd477b9595162 Mon Sep 17 00:00:00 2001 From: argafal Date: Tue, 14 Mar 2023 18:28:05 +0100 Subject: [PATCH 1/3] Two improvements to heap fragmentation See https://github.com/lumapu/ahoy/issues/768. --- src/publisher/pubMqtt.h | 22 +++++++++++++--------- src/publisher/pubMqttDefs.h | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 2b2b298e..3acbef1b 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -111,6 +111,7 @@ class PubMqtt { publish(subtopics[MQTT_UPTIME], val); publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str()); publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str()); + publish(subtopics[MQTT_HEAP_FRAG], String(ESP.getHeapFragmentation()).c_str()); } bool tickerSun(uint32_t sunrise, uint32_t sunset, uint32_t offs, bool disNightCom) { @@ -162,14 +163,16 @@ class PubMqtt { if(!mClient.connected()) return; - String topic = ""; - if(addTopic) - topic = String(mCfgMqtt->topic) + "/"; - topic += String(subTopic); + memset(mTopic, 0, MQTT_TOPIC_LEN+5); + if(addTopic){ + snprintf(mTopic, MQTT_TOPIC_LEN+5, "%s/%s", mCfgMqtt->topic, subTopic); + } else { + snprintf(mTopic, MQTT_TOPIC_LEN+5, "%s", subTopic); + } do { - if(0 != mClient.publish(topic.c_str(), QOS_0, retained, payload)) - break; + if(0 != mClient.publish(mTopic, QOS_0, retained, payload)) + break; if(!mClient.connected()) break; #if defined(ESP8266) @@ -207,7 +210,7 @@ class PubMqtt { DPRINTLN(DBG_VERBOSE, F("sendMqttDiscoveryConfig")); char topic[64], name[32], uniq_id[32]; - DynamicJsonDocument doc(256); + StaticJsonDocument<256> doc; uint8_t fldTotal[4] = {FLD_PAC, FLD_YT, FLD_YD, FLD_PDC}; const char* unitTotal[4] = {"W", "kWh", "Wh", "W"}; @@ -264,7 +267,7 @@ class PubMqtt { stateCls = getFieldStateClass(fldTotal[i]); } - DynamicJsonDocument doc2(512); + StaticJsonDocument<512> doc2; doc2[F("name")] = name; doc2[F("stat_t")] = String(mCfgMqtt->topic) + "/" + ((!total) ? String(iv->config->name) : "total" ) + String(topic); doc2[F("unit_of_meas")] = ((!total) ? (iv->getUnit(i,rec)) : (unitTotal[i])); @@ -356,7 +359,7 @@ class PubMqtt { if(NULL == mSubscriptionCb) return; - DynamicJsonDocument json(128); + StaticJsonDocument<128> json; JsonObject root = json.to(); bool limitAbs = false; @@ -636,6 +639,7 @@ class PubMqtt { char mLwtTopic[MQTT_TOPIC_LEN+5]; const char *mDevName, *mVersion; char mClientId[26]; // number of chars is limited to 23 up to v3.1 of MQTT + char mTopic[MQTT_TOPIC_LEN+5]; }; #endif /*__PUB_MQTT_H__*/ diff --git a/src/publisher/pubMqttDefs.h b/src/publisher/pubMqttDefs.h index 9633e2d4..088023b7 100644 --- a/src/publisher/pubMqttDefs.h +++ b/src/publisher/pubMqttDefs.h @@ -41,6 +41,7 @@ enum { MQTT_UPTIME = 0, MQTT_RSSI, MQTT_FREE_HEAP, + MQTT_HEAP_FRAG, MQTT_SUNRISE, MQTT_SUNSET, MQTT_COMM_START, @@ -64,6 +65,7 @@ const char* const subtopics[] PROGMEM = { "uptime", "wifi_rssi", "free_heap", + "heap_frag", "sunrise", "sunset", "comm_start", From 73032663c421c41d3b6355fc308820b5296974c8 Mon Sep 17 00:00:00 2001 From: argafal Date: Tue, 14 Mar 2023 18:31:03 +0100 Subject: [PATCH 2/3] Resolve conflict, accept upstream --- src/publisher/pubMqtt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 3acbef1b..633ec912 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -638,7 +638,7 @@ class PubMqtt { // last will topic and payload must be available trough lifetime of 'espMqttClient' char mLwtTopic[MQTT_TOPIC_LEN+5]; const char *mDevName, *mVersion; - char mClientId[26]; // number of chars is limited to 23 up to v3.1 of MQTT + char mClientId[24]; // number of chars is limited to 23 up to v3.1 of MQTT char mTopic[MQTT_TOPIC_LEN+5]; }; From 3b661b050fcf738b86b6ab5b008401356eee9546 Mon Sep 17 00:00:00 2001 From: argafal Date: Wed, 15 Mar 2023 10:12:49 +0100 Subject: [PATCH 3/3] Increase the MQTT topic buffer size See https://github.com/lumapu/ahoy/issues/768#issuecomment-1468692140 --- src/publisher/pubMqtt.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 2fdb77ba..d66f5899 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -163,11 +163,11 @@ class PubMqtt { if(!mClient.connected()) return; - memset(mTopic, 0, MQTT_TOPIC_LEN+5); + memset(mTopic, 0, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1); if(addTopic){ - snprintf(mTopic, MQTT_TOPIC_LEN+5, "%s/%s", mCfgMqtt->topic, subTopic); + snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s/%s", mCfgMqtt->topic, subTopic); } else { - snprintf(mTopic, MQTT_TOPIC_LEN+5, "%s", subTopic); + snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s", subTopic); } do { @@ -639,7 +639,8 @@ class PubMqtt { char mLwtTopic[MQTT_TOPIC_LEN+5]; const char *mDevName, *mVersion; char mClientId[24]; // number of chars is limited to 23 up to v3.1 of MQTT - char mTopic[MQTT_TOPIC_LEN+5]; + // global buffer for mqtt topic. Used when publishing mqtt messages. + char mTopic[MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1]; }; #endif /*__PUB_MQTT_H__*/