[RN8209] Detect voltage changes (#1850)

Voltage changes > 2V will trigger a data sending
This commit is contained in:
Florian
2024-01-05 09:33:25 -06:00
committed by GitHub
parent 5f1463e6ab
commit a84df78a33
3 changed files with 11 additions and 4 deletions

View File

@@ -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}`

View File

@@ -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<JSON_MSG_BUFFER> RN8209dataBuffer;
JsonObject RN8209data = RN8209dataBuffer.to<JsonObject>();
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);

View File

@@ -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