From 1f1ba1deb7265da4fe8c434a6cb79f00a47e5fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 19 Sep 2017 21:51:45 +0200 Subject: [PATCH] HLW8012 working with new modules --- code/espurna/power.h | 6 +++--- code/espurna/power.ino | 2 ++ code/espurna/power_hlw8012.ino | 11 +++-------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/code/espurna/power.h b/code/espurna/power.h index 22dadb59..27312904 100644 --- a/code/espurna/power.h +++ b/code/espurna/power.h @@ -15,9 +15,9 @@ class MedianFilter { } virtual void reset() { - _data[0] = _data[_size]; + _data[0] = _data[_pointer-1]; _pointer = 1; - for (unsigned char i=_pointer; i<=_size; i++) _data[i] = 0; + for (unsigned char i=1; i<=_size; i++) _data[i] = 0; } virtual void add(double value) { @@ -47,7 +47,7 @@ class MedianFilter { } - sum /= (_pointer - 1); + sum /= (_pointer - 2); } diff --git a/code/espurna/power.ino b/code/espurna/power.ino index 49dbe737..fb505f5d 100644 --- a/code/espurna/power.ino +++ b/code/espurna/power.ino @@ -153,12 +153,14 @@ void _powerReport() { _power_apparent = _filter_apparent.average(true); _power_voltage = _filter_voltage.average(true); _power_active = _filter_active.average(true); + if (_power_active > _power_apparent) _power_apparent = _power_active; #else _power_apparent = _power_current * _power_voltage; _power_active = _power_apparent; #endif _power_reactive = (_power_apparent > _power_active) ? sqrt(_power_apparent * _power_apparent - _power_active * _power_active) : 0; _power_factor = (_power_apparent > 0) ? _power_active / _power_apparent : 1; + if (_power_factor > 1) _power_factor = 1; _power_ready = true; char buf_current[10]; diff --git a/code/espurna/power_hlw8012.ino b/code/espurna/power_hlw8012.ino index d74399b8..b42e3bfb 100644 --- a/code/espurna/power_hlw8012.ino +++ b/code/espurna/power_hlw8012.ino @@ -94,16 +94,11 @@ double _powerApparentPower() { } double _powerReactivePower() { - double active = _powerActivePower(); - double apparent = _powerApparentPower(); - if (apparent > active) return sqrt(apparent * apparent - active * active); - return 0; + return _hlw8012.getReactivePower(); } double _powerPowerFactor() { - double apparent = _powerApparentPower(); - if (apparent > 0) return _powerActivePower() / apparent; - return 1; + return _hlw8012.getPowerFactor(); } void _powerEnabledProvider() { @@ -142,7 +137,7 @@ void _powerSetupProvider() { // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012 _hlw8012.setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN); - powerConfigureProvider(); + _powerConfigureProvider(); _power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) { powerEnabled(true);