Browse Source

Reporting energy incrementals (domoticz, MQTT). Removed energy API entry point

fastled
Xose Pérez 7 years ago
parent
commit
ad1b62776b
4 changed files with 22 additions and 67 deletions
  1. +1
    -1
      code/espurna/config/general.h
  2. +1
    -1
      code/espurna/config/sensors.h
  3. +12
    -35
      code/espurna/emon.ino
  4. +8
    -30
      code/espurna/pow.ino

+ 1
- 1
code/espurna/config/general.h View File

@ -12,7 +12,7 @@
//--------------------------------------------------------------------------------
#define EEPROM_RELAY_STATUS 0
#define EEPROM_POWER_COUNT 1
#define EEPROM_ENERGY_COUNT 1
//--------------------------------------------------------------------------------
// RELAY


+ 1
- 1
code/espurna/config/sensors.h View File

@ -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"
//--------------------------------------------------------------------------------


+ 12
- 35
code/espurna/emon.ino View File

@ -25,7 +25,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
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;
}


+ 8
- 30
code/espurna/pow.ino View File

@ -16,7 +16,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
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, ( class="kt">unsigned long) _energy);
snprintf(buffer, 20, "%d;%s", power, e);
domoticzSend("dczPowIdx", 0, buffer);
snprintf(buffer, 20, "%d", voltage);
domoticzSend("dczVoltIdx", 0, buffer);


Loading…
Cancel
Save