Browse Source

Added several checks

fastled
Xose Pérez 7 years ago
parent
commit
b0c1425044
3 changed files with 15 additions and 14 deletions
  1. +1
    -1
      code/espurna/power.h
  2. +5
    -4
      code/espurna/power.ino
  3. +9
    -9
      code/espurna/power_hlw8012.ino

+ 1
- 1
code/espurna/power.h View File

@ -15,7 +15,7 @@ class MedianFilter {
} }
virtual void reset() { virtual void reset() {
_data[0] = _data[_pointer-1];
if (_pointer > 1) _data[0] = _data[_pointer-1];
_pointer = 1; _pointer = 1;
for (unsigned char i=1; i<=_size; i++) _data[i] = 0; for (unsigned char i=1; i<=_size; i++) _data[i] = 0;
} }


+ 5
- 4
code/espurna/power.ino View File

@ -99,6 +99,7 @@ void _powerRead() {
double active = _powerActivePower(); double active = _powerActivePower();
double reactive = (apparent > active) ? sqrt(apparent * apparent - active * active) : 0; double reactive = (apparent > active) ? sqrt(apparent * apparent - active * active) : 0;
double factor = (apparent > 0) ? active / apparent : 1; double factor = (apparent > 0) ? active / apparent : 1;
if (factor > 1) factor = 1;
#endif #endif
// Filters // Filters
@ -172,10 +173,10 @@ void _powerReport() {
_power_ready = true; _power_ready = true;
char buf_current[10]; char buf_current[10];
dtostrf(_power_current, 6, POWER_CURRENT_PRECISION, buf_current);
dtostrf(_power_current, -9, POWER_CURRENT_PRECISION, buf_current);
double energy_delta = _power_active * POWER_ENERGY_FACTOR; double energy_delta = _power_active * POWER_ENERGY_FACTOR;
char buf_energy[10]; char buf_energy[10];
dtostrf(energy_delta, 6, POWER_CURRENT_PRECISION, buf_energy);
dtostrf(energy_delta, -9, POWER_CURRENT_PRECISION, buf_energy);
{ {
mqttSend(MQTT_TOPIC_CURRENT, buf_current); mqttSend(MQTT_TOPIC_CURRENT, buf_current);
@ -190,7 +191,7 @@ void _powerReport() {
} }
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
{
if (domoticzEnabled()) {
char buffer[20]; char buffer[20];
snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _power_active, buf_energy); snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _power_active, buf_energy);
domoticzSend("dczPowIdx", 0, buffer); domoticzSend("dczPowIdx", 0, buffer);
@ -205,7 +206,7 @@ void _powerReport() {
#endif #endif
#if INFLUXDB_SUPPORT #if INFLUXDB_SUPPORT
{
if (influxdbEnabled()) {
influxDBSend(MQTT_TOPIC_CURRENT, buf_current); influxDBSend(MQTT_TOPIC_CURRENT, buf_current);
influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
influxDBSend(MQTT_TOPIC_ENERGY, buf_energy); influxDBSend(MQTT_TOPIC_ENERGY, buf_energy);


+ 9
- 9
code/espurna/power_hlw8012.ino View File

@ -30,7 +30,7 @@ void ICACHE_RAM_ATTR _hlw_cf_isr() {
_hlw8012.cf_interrupt(); _hlw8012.cf_interrupt();
} }
void _hlwSetCalibration() {
void _hlwRestoreCalibration() {
double value; double value;
value = getSetting("pwrRatioP", 0).toFloat(); value = getSetting("pwrRatioP", 0).toFloat();
if (value > 0) _hlw8012.setPowerMultiplier(value); if (value > 0) _hlw8012.setPowerMultiplier(value);
@ -40,7 +40,7 @@ void _hlwSetCalibration() {
if (value > 0) _hlw8012.setVoltageMultiplier(value); if (value > 0) _hlw8012.setVoltageMultiplier(value);
} }
void _hlwGetCalibration() {
void _hlwPersistCalibration() {
setSetting("pwrRatioP", _hlw8012.getPowerMultiplier()); setSetting("pwrRatioP", _hlw8012.getPowerMultiplier());
setSetting("pwrRatioC", _hlw8012.getCurrentMultiplier()); setSetting("pwrRatioC", _hlw8012.getCurrentMultiplier());
setSetting("pwrRatioV", _hlw8012.getVoltageMultiplier()); setSetting("pwrRatioV", _hlw8012.getVoltageMultiplier());
@ -90,17 +90,19 @@ void _powerCalibrateProvider(unsigned char magnitude, double value) {
if (magnitude == POWER_MAGNITUDE_ACTIVE) _hlw8012.expectedActivePower(value); if (magnitude == POWER_MAGNITUDE_ACTIVE) _hlw8012.expectedActivePower(value);
if (magnitude == POWER_MAGNITUDE_CURRENT) _hlw8012.expectedCurrent(value); if (magnitude == POWER_MAGNITUDE_CURRENT) _hlw8012.expectedCurrent(value);
if (magnitude == POWER_MAGNITUDE_VOLTAGE) _hlw8012.expectedVoltage(value); if (magnitude == POWER_MAGNITUDE_VOLTAGE) _hlw8012.expectedVoltage(value);
_hlwGetCalibration();
_hlwPersistCalibration();
} }
void _powerResetCalibrationProvider() { void _powerResetCalibrationProvider() {
_hlw8012.resetMultipliers(); _hlw8012.resetMultipliers();
_hlwGetCalibration();
delSetting("pwrRatioC");
delSetting("pwrRatioV");
delSetting("pwrRatioP");
saveSettings();
} }
void _powerConfigureProvider() { void _powerConfigureProvider() {
_hlwSetCalibration();
_hlwGetCalibration();
// Nothing to do
} }
void _powerSetupProvider() { void _powerSetupProvider() {
@ -124,7 +126,7 @@ void _powerSetupProvider() {
// * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012 // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
_hlw8012.setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN); _hlw8012.setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
_powerConfigureProvider();
_hlwRestoreCalibration();
_power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) { _power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
powerEnabled(true); powerEnabled(true);
@ -152,8 +154,6 @@ void _powerLoopProvider(bool before) {
} }
} else {
} }
} }


Loading…
Cancel
Save