diff --git a/README.md b/README.md index 80f2d960..dbaf9fd7 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smar It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries. [![version](https://img.shields.io/badge/version-1.12.5a-brightgreen.svg)](CHANGELOG.md) -![branch](https://img.shields.io/badge/branch-dev-orange.svg) -[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna) -[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard) +![branch](https://img.shields.io/badge/branch-sensors-orange.svg) +[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=sensors)](https://travis-ci.org/xoseperez/espurna) +[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/sensors.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard) [![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE)
[![donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=xose%2eperez%40gmail%2ecom&lc=US&no_note=0¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHostedGuest) diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 6c5f340e..0989bee7 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -481,37 +481,10 @@ void _sensorInit() { for (unsigned char i=0; i<_sensors.size(); i++) { - // Do not process and already initialized sensor + // Do not process an already initialized sensor if (_sensors[i]->ready()) continue; DEBUG_MSG_P(PSTR("[SENSOR] Initializing %s\n"), _sensors[i]->description().c_str()); - #if EMON_ANALOG_SUPPORT - - if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) { - - double value; - EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i]; - - if (value = (getSetting("pwrExpectedP", 0).toInt() == 0)) { - value = getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat(); - if (value > 0) sensor->setCurrentRatio(0, value); - } else { - sensor->expectedPower(0, value); - setSetting("pwrRatioC", sensor->getCurrentRatio(0)); - } - - if (getSetting("pwrResetCalibration", 0).toInt() == 1) { - sensor->setCurrentRatio(0, EMON_CURRENT_RATIO); - delSetting("pwrRatioC"); - } - - sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt()); - - - } - - #endif // EMON_ANALOG_SUPPORT - // Force sensor to reload config _sensors[i]->begin(); if (!_sensors[i]->ready()) { @@ -555,6 +528,81 @@ void _sensorInit() { _sensorCallback(i, type, payload); }); + // Custom initializations + + #if EMON_ANALOG_SUPPORT + + if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) { + EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i]; + sensor->setCurrentRatio(0, getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat()); + sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt()); + } + + #endif // EMON_ANALOG_SUPPORT + + #if HLW8012_SUPPORT + + if (_sensors[i]->getID() == SENSOR_HLW8012_ID) { + + HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i]; + + double value; + + value = getSetting("pwrRatioC", 0).toFloat(); + if (value > 0) sensor->setCurrentRatio(value); + + value = getSetting("pwrRatioV", 0).toFloat(); + if (value > 0) sensor->setVoltageRatio(value); + + value = getSetting("pwrRatioP", 0).toFloat(); + if (value > 0) sensor->setPowerRatio(value); + + } + + #endif // HLW8012_SUPPORT + + } + +} + +void _sensorConfigure() { + + // General sensor settings + _sensor_read_interval = 1000 * constrain(getSetting("snsRead", SENSOR_READ_INTERVAL).toInt(), SENSOR_READ_MIN_INTERVAL, SENSOR_READ_MAX_INTERVAL); + _sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY); + _sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1; + _sensor_power_units = getSetting("powerUnits", SENSOR_POWER_UNITS).toInt(); + _sensor_energy_units = getSetting("energyUnits", SENSOR_ENERGY_UNITS).toInt(); + _sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt(); + _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat(); + _sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat(); + + // Specific sensor settings + for (unsigned char i=0; i<_sensors.size(); i++) { + + #if EMON_ANALOG_SUPPORT + + if (_sensors[i]->getID() == SENSOR_EMON_ANALOG_ID) { + + double value; + EmonAnalogSensor * sensor = (EmonAnalogSensor *) _sensors[i]; + + if (value = getSetting("pwrExpectedP", 0).toInt()) { + sensor->expectedPower(0, value); + setSetting("pwrRatioC", sensor->getCurrentRatio(0)); + } + + if (getSetting("pwrResetCalibration", 0).toInt() == 1) { + sensor->setCurrentRatio(0, EMON_CURRENT_RATIO); + delSetting("pwrRatioC"); + } + + sensor->setVoltage(getSetting("pwrVoltage", EMON_MAINS_VOLTAGE).toInt()); + + } + + #endif // EMON_ANALOG_SUPPORT + #if HLW8012_SUPPORT @@ -566,25 +614,16 @@ void _sensorInit() { if (value = getSetting("pwrExpectedC", 0).toFloat()) { sensor->expectedCurrent(value); setSetting("pwrRatioC", sensor->getCurrentRatio()); - } else { - value = getSetting("pwrRatioC", 0).toFloat(); - if (value > 0) sensor->setCurrentRatio(value); } if (value = getSetting("pwrExpectedV", 0).toInt()) { sensor->expectedVoltage(value); setSetting("pwrRatioV", sensor->getVoltageRatio()); - } else { - value = getSetting("pwrRatioV", 0).toFloat(); - if (value > 0) sensor->setVoltageRatio(value); } if (value = getSetting("pwrExpectedP", 0).toInt()) { sensor->expectedPower(value); setSetting("pwrRatioP", sensor->getPowerRatio()); - } else { - value = getSetting("pwrRatioP", 0).toFloat(); - if (value > 0) sensor->setPowerRatio(value); } if (getSetting("pwrResetCalibration", 0).toInt() == 1) { @@ -600,20 +639,6 @@ void _sensorInit() { } -} - -void _sensorConfigure() { - - // General sensor settings - _sensor_read_interval = 1000 * constrain(getSetting("snsRead", SENSOR_READ_INTERVAL).toInt(), SENSOR_READ_MIN_INTERVAL, SENSOR_READ_MAX_INTERVAL); - _sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY); - _sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1; - _sensor_power_units = getSetting("powerUnits", SENSOR_POWER_UNITS).toInt(); - _sensor_energy_units = getSetting("energyUnits", SENSOR_ENERGY_UNITS).toInt(); - _sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt(); - _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat(); - _sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat(); - // Update filter sizes for (unsigned char i=0; i<_magnitudes.size(); i++) { _magnitudes[i].filter->resize(_sensor_report_every); @@ -624,7 +649,7 @@ void _sensorConfigure() { delSetting("pwrExpectedC"); delSetting("pwrExpectedV"); delSetting("pwrResetCalibration"); - //saveSettings(); + saveSettings(); }