Browse Source

Improvement on ANALOG_SENSOR (#1326)

* Testing branch

* Added ability (by define) to scale analog sensor, also with number of decimals (it was formerly zero)
Useful when running on batteries to check the battery voltage just 
adding the appropriate resistor

* Reverting DHTSensor.h file

* Trying again to revert DHTSensor.h

* Using macro DEBUG_MSG

* fix method types
alexa
Carlos Iván Conde Martín 6 years ago
committed by Max Prokhorov
parent
commit
b016167cba
3 changed files with 65 additions and 1 deletions
  1. +19
    -0
      code/espurna/config/sensors.h
  2. +4
    -0
      code/espurna/sensor.ino
  3. +42
    -1
      code/espurna/sensors/AnalogSensor.h

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

@ -99,6 +99,25 @@
#define ANALOG_DELAY 0 // Delay between samples in micros #define ANALOG_DELAY 0 // Delay between samples in micros
#endif #endif
//Use the following to perform scaling of raw analog values
// scaledRead = ( factor * rawRead ) + offset
//
//Please take note that the offset is not affected by the scaling factor
#ifndef ANALOG_FACTOR
#define ANALOG_FACTOR 1.0 // Multiply raw reading by this factor
#endif
#ifndef ANALOG_OFFSET
#define ANALOG_OFFSET 0.0 // Add this offset to *scaled* value
#endif
// Round to this number of decimals
#ifndef ANALOG_DECIMALS
#define ANALOG_DECIMALS 2
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// BH1750 // BH1750
// Enable support by passing BH1750_SUPPORT=1 build flag // Enable support by passing BH1750_SUPPORT=1 build flag


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

@ -56,6 +56,7 @@ unsigned char _magnitudeDecimals(unsigned char type) {
// Hardcoded decimals (these should be linked to the unit, instead of the magnitude) // Hardcoded decimals (these should be linked to the unit, instead of the magnitude)
if (type == MAGNITUDE_ANALOG) return ANALOG_DECIMALS;
if (type == MAGNITUDE_ENERGY || if (type == MAGNITUDE_ENERGY ||
type == MAGNITUDE_ENERGY_DELTA) { type == MAGNITUDE_ENERGY_DELTA) {
if (_sensor_energy_units == ENERGY_KWH) return 3; if (_sensor_energy_units == ENERGY_KWH) return 3;
@ -414,6 +415,9 @@ void _sensorLoad() {
AnalogSensor * sensor = new AnalogSensor(); AnalogSensor * sensor = new AnalogSensor();
sensor->setSamples(ANALOG_SAMPLES); sensor->setSamples(ANALOG_SAMPLES);
sensor->setDelay(ANALOG_DELAY); sensor->setDelay(ANALOG_DELAY);
//CICM For analog scaling
sensor->setFactor(ANALOG_FACTOR);
sensor->setOffset(ANALOG_OFFSET);
_sensors.push_back(sensor); _sensors.push_back(sensor);
} }
#endif #endif


+ 42
- 1
code/espurna/sensors/AnalogSensor.h View File

@ -35,6 +35,16 @@ class AnalogSensor : public BaseSensor {
_micros = micros; _micros = micros;
} }
void setFactor(double factor) {
//DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(factor,6).c_str());
_factor = factor;
}
void setOffset(double offset) {
//DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(offset,6).c_str());
_offset = offset;
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
unsigned int getSamples() { unsigned int getSamples() {
@ -45,6 +55,14 @@ class AnalogSensor : public BaseSensor {
return _micros; return _micros;
} }
double getFactor() {
return _factor;
}
double getOffset() {
return _offset;
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Sensor API // Sensor API
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -77,6 +95,7 @@ class AnalogSensor : public BaseSensor {
} }
// Current value for slot # index // Current value for slot # index
// Changed return type as moving to scaled value
double value(unsigned char index) { double value(unsigned char index) {
if (index == 0) return _read(); if (index == 0) return _read();
return 0; return 0;
@ -84,7 +103,9 @@ class AnalogSensor : public BaseSensor {
protected: protected:
unsigned int _read() {
//CICM: this should be for raw values
// renaming protected function "_read" to "_rawRead"
unsigned int _rawRead() {
if (1 == _samples) return analogRead(0); if (1 == _samples) return analogRead(0);
unsigned long sum = 0; unsigned long sum = 0;
for (unsigned int i=0; i<_samples; i++) { for (unsigned int i=0; i<_samples; i++) {
@ -94,8 +115,28 @@ class AnalogSensor : public BaseSensor {
return sum / _samples; return sum / _samples;
} }
//CICM: and proper read should be scalable and thus needs sign
//and decimal part
double _read() {
//Raw measure could also be a class variable with getter so that can
//be reported through MQTT, ...
unsigned int rawValue;
double scaledValue;
// Debugging doubles to string
//DEBUG_MSG(("[ANALOG_SENSOR] Started standard read, factor: %s , offset: %s, decimals: %d \n"), String(_factor).c_str(), String(_offset).c_str(), ANALOG_DECIMALS);
rawValue = _rawRead();
//DEBUG_MSG(("[ANALOG_SENSOR] Raw read received: %d \n"), rawValue);
scaledValue = _factor*rawValue + _offset;
//DEBUG_MSG(("[ANALOG_SENSOR] Scaled value result: %s \n"), String(scaledValue).c_str());
return scaledValue;
}
unsigned int _samples = 1; unsigned int _samples = 1;
unsigned long _micros = 0; unsigned long _micros = 0;
//CICM: for scaling and offset, also with getters and setters
double _factor = 1.0;
double _offset = 0.0;
}; };


Loading…
Cancel
Save