Browse Source

Option to report energy based on delta since last report (#369)

ech1560
Xose Pérez 6 years ago
parent
commit
8c7aede92c
2 changed files with 27 additions and 6 deletions
  1. +4
    -0
      code/espurna/config/sensors.h
  2. +23
    -6
      code/espurna/sensor.ino

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

@ -36,6 +36,10 @@
#define HUMIDITY_MIN_CHANGE 0 // Minimum humidity change to report #define HUMIDITY_MIN_CHANGE 0 // Minimum humidity change to report
#endif #endif
#ifndef ENERGY_MAX_CHANGE
#define ENERGY_MAX_CHANGE 0 // Maximum energy change to report (if >0 it will allways report when delta(E) is greater than this)
#endif
#ifndef SENSOR_SAVE_EVERY #ifndef SENSOR_SAVE_EVERY
#define SENSOR_SAVE_EVERY 0 // Save accumulating values to EEPROM (atm only energy) #define SENSOR_SAVE_EVERY 0 // Save accumulating values to EEPROM (atm only energy)
// A 0 means do not save and it's the default value // A 0 means do not save and it's the default value


+ 23
- 6
code/espurna/sensor.ino View File

@ -22,9 +22,9 @@ typedef struct {
unsigned char type; // Type of measurement unsigned char type; // Type of measurement
unsigned char global; // Global index in its type unsigned char global; // Global index in its type
double current; // Current (last) value, unfiltered double current; // Current (last) value, unfiltered
double filtered; // Filtered (averaged) value
double reported; // Last reported value double reported; // Last reported value
double min_change; // Minimum value change to report double min_change; // Minimum value change to report
double max_change; // Maximum value change to report
} sensor_magnitude_t; } sensor_magnitude_t;
std::vector<BaseSensor *> _sensors; std::vector<BaseSensor *> _sensors;
@ -242,7 +242,7 @@ void _sensorAPISetup() {
apiRegister(topic.c_str(), [magnitude_id](char * buffer, size_t len) { apiRegister(topic.c_str(), [magnitude_id](char * buffer, size_t len) {
sensor_magnitude_t magnitude = _magnitudes[magnitude_id]; sensor_magnitude_t magnitude = _magnitudes[magnitude_id];
unsigned char decimals = _magnitudeDecimals(magnitude.type); unsigned char decimals = _magnitudeDecimals(magnitude.type);
double value = _sensor_realtime ? magnitude.current : magnitude.filtered;
double value = _sensor_realtime ? magnitude.current : magnitude.reported;
dtostrf(value, 1-len, decimals, buffer); dtostrf(value, 1-len, decimals, buffer);
}); });
@ -650,9 +650,19 @@ void _sensorInit() {
new_magnitude.type = type; new_magnitude.type = type;
new_magnitude.global = _counts[type]; new_magnitude.global = _counts[type];
new_magnitude.current = 0; new_magnitude.current = 0;
new_magnitude.filtered = 0;
new_magnitude.reported = 0; new_magnitude.reported = 0;
new_magnitude.min_change = 0; new_magnitude.min_change = 0;
new_magnitude.max_change = 0;
// TODO: find a proper way to extend this to min/max of any magnitude
if (MAGNITUDE_ENERGY == type) {
new_magnitude.max_change = getSetting("eneMaxDelta", ENERGY_MAX_CHANGE).toFloat();
} else if (MAGNITUDE_TEMPERATURE == type) {
new_magnitude.min_change = getSetting("tmpMinDelta", TEMPERATURE_MIN_CHANGE).toFloat();
} else if (MAGNITUDE_HUMIDITY == type) {
new_magnitude.min_change = getSetting("humMinDelta", HUMIDITY_MIN_CHANGE).toFloat();
}
if (MAGNITUDE_ENERGY == type) { if (MAGNITUDE_ENERGY == type) {
new_magnitude.filter = new LastFilter(); new_magnitude.filter = new LastFilter();
} else if (MAGNITUDE_DIGITAL == type) { } else if (MAGNITUDE_DIGITAL == type) {
@ -663,6 +673,7 @@ void _sensorInit() {
new_magnitude.filter = new MedianFilter(); new_magnitude.filter = new MedianFilter();
} }
new_magnitude.filter->resize(_sensor_report_every); new_magnitude.filter->resize(_sensor_report_every);
_magnitudes.push_back(new_magnitude); _magnitudes.push_back(new_magnitude);
DEBUG_MSG_P(PSTR("[SENSOR] -> %s:%d\n"), magnitudeTopic(type).c_str(), _counts[type]); DEBUG_MSG_P(PSTR("[SENSOR] -> %s:%d\n"), magnitudeTopic(type).c_str(), _counts[type]);
@ -1190,12 +1201,18 @@ void sensorLoop() {
// (we do it every _sensor_report_every readings) // (we do it every _sensor_report_every readings)
// ------------------------------------------------------------- // -------------------------------------------------------------
if (0 == report_count) {
bool report = (0 == report_count);
if ((MAGNITUDE_ENERGY == magnitude.type) && (magnitude.max_change > 0)) {
// for MAGNITUDE_ENERGY, filtered value is last value
double value = _magnitudeProcess(magnitude.type, current);
report = (fabs(value - magnitude.reported) >= magnitude.max_change);
} // if ((MAGNITUDE_ENERGY == magnitude.type) && (magnitude.max_change > 0))
if (report) {
filtered = magnitude.filter->result(); filtered = magnitude.filter->result();
magnitude.filter->reset();
filtered = _magnitudeProcess(magnitude.type, filtered); filtered = _magnitudeProcess(magnitude.type, filtered);
_magnitudes[i].filtered = filtered;
magnitude.filter->reset();
// Check if there is a minimum change threshold to report // Check if there is a minimum change threshold to report
if (fabs(filtered - magnitude.reported) >= magnitude.min_change) { if (fabs(filtered - magnitude.reported) >= magnitude.min_change) {


Loading…
Cancel
Save