Browse Source

change from counting to millis()

pull/1155/head
DanielR92 2 years ago
parent
commit
b8994248fd
  1. 42
      src/app.cpp
  2. 6
      src/config/settings.h
  3. 15
      src/plugins/zeroExport/zeroExport.h
  4. 2
      src/web/RestApi.h

42
src/app.cpp

@ -593,29 +593,33 @@ void app::updateLed(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#if defined(ESP32) #if defined(ESP32)
void app::zeroexport() { void app::zeroexport() {
if (!mConfig->plugin.zexport.enabled && if (!mConfig->plugin.zexport.enabled) return; // check if plugin is enabled && indicate to send new value
!mConfig->plugin.zexport.rdytoSend) 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); DynamicJsonDocument doc(512);
JsonObject object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
double nValue = round(mzExport.getPowertoSetnewValue()); double nValue = round(mzExport.getPowertoSetnewValue());
double twoPerVal = nValue <= (iv->getMaxPower() / 100 * 2 ); double twoPerVal = nValue <= (iv->getMaxPower() / 100 * 2 );
if(mConfig->plugin.zexport.two_percent && (nValue <= twoPerVal)) { if(mConfig->plugin.zexport.two_percent && (nValue <= twoPerVal)) {
object["val"] = twoPerVal; object["val"] = twoPerVal;
} else { } else {
object["val"] = nValue; object["val"] = nValue;
} }
object["id"] = mConfig->plugin.zexport.Iv;
object["path"] = "ctrl";
object["cmd"] = "limit_nonpersistent_absolute";
object["id"] = mConfig->plugin.zexport.Iv; String data;
object["path"] = "ctrl"; serializeJsonPretty(object, data);
object["cmd"] = "limit_nonpersistent_absolute"; DPRINTLN(DBG_INFO, data);
mApi.ctrlRequest(object);
}
String data; mConfig->plugin.zexport.lastTime = millis(); // set last timestamp
serializeJsonPretty(object, data);
DPRINTLN(DBG_INFO, data);
mApi.ctrlRequest(object);
} }
#endif #endif

6
src/config/settings.h

@ -143,11 +143,12 @@ typedef struct {
uint8_t device; // save the monitor device (1 - Shelly; 2 - Hichi;) uint8_t device; // save the monitor device (1 - Shelly; 2 - Hichi;)
uint8_t Iv; // saves the inverter that is used for regulation uint8_t Iv; // saves the inverter that is used for regulation
bool enabled; bool enabled;
bool rdytoSend; // indicate to send new value
float power_avg; float power_avg;
uint8_t count_avg; uint8_t count_avg;
double total_power; double total_power;
unsigned long lastTime; // tic toc
bool two_percent; // ask if not go lower then 2% bool two_percent; // ask if not go lower then 2%
} cfgzeroExport_t; } cfgzeroExport_t;
#endif #endif
@ -457,8 +458,9 @@ class settings {
#if defined(ESP32) #if defined(ESP32)
snprintf(mCfg.plugin.zexport.monitor_ip, ZEXPORT_ADDR_LEN, "%s", DEF_ZEXPORT); snprintf(mCfg.plugin.zexport.monitor_ip, ZEXPORT_ADDR_LEN, "%s", DEF_ZEXPORT);
mCfg.plugin.zexport.enabled = false; mCfg.plugin.zexport.enabled = false;
mCfg.plugin.zexport.rdytoSend = false;
mCfg.plugin.zexport.count_avg = 10; mCfg.plugin.zexport.count_avg = 10;
mCfg.plugin.zexport.lastTime = millis(); // do not change!
mCfg.plugin.zexport.power_avg = 10; mCfg.plugin.zexport.power_avg = 10;
mCfg.plugin.zexport.device = 0; mCfg.plugin.zexport.device = 0;
mCfg.plugin.zexport.Iv = 0; mCfg.plugin.zexport.Iv = 0;

15
src/plugins/zeroExport/zeroExport.h

@ -20,15 +20,11 @@ class ZeroExport {
mConfig = config; mConfig = config;
} }
void payloadEventListener(uint8_t cmd) {
mCfg->rdytoSend = false;
}
void tickerSecond() { void tickerSecond() {
if (!mCfg->rdytoSend || ((++mLoopCnt % mCfg->count_avg) == 0)) { //DPRINTLN(DBG_INFO, (F("tickerSecond()")));
mCfg->rdytoSend = true; if (millis() - mCfg->lastTime < mCfg->count_avg * 1000UL) {
mLoopCnt = 0; zero(); // just refresh when it is needed. To get cpu load low.
zero(); //DPRINTLN(DBG_INFO, (F("zero()")));
} }
} }
@ -53,8 +49,6 @@ class ZeroExport {
private: private:
HTTPClient http; HTTPClient http;
void loop() { }
// TODO: Need to improve here. 2048 for a JSON Obj is to big!? // TODO: Need to improve here. 2048 for a JSON Obj is to big!?
void zero() { void zero() {
switch (mCfg->device) { switch (mCfg->device) {
@ -117,7 +111,6 @@ class ZeroExport {
} }
// private member variables // private member variables
uint8_t mLoopCnt;
const char *mVersion; const char *mVersion;
cfgzeroExport_t *mCfg; cfgzeroExport_t *mCfg;

2
src/web/RestApi.h

@ -65,7 +65,7 @@ class RestApi {
void ctrlRequest(JsonObject obj) { void ctrlRequest(JsonObject obj) {
char out[128]; char out[128];
serializeJson(obj, out, 128); serializeJson(obj, out, 128);
DPRINTLN(DBG_INFO, "RestApi: " + String(out)); //DPRINTLN(DBG_INFO, "RestApi: " + String(out));
DynamicJsonDocument json(128); DynamicJsonDocument json(128);
JsonObject dummy = json.as<JsonObject>(); JsonObject dummy = json.as<JsonObject>();
if(obj[F("path")] == "ctrl") if(obj[F("path")] == "ctrl")

Loading…
Cancel
Save