diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index a40bcc3d..dd7b4425 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -14,7 +14,6 @@ app::app() : Main() { mSerialTicker = 0xffff; mSerialInterval = 0; mMqttActive = false; - mMqttNewDataAvail = false; mTicker = 0; mRxTicker = 0; @@ -25,8 +24,9 @@ app::app() : Main() { mSerialDebug = false; memset(mPayload, 0, (MAX_NUM_INVERTERS * sizeof(invPayload_t))); - mRxFailed = 0; - mRxSuccess = 0; + mRxFailed = 0; + mRxSuccess = 0; + mLastPacketId = 0x00; mSys = new HmSystemType(); } @@ -162,8 +162,11 @@ void app::loop(void) { } if((*pid & 0x80) == 0x80) { - if((*pid & 0x7f) > mPayload[iv->id].maxPackId) + if((*pid & 0x7f) > mPayload[iv->id].maxPackId) { mPayload[iv->id].maxPackId = (*pid & 0x7f); + if(*pid > 0x81) + mLastPacketId = *pid; + } } } } @@ -182,14 +185,14 @@ void app::loop(void) { mMqtt.loop(); if(checkTicker(&mTicker, 1000)) { - if(mMqttNewDataAvail) { - if(++mMqttTicker >= mMqttInterval) { - mMqttTicker = 0; - mMqtt.isConnected(true); - char topic[30], val[10]; - for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { - Inverter<> *iv = mSys->getInverterByPos(id); - if(NULL != iv) { + if(++mMqttTicker >= mMqttInterval) { + mMqttTicker = 0; + mMqtt.isConnected(true); + char topic[30], val[10]; + for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { + Inverter<> *iv = mSys->getInverterByPos(id); + if(NULL != iv) { + if(iv->isAvailable(mTimestamp)) { for(uint8_t i = 0; i < iv->listLen; i++) { snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, fields[iv->assign[i].fieldId]); snprintf(val, 10, "%.3f", iv->getValue(i)); @@ -199,10 +202,9 @@ void app::loop(void) { } } } - snprintf(val, 10, "%d", ESP.getFreeHeap()); - mMqtt.sendMsg("free_heap", val); - mMqttNewDataAvail = false; } + snprintf(val, 10, "%d", ESP.getFreeHeap()); + mMqtt.sendMsg("free_heap", val); } if(mSerialValues) { @@ -212,13 +214,15 @@ void app::loop(void) { for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { Inverter<> *iv = mSys->getInverterByPos(id); if(NULL != iv) { - for(uint8_t i = 0; i < iv->listLen; i++) { - if(0.0f != iv->getValue(i)) { - snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); - snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i)); - DPRINTLN(String(topic) + ": " + String(val)); + if(iv->isAvailable(mTimestamp)) { + for(uint8_t i = 0; i < iv->listLen; i++) { + if(0.0f != iv->getValue(i)) { + snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); + snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i)); + DPRINTLN(String(topic) + ": " + String(val)); + } + yield(); } - yield(); } } } @@ -318,7 +322,10 @@ void app::processPayload(bool retransmit) { else { if(mSerialDebug) DPRINTLN(F("Error while retrieving data: last frame missing: Request Retransmit")); - mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); + if(0x00 != mLastPacketId) + mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, mLastPacketId, true); + else + mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); } mSys->Radio.switchRxCh(100); } @@ -343,7 +350,6 @@ void app::processPayload(bool retransmit) { iv->addValue(i, payload); } iv->doCalculations(); - mMqttNewDataAvail = true; } } } diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 9fb6c0e8..055ea44f 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -87,6 +87,7 @@ class app : public Main { invPayload_t mPayload[MAX_NUM_INVERTERS]; uint32_t mRxFailed; uint32_t mRxSuccess; + uint8_t mLastPacketId; // timer uint32_t mTicker; @@ -100,9 +101,10 @@ class app : public Main { uint16_t mMqttTicker; uint16_t mMqttInterval; bool mMqttActive; + + // serial uint16_t mSerialTicker; uint16_t mSerialInterval; - bool mMqttNewDataAvail; }; #endif /*__APP_H__*/ diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 5608e51e..37f8c024 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -16,7 +16,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 4 -#define VERSION_PATCH 10 +#define VERSION_PATCH 11 //------------------------------------- diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 3864d508..a539ee4f 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -123,6 +123,7 @@ class HmRadio { bool tx_ok, tx_fail, rx_ready; mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // reset interrupt status mNrf24.flush_rx(); // drop the packet + break; } } RESTORE_IRQ; diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index e332209c..5d4bc974 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -32,6 +32,8 @@ Main::Main(void) { mUptimeInterval = 1000; mTimestamp = 0; + + mHeapStatCnt = 0; } @@ -102,6 +104,11 @@ void Main::loop(void) { DPRINTLN("[NTP]: " + getDateTimeStr(mTimestamp)); } } + + if(++mHeapStatCnt >= 10) { + mHeapStatCnt = 0; + stats(); + } } } @@ -134,6 +141,7 @@ bool Main::getConfig(void) { if(!mSettingsValid) { DPRINTLN(F("Settings not valid, erasing ...")); eraseSettings(); + saveValues(false); delay(100); DPRINTLN(F("... restarting ...")); delay(100); diff --git a/tools/esp8266/main.h b/tools/esp8266/main.h index 9656c017..7523cfc2 100644 --- a/tools/esp8266/main.h +++ b/tools/esp8266/main.h @@ -81,6 +81,15 @@ class Main { return false; } + void stats(void) { + uint32_t free; + uint16_t max; + uint8_t frag; + ESP.getHeapStats(&free, &max, &frag); + + Serial.printf("free: 0x%x - max: 0x%x - frag: %d%%\n", free, max, frag); + } + char mStationSsid[SSID_LEN]; char mStationPwd[PWD_LEN]; bool mWifiSettingsValid; @@ -115,6 +124,7 @@ class Main { uint32_t mUptimeTicker; uint16_t mUptimeInterval; uint32_t mUptimeSecs; + uint8_t mHeapStatCnt; DNSServer *mDns; ESP8266HTTPUpdateServer *mUpdater;