From a84df78a33897c50f618cdbd53365ea1eefa7301 Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 5 Jan 2024 09:33:25 -0600 Subject: [PATCH] [RN8209] Detect voltage changes (#1850) Voltage changes > 2V will trigger a data sending --- docs/use/sensors.md | 2 +- main/ZsensorRN8209.ino | 10 +++++++--- main/config_RN8209.h | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/use/sensors.md b/docs/use/sensors.md index abee7a52..b94c2c20 100644 --- a/docs/use/sensors.md +++ b/docs/use/sensors.md @@ -52,7 +52,7 @@ This notification pin can be inverted if driving directly or through a transisto `#define INVERT_LED_NOTIFY true` ### RN8209 -You will receive every `TimeBetweenPublishingRN8209` (set into config_RN8209.h) the RN8209 measurements (every 60s per default), or if the difference between the previous current reading and the new reading is more than 0.1A. +You will receive every `TimeBetweenPublishingRN8209` (set into config_RN8209.h) the RN8209 measurements (every 60s per default), or if the difference between the previous current reading and the new reading is more than 0.1A, or if the difference between the previous voltage reading and the new reading is more than 2V. One reading is done every 0.5s. `home/OpenMQTTGateway/RN8209toMQTT {"volt":120.34,"current":7.92,"power":954.61}` diff --git a/main/ZsensorRN8209.ino b/main/ZsensorRN8209.ino index f881cf30..d0fced60 100644 --- a/main/ZsensorRN8209.ino +++ b/main/ZsensorRN8209.ino @@ -49,6 +49,7 @@ void rn8209_loop(void* mode) { uint8_t retc = 1; uint8_t retp = 1; static float previousCurrent = 0; + static float previousVoltage = 0; if (ret) { uint32_t temp_current = 0; retc = rn8209c_read_current(phase_A, &temp_current); @@ -61,21 +62,25 @@ void rn8209_loop(void* mode) { current = current / 10000.0; overLimitCurrent(current); } + if (retv == 0) { + voltage = (float)temp_voltage / 1000.0; + } } unsigned long now = millis(); if ((now > (PublishingTimerRN8209 + TimeBetweenPublishingRN8209) || !PublishingTimerRN8209 || - (abs(current - previousCurrent) > MinCurrentThreshold)) && + (abs(current - previousCurrent) > MinCurrentThreshold) || (abs(voltage - previousVoltage) > MinVoltageThreshold)) && !ProcessLock) { StaticJsonDocument RN8209dataBuffer; JsonObject RN8209data = RN8209dataBuffer.to(); if (retc == 0) { + previousCurrent = current; RN8209data["current"] = round2(current); } uint32_t temp_power = 0; retp = rn8209c_read_power(phase_A, &temp_power); if (retv == 0) { - voltage = (float)temp_voltage / 1000.0; + previousVoltage = voltage; RN8209data["volt"] = round2(voltage); } if (ret == 1) { @@ -88,7 +93,6 @@ void rn8209_loop(void* mode) { RN8209data["power"] = round2(power); } PublishingTimerRN8209 = now; - previousCurrent = current; if (RN8209data) { RN8209data["origin"] = subjectRN8209toMQTT; handleJsonEnqueue(RN8209data, QueueSemaphoreTimeOutTask); diff --git a/main/config_RN8209.h b/main/config_RN8209.h index 74083d40..744ce394 100644 --- a/main/config_RN8209.h +++ b/main/config_RN8209.h @@ -53,4 +53,7 @@ extern void RN8209toMQTT(); #ifndef MinCurrentThreshold # define MinCurrentThreshold 0.1 // (A) Minimum current change that will trigger the publishing of the RN8209 measurements #endif +#ifndef MinVoltageThreshold +# define MinVoltageThreshold 2 // (V) Minimum voltage change that will trigger the publishing of the RN8209 measurements +#endif #endif