Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RN8209] Detect voltage changes #1850

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/use/sensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
10 changes: 7 additions & 3 deletions main/ZsensorRN8209.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions main/config_RN8209.h
Original file line number Diff line number Diff line change
Expand Up @@ -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