Skip to content

Commit

Permalink
reduced heap fragmentation (removed strtok completely) #644, #645, #…
Browse files Browse the repository at this point in the history
…682

added part of mac address to MQTT client ID to seperate multiple ESPs in same network
added dictionary for MQTT to reduce heap-fragmentation
removed `last Alarm` from Live view, because it showed always the same alarm - will change in future
  • Loading branch information
lumapu committed Feb 15, 2023
1 parent fe54599 commit a0879cf
Show file tree
Hide file tree
Showing 17 changed files with 394 additions and 149 deletions.
6 changes: 6 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

(starting from release version `0.5.66`)

## 0.5.89
* reduced heap fragmentation (removed `strtok` completely) #644, #645, #682
* added part of mac address to MQTT client ID to seperate multiple ESPs in same network
* added dictionary for MQTT to reduce heap-fragmentation
* removed `last Alarm` from Live view, because it showed always the same alarm - will change in future

## 0.5.88
* MQTT Yield Day zero, next try to fix #671, thx @beegee3
* added Solenso inverter to supported devices
Expand Down
45 changes: 35 additions & 10 deletions src/app.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// 2023 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

Expand All @@ -21,9 +21,19 @@ void app::setup() {

resetSystem();

/*DBGPRINTLN("--- start");
DBGPRINTLN(String(ESP.getFreeHeap()));
DBGPRINTLN(String(ESP.getHeapFragmentation()));
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/


mSettings.setup();
mSettings.getPtr(mConfig);
DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false")));
DPRINT(DBG_INFO, F("Settings valid: "));
if(mSettings.getValid())
DBGPRINTLN(F("true"));
else
DBGPRINTLN(F("false"));

mSys.enableDebug();
mSys.setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
Expand All @@ -47,6 +57,11 @@ void app::setup() {
mMiPayload.setup(this, &mSys, &mStat, mConfig->nrf.maxRetransPerPyld, &mTimestamp);
mMiPayload.enableSerialDebug(mConfig->serial.debug);

/*DBGPRINTLN("--- after payload");
DBGPRINTLN(String(ESP.getFreeHeap()));
DBGPRINTLN(String(ESP.getHeapFragmentation()));
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/

if(!mSys.Radio.isChipConnected())
DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));

Expand All @@ -73,6 +88,12 @@ void app::setup() {
mPubSerial.setup(mConfig, &mSys, &mTimestamp);

regularTickers();


/*DBGPRINTLN("--- end setup");
DBGPRINTLN(String(ESP.getFreeHeap()));
DBGPRINTLN(String(ESP.getHeapFragmentation()));
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/
}

//-----------------------------------------------------------------------------
Expand All @@ -89,7 +110,11 @@ void app::loopStandard(void) {
packet_t *p = &mSys.Radio.mBufCtrl.front();

if (mConfig->serial.debug) {
DPRINT(DBG_INFO, "RX " + String(p->len) + "B Ch" + String(p->ch) + " | ");
DPRINT(DBG_INFO, F("RX "));
DBGPRINT(String(p->len));
DBGPRINT(F("B Ch"));
DBGPRINT(String(p->ch));
DBGPRINT(F(" | "));
mSys.Radio.dumpBuf(p->packet, p->len);
}
mStat.frmCnt++;
Expand Down Expand Up @@ -158,13 +183,13 @@ void app::tickNtpUpdate(void) {
bool isOK = mWifi.getNtpTime();
if (isOK || mTimestamp != 0) {
if (mMqttReconnect && mMqttEnabled) {
mMqtt.connect();
mMqtt.tickerMinute();
everySec(std::bind(&PubMqttType::tickerSecond, &mMqtt), "mqttS");
everyMin(std::bind(&PubMqttType::tickerMinute, &mMqtt), "mqttM");
}

// only install schedulers once even if NTP wasn't successful in first loop
if(mMqttReconnect) { // @TODO: mMqttReconnect is wrong name here
if(mMqttReconnect) { // @TODO: mMqttReconnect is variable which scope has changed
if(mConfig->inst.rstValsNotAvail)
everyMin(std::bind(&app::tickMinute, this), "tMin");
if(mConfig->inst.rstYieldMidNight) {
Expand Down Expand Up @@ -282,8 +307,6 @@ void app::tickMidnight(void) {
uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight
onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2");

DPRINTLN(DBG_INFO, "tickMidnight " + String(nxtTrig));

Inverter<> *iv;
// set values to zero, except yield total
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
Expand All @@ -302,13 +325,15 @@ void app::tickMidnight(void) {
//-----------------------------------------------------------------------------
void app::tickSend(void) {
if(!mSys.Radio.isChipConnected()) {
DPRINTLN(DBG_WARN, "NRF24 not connected!");
DPRINTLN(DBG_WARN, F("NRF24 not connected!"));
return;
}
if (mIVCommunicationOn) {
if (!mSys.Radio.mBufCtrl.empty()) {
if (mConfig->serial.debug)
DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys.Radio.mBufCtrl.size()));
if (mConfig->serial.debug) {
DPRINT(DBG_DEBUG, F("recbuf not empty! #"));
DBGPRINTLN(String(mSys.Radio.mBufCtrl.size()));
}
}

int8_t maxLoop = MAX_NUM_INVERTERS;
Expand Down
5 changes: 3 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// 2023 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -182,7 +182,8 @@ class app : public IApp, public ah::Scheduler {
}

void setTimestamp(uint32_t newTime) {
DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime));
DPRINT(DBG_DEBUG, F("setTimestamp: "));
DBGPRINTLN(String(newTime));
if(0 == newTime)
mWifi.getNtpTime();
else
Expand Down
6 changes: 2 additions & 4 deletions src/config/settings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// 2023 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -212,7 +212,6 @@ class settings {
}

bool readSettings(const char* path) {
bool success = false;
loadDefaults();
File fp = LittleFS.open(path, "r");
if(!fp)
Expand All @@ -233,15 +232,14 @@ class settings {
jsonLed(root[F("led")]);
jsonPlugin(root[F("plugin")]);
jsonInst(root[F("inst")]);
success = true;
}
else {
Serial.println(F("failed to parse json, using default config"));
}

fp.close();
}
return success;
return mCfg.valid;
}

bool saveSettings(void) {
Expand Down
4 changes: 2 additions & 2 deletions src/defines.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// 2023 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

Expand All @@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 88
#define VERSION_PATCH 89

//-------------------------------------
typedef struct {
Expand Down
17 changes: 11 additions & 6 deletions src/hm/hmInverter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// 2023 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -119,7 +119,7 @@ class Inverter {
record_t<REC_TYP> recordInfo; // structure for info values
record_t<REC_TYP> recordConfig; // structure for system config values
record_t<REC_TYP> recordAlarm; // structure for alarm values
String lastAlarmMsg;
//String lastAlarmMsg;
bool initialized; // needed to check if the inverter was correctly added (ESP32 specific - union types are never null)
bool isConnected; // shows if inverter was successfully identified (fw version and hardware info)

Expand All @@ -131,7 +131,7 @@ class Inverter {
mDevControlRequest = false;
devControlCmd = InitDataState;
initialized = false;
lastAlarmMsg = "nothing";
//lastAlarmMsg = "nothing";
alarmMesIndex = 0;
isConnected = false;
}
Expand Down Expand Up @@ -294,9 +294,9 @@ class Inverter {
}
else if (rec->assign == AlarmDataAssignment) {
DPRINTLN(DBG_DEBUG, "add alarm");
if (getPosByChFld(0, FLD_LAST_ALARM_CODE, rec) == pos){
lastAlarmMsg = getAlarmStr(rec->record[pos]);
}
//if (getPosByChFld(0, FLD_LAST_ALARM_CODE, rec) == pos){
// lastAlarmMsg = getAlarmStr(rec->record[pos]);
//}
}
else
DPRINTLN(DBG_WARN, F("add with unknown assginment"));
Expand All @@ -305,6 +305,11 @@ class Inverter {
DPRINTLN(DBG_ERROR, F("addValue: assignment not found with cmd 0x"));
}

/*inline REC_TYP getPowerLimit(void) {
record_t<> *rec = getRecordStruct(SystemConfigPara);
return getChannelFieldValue(CH0, FLD_ACT_ACTIVE_PWR_LIMIT, rec);
}*/

bool setValue(uint8_t pos, record_t<> *rec, REC_TYP val) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:setValue"));
if(NULL == rec)
Expand Down
Loading

0 comments on commit a0879cf

Please sign in to comment.