Browse Source

Removed voltage report from EMON module. Refactor

fastled
Xose Pérez 7 years ago
parent
commit
f47128ac85
5 changed files with 20 additions and 6 deletions
  1. +1
    -0
      code/espurna/config/sensors.h
  2. +14
    -4
      code/espurna/emon.ino
  3. +4
    -0
      code/espurna/pow.ino
  4. +0
    -1
      code/espurna/web.ino
  5. +1
    -1
      code/html/index.html

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

@ -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)


+ 14
- 4
code/espurna/emon.ino View File

@ -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;
}


+ 4
- 0
code/espurna/pow.ino View File

@ -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;


+ 0
- 1
code/espurna/web.ino View File

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


+ 1
- 1
code/html/index.html View File

@ -415,7 +415,7 @@
<div class="pure-u-1 pure-u-sm-5-8 hint center">Set to 0 to disable notifications.</div>
</div>
<div class="pure-g module module-pow module-emon">
<div class="pure-g module module-pow">
<label class="pure-u-1 pure-u-sm-1-4" for="dczVoltIdx">Voltage IDX</label>
<div class="pure-u-1 pure-u-sm-1-8"><input class="pure-u-sm-23-24" name="dczVoltIdx" type="number" min="0" tabindex="37" data="0" /></div>
<div class="pure-u-1 pure-u-sm-5-8 hint center">Set to 0 to disable notifications.</div>


Loading…
Cancel
Save