diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index b1de178f..350d9a9e 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -70,6 +70,7 @@ #define EMON_MAINS_VOLTAGE 230 #define EMON_APOWER_TOPIC "/apower" #define EMON_ENERGY_TOPIC "/energy" +#define EMON_CURRENT_TOPIC "/current" //-------------------------------------------------------------------------------- // HLW8012 power sensor (Sonoff POW) diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index 20d40a28..1f2edd99 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -153,19 +153,30 @@ void powerMonitorLoop() { // Send MQTT messages averaged every EMON_MEASUREMENTS if (measurements == EMON_MEASUREMENTS) { - _power = (int) ((sum - max - min) * mainsVoltage / (measurements - 2)); + // Calculate average current (removing max and min values) and create C-string + double average = (sum - max - min) / (measurements - 2); + dtostrf(average, 5, 2, current); + char *c = current; + while ((unsigned char) *c == ' ') ++c; + + // Calculate average apparent power from current and create C-string + _power = (int) (current_avgd * mainsVoltage); char power[6]; snprintf(power, 6, "%d", _power); + // Calculate energy increment (ppower times time) and create C-string double energy_inc = (double) _power * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0; char energy_buf[11]; dtostrf(energy_inc, 11, 3, energy_buf); char *e = energy_buf; while ((unsigned char) *e == ' ') ++e; + // Report values to MQTT broker mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), power); + mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), c); mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), e); + // Report values to Domoticz #if ENABLE_DOMOTICZ { char buffer[20]; @@ -173,13 +184,12 @@ void powerMonitorLoop() { domoticzSend("dczPowIdx", 0, buffer); snprintf(buffer, 20, "%s", e); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf(buffer, 20, "%d", voltage); - domoticzSend("dczVoltIdx", 0, buffer); - snprintf(buffer, 20, "%s", String(current).c_str()); + snprintf(buffer, 20, "%s", c); domoticzSend("dczCurrentIdx", 0, buffer); } #endif + // Reset counters sum = measurements = 0; } diff --git a/code/espurna/pow.ino b/code/espurna/pow.ino index 450dcecb..b6293320 100644 --- a/code/espurna/pow.ino +++ b/code/espurna/pow.ino @@ -218,12 +218,14 @@ void powLoop() { factor = (apparent > 0) ? 100 * power / apparent : 100; if (factor > 100) factor = 100; + // Calculate energy increment (ppower times time) and create C-string double energy_inc = (double) power * POW_REPORT_EVERY * POW_UPDATE_INTERVAL / 1000.0 / 3600.0; char energy_buf[11]; dtostrf(energy_inc, 11, 3, energy_buf); char *e = energy_buf; while ((unsigned char) *e == ' ') ++e; + // Report values to MQTT broker mqttSend(getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), String(power).c_str()); mqttSend(getSetting("powEnergyTopic", POW_ENERGY_TOPIC).c_str(), e); mqttSend(getSetting("powCurrentTopic", POW_CURRENT_TOPIC).c_str(), String(current).c_str()); @@ -232,6 +234,7 @@ void powLoop() { mqttSend(getSetting("powRPowerTopic", POW_RPOWER_TOPIC).c_str(), String(reactive).c_str()); mqttSend(getSetting("powPFactorTopic", POW_PFACTOR_TOPIC).c_str(), String(factor).c_str()); + // Report values to Domoticz #if ENABLE_DOMOTICZ { char buffer[20]; @@ -246,6 +249,7 @@ void powLoop() { } #endif + // Reset counters power_sum = current_sum = voltage_sum = 0; report_count = POW_REPORT_EVERY; diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 3dafb0b1..543eee2e 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -351,7 +351,6 @@ void _wsStart(uint32_t client_id) { #if ENABLE_EMON root["dczPowIdx"] = getSetting("dczPowIdx").toInt(); root["dczEnergyIdx"] = getSetting("dczEnergyIdx").toInt(); - root["dczVoltIdx"] = getSetting("dczVoltIdx").toInt(); root["dczCurrentIdx"] = getSetting("dczCurrentIdx").toInt(); #endif diff --git a/code/html/index.html b/code/html/index.html index a83d3436..94111a25 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -415,7 +415,7 @@