Browse Source

0.5.109

* reduced heap fragmentation by optimizing MqTT #768
pull/820/head
lumapu 2 years ago
parent
commit
4a44041e7d
  1. 3
      src/CHANGES.md
  2. 2
      src/defines.h
  3. 66
      src/publisher/pubMqtt.h

3
src/CHANGES.md

@ -2,6 +2,9 @@
(starting from release version `0.5.66`) (starting from release version `0.5.66`)
## 0.5.109
* reduced heap fragmentation by optimizing MqTT #768
## 0.5.108 ## 0.5.108
* merge: PR SPI pins configureable (ESP32) #807, #806 * merge: PR SPI pins configureable (ESP32) #807, #806
* merge: PR MI serial outputs #809 * merge: PR MI serial outputs #809

2
src/defines.h

@ -13,7 +13,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 5 #define VERSION_MINOR 5
#define VERSION_PATCH 108 #define VERSION_PATCH 109
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {

66
src/publisher/pubMqtt.h

@ -118,9 +118,8 @@ class PubMqtt {
} }
void tickerMinute() { void tickerMinute() {
char val[12]; snprintf(mVal, 40, "%ld", millis() / 1000);
snprintf(val, 12, "%ld", millis() / 1000); publish(subtopics[MQTT_UPTIME], mVal);
publish(subtopics[MQTT_UPTIME], val);
publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str()); publish(subtopics[MQTT_RSSI], String(WiFi.RSSI()).c_str());
publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str()); publish(subtopics[MQTT_FREE_HEAP], String(ESP.getFreeHeap()).c_str());
#ifndef ESP32 #ifndef ESP32
@ -152,12 +151,10 @@ class PubMqtt {
} }
void tickerMidnight() { void tickerMidnight() {
char topic[7 + MQTT_TOPIC_LEN], val[4];
// set Total YieldDay to zero // set Total YieldDay to zero
snprintf(topic, 32 + MAX_NAME_LENGTH, "total/%s", fields[FLD_YD]); snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "total/%s", fields[FLD_YD]);
snprintf(val, 2, "0"); snprintf(mVal, 2, "0");
publish(topic, val, true); publish(mSubTopic, mVal, true);
} }
void payloadEventListener(uint8_t cmd) { void payloadEventListener(uint8_t cmd) {
@ -177,7 +174,6 @@ class PubMqtt {
if(!mClient.connected()) if(!mClient.connected())
return; return;
memset(mTopic, 0, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1);
if(addTopic){ if(addTopic){
snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s/%s", mCfgMqtt->topic, subTopic); snprintf(mTopic, MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1, "%s/%s", mCfgMqtt->topic, subTopic);
} else { } else {
@ -230,10 +226,8 @@ class PubMqtt {
void setPowerLimitAck(Inverter<> *iv) { void setPowerLimitAck(Inverter<> *iv) {
if (NULL != iv) { if (NULL != iv) {
char topic[7 + MQTT_TOPIC_LEN]; snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "%s/%s", iv->config->name, subtopics[MQTT_ACK_PWR_LMT]);
publish(mSubTopic, "true", true);
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/%s", iv->config->name, subtopics[MQTT_ACK_PWR_LMT]);
publish(topic, "true", true);
} }
} }
@ -247,12 +241,11 @@ class PubMqtt {
tickerMinute(); tickerMinute();
publish(mLwtTopic, mqttStr[MQTT_STR_LWT_CONN], true, false); publish(mLwtTopic, mqttStr[MQTT_STR_LWT_CONN], true, false);
char sub[20];
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) {
snprintf(sub, 20, "ctrl/limit/%d", i); snprintf(mVal, 20, "ctrl/limit/%d", i);
subscribe(sub); subscribe(mVal);
snprintf(sub, 20, "ctrl/restart/%d", i); snprintf(mVal, 20, "ctrl/restart/%d", i);
subscribe(sub); subscribe(mVal);
} }
subscribe(subscr[MQTT_SUBS_SET_TIME]); subscribe(subscr[MQTT_SUBS_SET_TIME]);
} }
@ -356,7 +349,7 @@ class PubMqtt {
bool total = (mDiscovery.lastIvId == MAX_NUM_INVERTERS); bool total = (mDiscovery.lastIvId == MAX_NUM_INVERTERS);
Inverter<> *iv = mSys->getInverterByPos(mDiscovery.lastIvId); Inverter<> *iv = mSys->getInverterByPos(mDiscovery.lastIvId);
record_t<> *rec; record_t<> *rec = NULL;
if (NULL != iv) { if (NULL != iv) {
rec = iv->getRecordStruct(RealTimeRunData_Debug); rec = iv->getRecordStruct(RealTimeRunData_Debug);
if(0 == mDiscovery.sub) if(0 == mDiscovery.sub)
@ -437,7 +430,6 @@ class PubMqtt {
void checkDiscoveryEnd(void) { void checkDiscoveryEnd(void) {
if(++mDiscovery.lastIvId == MAX_NUM_INVERTERS) { if(++mDiscovery.lastIvId == MAX_NUM_INVERTERS) {
// check if only one inverter was found, then don't create 'total' sensor // check if only one inverter was found, then don't create 'total' sensor
DPRINTLN(DBG_INFO, "found: " + String(mDiscovery.foundIvCnt));
if(mDiscovery.foundIvCnt == 1) if(mDiscovery.foundIvCnt == 1)
mDiscovery.running = false; mDiscovery.running = false;
} else if(mDiscovery.lastIvId == (MAX_NUM_INVERTERS + 1)) } else if(mDiscovery.lastIvId == (MAX_NUM_INVERTERS + 1))
@ -467,7 +459,6 @@ class PubMqtt {
bool allAvail = true; // shows if all enabled inverters are available bool allAvail = true; // shows if all enabled inverters are available
bool anyAvail = false; // shows if at least one enabled inverter is available bool anyAvail = false; // shows if at least one enabled inverter is available
bool changed = false; bool changed = false;
char topic[7 + MQTT_TOPIC_LEN], val[40];
Inverter<> *iv; Inverter<> *iv;
record_t<> *rec; record_t<> *rec;
@ -497,19 +488,19 @@ class PubMqtt {
mLastIvState[id] = status; mLastIvState[id] = status;
changed = true; changed = true;
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name); snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name);
snprintf(val, 40, "%d", status); snprintf(mVal, 40, "%d", status);
publish(topic, val, true); publish(mSubTopic, mVal, true);
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->config->name); snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->config->name);
snprintf(val, 40, "%d", iv->getLastTs(rec)); snprintf(mVal, 40, "%d", iv->getLastTs(rec));
publish(topic, val, true); publish(mSubTopic, mVal, true);
} }
} }
if(changed) { if(changed) {
snprintf(val, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((anyAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE))); snprintf(mVal, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((anyAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE)));
publish("status", val, true); publish("status", mVal, true);
} }
return anyAvail; return anyAvail;
@ -529,7 +520,6 @@ class PubMqtt {
} }
void sendData(Inverter<> *iv, uint8_t curInfoCmd) { void sendData(Inverter<> *iv, uint8_t curInfoCmd) {
char topic[7 + MQTT_TOPIC_LEN], val[40];
record_t<> *rec = iv->getRecordStruct(curInfoCmd); record_t<> *rec = iv->getRecordStruct(curInfoCmd);
if (iv->getLastTs(rec) > 0) { if (iv->getLastTs(rec) > 0) {
@ -546,9 +536,9 @@ class PubMqtt {
} }
} }
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->config->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]); snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->config->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]);
snprintf(val, 40, "%g", ah::round3(iv->getValue(i, rec))); snprintf(mVal, 40, "%g", ah::round3(iv->getValue(i, rec)));
publish(topic, val, retained); publish(mSubTopic, mVal, retained);
yield(); yield();
} }
@ -563,7 +553,6 @@ class PubMqtt {
if(mSendList.empty()) if(mSendList.empty())
return; return;
char topic[7 + MQTT_TOPIC_LEN], val[40];
float total[4]; float total[4];
bool RTRDataHasBeenSent = false; bool RTRDataHasBeenSent = false;
@ -635,9 +624,9 @@ class PubMqtt {
retained = false; retained = false;
break; break;
} }
snprintf(topic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]); snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]);
snprintf(val, 40, "%g", ah::round3(total[i])); snprintf(mVal, 40, "%g", ah::round3(total[i]));
publish(topic, val, retained); publish(mSubTopic, mVal, retained);
} }
RTRDataHasBeenSent = true; RTRDataHasBeenSent = true;
yield(); yield();
@ -672,7 +661,8 @@ class PubMqtt {
char mClientId[24]; // 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
// global buffer for mqtt topic. Used when publishing mqtt messages. // global buffer for mqtt topic. Used when publishing mqtt messages.
char mTopic[MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1]; char mTopic[MQTT_TOPIC_LEN + 32 + MAX_NAME_LENGTH + 1];
char mSubTopic[32 + MAX_NAME_LENGTH + 1];
char mVal[40];
discovery_t mDiscovery; discovery_t mDiscovery;
}; };

Loading…
Cancel
Save