diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 912a7ff0..311db990 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -447,11 +447,12 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_TOPIC_RFLEARN "rflearn" // Power module -#define MQTT_TOPIC_POWER_ACTIVE "power" #define MQTT_TOPIC_CURRENT "current" #define MQTT_TOPIC_VOLTAGE "voltage" +#define MQTT_TOPIC_POWER_ACTIVE "power" #define MQTT_TOPIC_POWER_APPARENT "apparent" #define MQTT_TOPIC_POWER_REACTIVE "reactive" +#define MQTT_TOPIC_MAX_POWER "max_power" #define MQTT_TOPIC_POWER_FACTOR "factor" #define MQTT_TOPIC_ENERGY_DELTA "energy_delta" #define MQTT_TOPIC_ENERGY_TOTAL "energy_total" diff --git a/code/espurna/libs/MedianFilter.h b/code/espurna/libs/MedianFilter.h index 944fe894..de4aaaf6 100644 --- a/code/espurna/libs/MedianFilter.h +++ b/code/espurna/libs/MedianFilter.h @@ -30,6 +30,14 @@ class MedianFilter { add(last); } + virtual double max() { + double max = 0; + for (unsigned char i = 1; i < _data->size(); i++) { + if (max < _data->at(i)) max = _data->at(i); + } + return max; + } + virtual double result(bool do_reset = false) { double sum = 0; diff --git a/code/espurna/power.ino b/code/espurna/power.ino index d16845f1..066018ed 100644 --- a/code/espurna/power.ino +++ b/code/espurna/power.ino @@ -199,17 +199,20 @@ void _powerRead() { void _powerReport() { // Get the fitered values - _power_current = _filter_current.result(true); #if POWER_HAS_ACTIVE - _power_apparent = _filter_apparent.result(true); + double max_power = _filter_active.max(); + _power_current = _filter_current.result(true); _power_voltage = _filter_voltage.result(true); _power_active = _filter_active.result(true); + _power_apparent = _filter_apparent.result(true); if (_power_active > _power_apparent) _power_apparent = _power_active; _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; double power = _power_active; #else + double max_power = _filter_current.max() * _power_voltage; + _power_current = _filter_current.result(true); _power_apparent = _power_current * _power_voltage; double power = _power_apparent; #endif @@ -232,6 +235,7 @@ void _powerReport() { { mqttSend(MQTT_TOPIC_CURRENT, buf_current); mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); + mqttSend(MQTT_TOPIC_MAX_POWER, String((int) max_power).c_str()); mqttSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta); mqttSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total); #if POWER_HAS_ACTIVE @@ -267,6 +271,7 @@ void _powerReport() { if (idbEnabled()) { idbSend(MQTT_TOPIC_CURRENT, buf_current); idbSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); + idbSend(MQTT_TOPIC_MAX_POWER, String((int) max_power).c_str()); idbSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta); idbSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total); #if POWER_HAS_ACTIVE