diff --git a/code/espurna/power.h b/code/espurna/power.h index 27312904..2c1a786a 100644 --- a/code/espurna/power.h +++ b/code/espurna/power.h @@ -15,7 +15,7 @@ class MedianFilter { } virtual void reset() { - _data[0] = _data[_pointer-1]; + if (_pointer > 1) _data[0] = _data[_pointer-1]; _pointer = 1; for (unsigned char i=1; i<=_size; i++) _data[i] = 0; } diff --git a/code/espurna/power.ino b/code/espurna/power.ino index 8a970830..b8d6f989 100644 --- a/code/espurna/power.ino +++ b/code/espurna/power.ino @@ -99,6 +99,7 @@ void _powerRead() { double active = _powerActivePower(); double reactive = (apparent > active) ? sqrt(apparent * apparent - active * active) : 0; double factor = (apparent > 0) ? active / apparent : 1; + if (factor > 1) factor = 1; #endif // Filters @@ -172,10 +173,10 @@ void _powerReport() { _power_ready = true; char buf_current[10]; - dtostrf(_power_current, 6, POWER_CURRENT_PRECISION, buf_current); + dtostrf(_power_current, -9, POWER_CURRENT_PRECISION, buf_current); double energy_delta = _power_active * POWER_ENERGY_FACTOR; char buf_energy[10]; - dtostrf(energy_delta, 6, POWER_CURRENT_PRECISION, buf_energy); + dtostrf(energy_delta, -9, POWER_CURRENT_PRECISION, buf_energy); { mqttSend(MQTT_TOPIC_CURRENT, buf_current); @@ -190,7 +191,7 @@ void _powerReport() { } #if DOMOTICZ_SUPPORT - { + if (domoticzEnabled()) { char buffer[20]; snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _power_active, buf_energy); domoticzSend("dczPowIdx", 0, buffer); @@ -205,7 +206,7 @@ void _powerReport() { #endif #if INFLUXDB_SUPPORT - { + if (influxdbEnabled()) { influxDBSend(MQTT_TOPIC_CURRENT, buf_current); influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); influxDBSend(MQTT_TOPIC_ENERGY, buf_energy); diff --git a/code/espurna/power_hlw8012.ino b/code/espurna/power_hlw8012.ino index de8ebe14..38914e12 100644 --- a/code/espurna/power_hlw8012.ino +++ b/code/espurna/power_hlw8012.ino @@ -30,7 +30,7 @@ void ICACHE_RAM_ATTR _hlw_cf_isr() { _hlw8012.cf_interrupt(); } -void _hlwSetCalibration() { +void _hlwRestoreCalibration() { double value; value = getSetting("pwrRatioP", 0).toFloat(); if (value > 0) _hlw8012.setPowerMultiplier(value); @@ -40,7 +40,7 @@ void _hlwSetCalibration() { if (value > 0) _hlw8012.setVoltageMultiplier(value); } -void _hlwGetCalibration() { +void _hlwPersistCalibration() { setSetting("pwrRatioP", _hlw8012.getPowerMultiplier()); setSetting("pwrRatioC", _hlw8012.getCurrentMultiplier()); setSetting("pwrRatioV", _hlw8012.getVoltageMultiplier()); @@ -90,17 +90,19 @@ void _powerCalibrateProvider(unsigned char magnitude, double value) { if (magnitude == POWER_MAGNITUDE_ACTIVE) _hlw8012.expectedActivePower(value); if (magnitude == POWER_MAGNITUDE_CURRENT) _hlw8012.expectedCurrent(value); if (magnitude == POWER_MAGNITUDE_VOLTAGE) _hlw8012.expectedVoltage(value); - _hlwGetCalibration(); + _hlwPersistCalibration(); } void _powerResetCalibrationProvider() { _hlw8012.resetMultipliers(); - _hlwGetCalibration(); + delSetting("pwrRatioC"); + delSetting("pwrRatioV"); + delSetting("pwrRatioP"); + saveSettings(); } void _powerConfigureProvider() { - _hlwSetCalibration(); - _hlwGetCalibration(); + // Nothing to do } void _powerSetupProvider() { @@ -124,7 +126,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(); + _hlwRestoreCalibration(); _power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) { powerEnabled(true); @@ -152,8 +154,6 @@ void _powerLoopProvider(bool before) { } - } else { - } }