From 665a4abc47cbee81eb32cf5bcd23782ee044561b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 22 Sep 2018 16:09:13 +0200 Subject: [PATCH] Fix Pow R2 and S31 low power * Add power value below 5W to Sonoff Pow R2 and S31 (#3745) * Add force_update to Home Assistant discovery (#3873) --- sonoff/_changelog.ino | 2 ++ sonoff/xnrg_02_cse7766.ino | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 2b954e151..cb72ff407 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -2,6 +2,8 @@ * Removed commands PowerCal, VoltageCal and CurrentCal as more functionality is provided by commands PowerSet, VoltageSet and CurrentSet * Allow decimals as input to commands PowerSet, VoltageSet and CurrentSet * Add support for PCA9685 12bit 16pin hardware PWM driver (#3866) + * Add power value below 5W to Sonoff Pow R2 and S31 (#3745) + * Add force_update to Home Assistant discovery (#3873) * * 6.2.1.5 20180921 * Add authentication to HTTP web pages diff --git a/sonoff/xnrg_02_cse7766.ino b/sonoff/xnrg_02_cse7766.ino index a433bc4a4..8ba1b146e 100644 --- a/sonoff/xnrg_02_cse7766.ino +++ b/sonoff/xnrg_02_cse7766.ino @@ -27,6 +27,8 @@ #define XNRG_02 2 +#define CSE_MAX_INVALID_POWER 128 // Number of invalid power receipts before deciding active power is zero + #define CSE_NOT_CALIBRATED 0xAA #define CSE_PULSES_NOT_INITIALIZED -1 @@ -42,10 +44,12 @@ long power_cycle = 0; unsigned long power_cycle_first = 0; long cf_pulses = 0; long cf_pulses_last_time = CSE_PULSES_NOT_INITIALIZED; +uint8_t cse_power_invalid = CSE_MAX_INVALID_POWER; void CseReceived() { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 + // 55 5A 02 F7 60 00 03 5A 00 40 10 04 8B 9F 51 A6 58 18 72 75 61 AC A1 30 - Power not valid (load below 5W) // 55 5A 02 F7 60 00 03 AB 00 40 10 02 60 5D 51 A6 58 03 E9 EF 71 0B 7A 36 // Hd Id VCal---- Voltage- ICal---- Current- PCal---- Power--- Ad CF--- Ck @@ -89,10 +93,11 @@ void CseReceived() energy_voltage = (float)(Settings.energy_voltage_calibration * CSE_UREF) / (float)voltage_cycle; } if (adjustement & 0x10) { // Power valid + cse_power_invalid = 0; if ((header & 0xF2) == 0xF2) { // Power cycle exceeds range energy_power = 0; } else { - if (0 == power_cycle_first) power_cycle_first = power_cycle; // Skip first incomplete power_cycle + if (0 == power_cycle_first) { power_cycle_first = power_cycle; } // Skip first incomplete power_cycle if (power_cycle_first != power_cycle) { power_cycle_first = -1; energy_power = (float)(Settings.energy_power_calibration * CSE_PREF) / (float)power_cycle; @@ -101,8 +106,12 @@ void CseReceived() } } } else { - power_cycle_first = 0; - energy_power = 0; // Powered on but no load + if (cse_power_invalid < CSE_MAX_INVALID_POWER) { // Allow measurements down to about 1W + cse_power_invalid++; + } else { + power_cycle_first = 0; + energy_power = 0; // Powered on but no load + } } if (adjustement & 0x20) { // Current valid if (0 == energy_power) {