diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 88569882..41bacc96 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -156,7 +156,7 @@ //-------------------------------------------------------------------------------- #ifndef EMON_ANALOG_SUPPORT -#define EMON_ANALOG_SUPPORT 0 // Do not build support by default +#define EMON_ANALOG_SUPPORT 1 // Do not build support by default #endif #define EMON_ANALOG_CURRENT_RATIO 30 // Current ratio in the clamp (30V/1A) diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 4d6c9b35..ae44d91e 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -50,7 +50,7 @@ class EmonADC121Sensor : public EmonSensor { #endif // warmup - read(_address); + read(_address, _pivot); } @@ -90,11 +90,10 @@ class EmonADC121Sensor : public EmonSensor { // Cache the value static unsigned long last = 0; - static double current = 0; if ((last == 0) || (millis() - last > 1000)) { - current = read(0, _pivot); + _current = read(0, _pivot); #if EMON_REPORT_ENERGY - _energy += (current * _voltage * (millis() - last) / 1000); + _energy += (_current * _voltage * (millis() - last) / 1000); #endif last = millis(); } @@ -102,10 +101,10 @@ class EmonADC121Sensor : public EmonSensor { // Report unsigned char i = 0; #if EMON_REPORT_CURRENT - if (index == i++) return current; + if (index == i++) return _current; #endif #if EMON_REPORT_POWER - if (index == i++) return current * _voltage; + if (index == i++) return _current * _voltage; #endif #if EMON_REPORT_ENERGY if (index == i) return _energy; @@ -146,6 +145,7 @@ class EmonADC121Sensor : public EmonSensor { unsigned char _address; double _pivot = 0; + double _current = 0; #if EMON_REPORT_ENERGY unsigned long _energy = 0; #endif diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index 85e2f66b..8e83dee4 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -22,7 +22,7 @@ class EmonAnalogSensor : public EmonSensor { pinMode(gpio, INPUT); // warmup - read(_gpio); + read(_gpio, _pivot); } @@ -62,11 +62,10 @@ class EmonAnalogSensor : public EmonSensor { // Cache the value static unsigned long last = 0; - static double current = 0; if ((last == 0) || (millis() - last > 1000)) { - current = read(0, _pivot); + _current = read(0, _pivot); #if EMON_REPORT_ENERGY - _energy += (current * _voltage * (millis() - last) / 1000); + _energy += (_current * _voltage * (millis() - last) / 1000); #endif last = millis(); } @@ -74,10 +73,10 @@ class EmonAnalogSensor : public EmonSensor { // Report unsigned char i=0; #if EMON_REPORT_CURRENT - if (index == i++) return current; + if (index == i++) return _current; #endif #if EMON_REPORT_POWER - if (index == i++) return current * _voltage; + if (index == i++) return _current * _voltage; #endif #if EMON_REPORT_ENERGY if (index == i) return _energy; @@ -96,6 +95,7 @@ class EmonAnalogSensor : public EmonSensor { unsigned char _gpio; double _pivot = 0; + double _current; #if EMON_REPORT_ENERGY unsigned long _energy = 0; #endif