From b8994248fdf7b38d728d635d54aaa0b35b455247 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Tue, 26 Sep 2023 19:12:58 +0200 Subject: [PATCH] change from counting to millis() --- src/app.cpp | 42 ++++++++++++++++------------- src/config/settings.h | 6 +++-- src/plugins/zeroExport/zeroExport.h | 15 +++-------- src/web/RestApi.h | 2 +- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index d8ab985b..d49a4c70 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -593,29 +593,33 @@ void app::updateLed(void) { //----------------------------------------------------------------------------- #if defined(ESP32) void app::zeroexport() { - if (!mConfig->plugin.zexport.enabled && - !mConfig->plugin.zexport.rdytoSend) return; // check if plugin is enabled && indicate to send new value + if (!mConfig->plugin.zexport.enabled) return; // check if plugin is enabled && indicate to send new value - Inverter<> *iv = mSys.getInverterByPos(mConfig->plugin.zexport.Iv); + if (millis() - mConfig->plugin.zexport.lastTime < mConfig->plugin.zexport.count_avg * 1000UL) + { + Inverter<> *iv = mSys.getInverterByPos(mConfig->plugin.zexport.Iv); - DynamicJsonDocument doc(512); - JsonObject object = doc.to(); + DynamicJsonDocument doc(512); + JsonObject object = doc.to(); - double nValue = round(mzExport.getPowertoSetnewValue()); - double twoPerVal = nValue <= (iv->getMaxPower() / 100 * 2 ); - if(mConfig->plugin.zexport.two_percent && (nValue <= twoPerVal)) { - object["val"] = twoPerVal; - } else { - object["val"] = nValue; - } + double nValue = round(mzExport.getPowertoSetnewValue()); + double twoPerVal = nValue <= (iv->getMaxPower() / 100 * 2 ); + if(mConfig->plugin.zexport.two_percent && (nValue <= twoPerVal)) { + object["val"] = twoPerVal; + } else { + object["val"] = nValue; + } + + object["id"] = mConfig->plugin.zexport.Iv; + object["path"] = "ctrl"; + object["cmd"] = "limit_nonpersistent_absolute"; - object["id"] = mConfig->plugin.zexport.Iv; - object["path"] = "ctrl"; - object["cmd"] = "limit_nonpersistent_absolute"; + String data; + serializeJsonPretty(object, data); + DPRINTLN(DBG_INFO, data); + mApi.ctrlRequest(object); + } - String data; - serializeJsonPretty(object, data); - DPRINTLN(DBG_INFO, data); - mApi.ctrlRequest(object); + mConfig->plugin.zexport.lastTime = millis(); // set last timestamp } #endif \ No newline at end of file diff --git a/src/config/settings.h b/src/config/settings.h index f75c3689..b4f450d8 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -143,11 +143,12 @@ typedef struct { uint8_t device; // save the monitor device (1 - Shelly; 2 - Hichi;) uint8_t Iv; // saves the inverter that is used for regulation bool enabled; - bool rdytoSend; // indicate to send new value float power_avg; uint8_t count_avg; double total_power; + unsigned long lastTime; // tic toc + bool two_percent; // ask if not go lower then 2% } cfgzeroExport_t; #endif @@ -457,8 +458,9 @@ class settings { #if defined(ESP32) snprintf(mCfg.plugin.zexport.monitor_ip, ZEXPORT_ADDR_LEN, "%s", DEF_ZEXPORT); mCfg.plugin.zexport.enabled = false; - mCfg.plugin.zexport.rdytoSend = false; mCfg.plugin.zexport.count_avg = 10; + mCfg.plugin.zexport.lastTime = millis(); // do not change! + mCfg.plugin.zexport.power_avg = 10; mCfg.plugin.zexport.device = 0; mCfg.plugin.zexport.Iv = 0; diff --git a/src/plugins/zeroExport/zeroExport.h b/src/plugins/zeroExport/zeroExport.h index 35e3dd6d..75411a7b 100644 --- a/src/plugins/zeroExport/zeroExport.h +++ b/src/plugins/zeroExport/zeroExport.h @@ -20,15 +20,11 @@ class ZeroExport { mConfig = config; } - void payloadEventListener(uint8_t cmd) { - mCfg->rdytoSend = false; - } - void tickerSecond() { - if (!mCfg->rdytoSend || ((++mLoopCnt % mCfg->count_avg) == 0)) { - mCfg->rdytoSend = true; - mLoopCnt = 0; - zero(); + //DPRINTLN(DBG_INFO, (F("tickerSecond()"))); + if (millis() - mCfg->lastTime < mCfg->count_avg * 1000UL) { + zero(); // just refresh when it is needed. To get cpu load low. + //DPRINTLN(DBG_INFO, (F("zero()"))); } } @@ -53,8 +49,6 @@ class ZeroExport { private: HTTPClient http; - void loop() { } - // TODO: Need to improve here. 2048 for a JSON Obj is to big!? void zero() { switch (mCfg->device) { @@ -117,7 +111,6 @@ class ZeroExport { } // private member variables - uint8_t mLoopCnt; const char *mVersion; cfgzeroExport_t *mCfg; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 18ba74e2..80a6d7c6 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -65,7 +65,7 @@ class RestApi { void ctrlRequest(JsonObject obj) { char out[128]; serializeJson(obj, out, 128); - DPRINTLN(DBG_INFO, "RestApi: " + String(out)); + //DPRINTLN(DBG_INFO, "RestApi: " + String(out)); DynamicJsonDocument json(128); JsonObject dummy = json.as(); if(obj[F("path")] == "ctrl")