From ad1b62776bfa8bea259c5bf0a0b942987f7f5a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 5 Feb 2017 11:40:13 +0100 Subject: [PATCH] Reporting energy incrementals (domoticz, MQTT). Removed energy API entry point --- code/espurna/config/general.h | 2 +- code/espurna/config/sensors.h | 2 +- code/espurna/emon.ino | 47 +++++++++-------------------------- code/espurna/pow.ino | 38 ++++++---------------------- 4 files changed, 22 insertions(+), 67 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 477e62c8..10145c7d 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -12,7 +12,7 @@ //-------------------------------------------------------------------------------- #define EEPROM_RELAY_STATUS 0 -#define EEPROM_POWER_COUNT 1 +#define EEPROM_ENERGY_COUNT 1 //-------------------------------------------------------------------------------- // RELAY diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 1763d4fd..36a57ea3 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -67,7 +67,7 @@ #define EMON_INTERVAL 10000 #define EMON_MEASUREMENTS 6 #define EMON_MAINS_VOLTAGE 230 -#define EMON_POWER_TOPIC "/power" +#define EMON_APOWER_TOPIC "/apower" #define EMON_ENERGY_TOPIC "/energy" //-------------------------------------------------------------------------------- diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index f9bd2609..7b9eabf0 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -25,7 +25,6 @@ Copyright (C) 2016-2017 by Xose Pérez EmonLiteESP emon; double _current = 0; unsigned int _power = 0; -double _energy = 0; // ----------------------------------------------------------------------------- // Provider @@ -64,28 +63,10 @@ unsigned int getPower() { return _power; } -double getEnergy() { - return _energy; -} - double getCurrent() { return _current; } -void retrieveEnergy() { - unsigned long energy = EEPROM.read(EEPROM_POWER_COUNT + 1); - energy = (energy << 8) + EEPROM.read(EEPROM_POWER_COUNT); - if (energy == 0xFFFF) energy = 0; - _energy = energy; -} - -void saveEnergy() { - unsigned int energy = (int) _energy; - EEPROM.write(EEPROM_POWER_COUNT, energy & 0xFF); - EEPROM.write(EEPROM_POWER_COUNT + 1, (energy >> 8) & 0xFF); - EEPROM.commit(); -} - void powerMonitorSetup() { // backwards compatibility @@ -117,11 +98,6 @@ void powerMonitorSetup() { apiRegister("/api/power", "power", [](char * buffer, size_t len) { snprintf(buffer, len, "%d", _power); }); - apiRegister("/api/energy", "energy", [](char * buffer, size_t len) { - snprintf(buffer, len, "%ld", (unsigned long) _energy); - }); - - retrieveEnergy(); } @@ -178,26 +154,27 @@ void powerMonitorLoop() { if (measurements == EMON_MEASUREMENTS) { _power = (int) ((sum - max - min) * mainsVoltage / (measurements - 2)); - double window = (double) EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0; - _energy += _power * window; - saveEnergy(); - sum = 0; - measurements = 0; - char power[6]; snprintf(power, 6, "%d", _power); - char energy[8]; - snprintf(energy, 8, "%ld", (unsigned long) _energy); - mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); - mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), energy); + + double energy_inc = (double) _power * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0; + char energy_buf[10]; + dtostrf(energy_inc, 9, 2, energy_buf); + char *e = energy_buf; + while ((unsigned char) *e == ' ') ++e; + + mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), power); + mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), e); + #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf(buffer, 20, "%s;%s", power, energy); + snprintf(buffer, 20, "%s;%s", power, e); domoticzSend("dczPowIdx", 0, buffer); } #endif + sum = measurements = 0; } diff --git a/code/espurna/pow.ino b/code/espurna/pow.ino index 41438d72..955352d8 100644 --- a/code/espurna/pow.ino +++ b/code/espurna/pow.ino @@ -16,7 +16,6 @@ Copyright (C) 2016-2017 by Xose Pérez HLW8012 hlw8012; bool _powEnabled = false; -double _energy = 0; // ----------------------------------------------------------------------------- // POW @@ -119,26 +118,8 @@ unsigned int getPowerFactor() { return (int) (100 * hlw8012.getPowerFactor()); } -double getEnergy() { - return _energy; -} - // ----------------------------------------------------------------------------- -void retrieveEnergy() { - unsigned long energy = EEPROM.read(EEPROM_POWER_COUNT + 1); - energy = (energy << 8) + EEPROM.read(EEPROM_POWER_COUNT); - if (energy == 0xFFFF) energy = 0; - _energy = energy; -} - -void saveEnergy() { - unsigned int energy = (int) _energy; - EEPROM.write(EEPROM_POWER_COUNT, energy & 0xFF); - EEPROM.write(EEPROM_POWER_COUNT + 1, (energy >> 8) & 0xFF); - EEPROM.commit(); -} - void powSetup() { // Initialize HLW8012 @@ -163,16 +144,10 @@ void powSetup() { // Retrieve calibration values powRetrieveCalibration(); - // Recover energy reading - retrieveEnergy(); - // API definitions apiRegister("/api/power", "power", [](char * buffer, size_t len) { snprintf(buffer, len, "%d", getActivePower()); }); - apiRegister("/api/energy", "energy", [](char * buffer, size_t len) { - snprintf(buffer, len, "%ld", (unsigned long) _energy); - }); apiRegister("/api/current", "current", [](char * buffer, size_t len) { dtostrf(getCurrent(), len-1, 2, buffer); }); @@ -242,12 +217,15 @@ void powLoop() { reactive = (apparent > power) ? sqrt(apparent * apparent - power * power) : 0; factor = (apparent > 0) ? 100 * power / apparent : 100; if (factor > 100) factor = 100; - double window = (double) POW_REPORT_EVERY * POW_UPDATE_INTERVAL / 1000.0 / 3600.0; - _energy += power * window; - saveEnergy(); + + double energy_inc = (double) power * POW_REPORT_EVERY * POW_UPDATE_INTERVAL / 1000.0 / 3600.0; + char energy_buf[10]; + dtostrf(energy_inc, 9, 2, energy_buf); + char *e = energy_buf; + while ((unsigned char) *e == ' ') ++e; mqttSend(getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), String(power).c_str()); - mqttSend(getSetting("powEnergyTopic", POW_ENERGY_TOPIC).c_str(), String(_energy).c_str()); + mqttSend(getSetting("powEnergyTopic", POW_ENERGY_TOPIC).c_str(), e); mqttSend(getSetting("powCurrentTopic", POW_CURRENT_TOPIC).c_str(), String(current).c_str()); mqttSend(getSetting("powVoltageTopic", POW_VOLTAGE_TOPIC).c_str(), String(voltage).c_str()); mqttSend(getSetting("powAPowerTopic", POW_APOWER_TOPIC).c_str(), String(apparent).c_str()); @@ -257,7 +235,7 @@ void powLoop() { #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf(buffer, 20, "%d;%ld", power, (unsigned long) _energy); + snprintf(buffer, 20, "%d;%s", power, e); domoticzSend("dczPowIdx", 0, buffer); snprintf(buffer, 20, "%d", voltage); domoticzSend("dczVoltIdx", 0, buffer);