|
@ -7,21 +7,21 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
#include <vector>
|
|
|
#include "libs/AggregatorMedian.h"
|
|
|
|
|
|
#include "libs/AggregatorMovingAverage.h"
|
|
|
|
|
|
#include "sensors/SensorBase.h"
|
|
|
|
|
|
|
|
|
#include "filters/MedianFilter.h"
|
|
|
|
|
|
#include "filters/MovingAverageFilter.h"
|
|
|
|
|
|
#include "sensors/BaseSensor.h"
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
typedef struct { |
|
|
SensorBase * sensor; |
|
|
|
|
|
|
|
|
BaseSensor * sensor; |
|
|
unsigned char local; // Local index in its provider
|
|
|
unsigned char local; // Local index in its provider
|
|
|
magnitude_t type; // Type of measurement
|
|
|
magnitude_t 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 filtered; // Filtered (averaged) value
|
|
|
AggregatorBase * filter; // Filter object
|
|
|
|
|
|
|
|
|
BaseFilter * filter; // Filter object
|
|
|
} sensor_magnitude_t; |
|
|
} sensor_magnitude_t; |
|
|
|
|
|
|
|
|
std::vector<SensorBase *> _sensors; |
|
|
|
|
|
|
|
|
std::vector<BaseSensor *> _sensors; |
|
|
std::vector<sensor_magnitude_t> _magnitudes; |
|
|
std::vector<sensor_magnitude_t> _magnitudes; |
|
|
|
|
|
|
|
|
unsigned char _counts[MAGNITUDE_MAX]; |
|
|
unsigned char _counts[MAGNITUDE_MAX]; |
|
@ -29,10 +29,6 @@ bool _sensor_realtime = API_REAL_TIME_VALUES; |
|
|
unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS; |
|
|
unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS; |
|
|
double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION; |
|
|
double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION; |
|
|
|
|
|
|
|
|
#if DHT_SUPPORT
|
|
|
|
|
|
#include "sensors/SensorDHT.h"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// Private
|
|
|
// Private
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
@ -42,6 +38,8 @@ String _sensorTopic(magnitude_t type) { |
|
|
return String(SENSOR_TEMPERATURE_TOPIC); |
|
|
return String(SENSOR_TEMPERATURE_TOPIC); |
|
|
} else if (type == MAGNITUDE_HUMIDITY) { |
|
|
} else if (type == MAGNITUDE_HUMIDITY) { |
|
|
return String(SENSOR_HUMIDITY_TOPIC); |
|
|
return String(SENSOR_HUMIDITY_TOPIC); |
|
|
|
|
|
} else if (type == MAGNITUDE_ANALOG) { |
|
|
|
|
|
return String(SENSOR_ANALOG_TOPIC); |
|
|
} |
|
|
} |
|
|
return String(SENSOR_UNKNOWN_TOPIC); |
|
|
return String(SENSOR_UNKNOWN_TOPIC); |
|
|
} |
|
|
} |
|
@ -51,6 +49,8 @@ unsigned char _sensorDecimals(magnitude_t type) { |
|
|
return SENSOR_TEMPERATURE_DECIMALS; |
|
|
return SENSOR_TEMPERATURE_DECIMALS; |
|
|
} else if (type == MAGNITUDE_HUMIDITY) { |
|
|
} else if (type == MAGNITUDE_HUMIDITY) { |
|
|
return SENSOR_HUMIDITY_DECIMALS; |
|
|
return SENSOR_HUMIDITY_DECIMALS; |
|
|
|
|
|
} else if (type == MAGNITUDE_ANALOG) { |
|
|
|
|
|
return SENSOR_ANALOG_DECIMALS; |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
@ -86,15 +86,19 @@ void _sensorConfigure() { |
|
|
|
|
|
|
|
|
void _sensorWebSocketOnSend(JsonObject& root) { |
|
|
void _sensorWebSocketOnSend(JsonObject& root) { |
|
|
|
|
|
|
|
|
|
|
|
char buffer[10]; |
|
|
bool hasTemperature = false; |
|
|
bool hasTemperature = false; |
|
|
|
|
|
|
|
|
JsonArray& sensors = root.createNestedArray("sensors"); |
|
|
JsonArray& sensors = root.createNestedArray("sensors"); |
|
|
for (unsigned char i=0; i<_magnitudes.size(); i++) { |
|
|
for (unsigned char i=0; i<_magnitudes.size(); i++) { |
|
|
|
|
|
|
|
|
sensor_magnitude_t magnitude = _magnitudes[i]; |
|
|
sensor_magnitude_t magnitude = _magnitudes[i]; |
|
|
|
|
|
unsigned char decimals = _sensorDecimals(magnitude.type); |
|
|
|
|
|
dtostrf(magnitude.current, 1-sizeof(buffer), decimals, buffer); |
|
|
|
|
|
|
|
|
JsonObject& sensor = sensors.createNestedObject(); |
|
|
JsonObject& sensor = sensors.createNestedObject(); |
|
|
sensor["type"] = int(magnitude.type); |
|
|
sensor["type"] = int(magnitude.type); |
|
|
sensor["value"] = magnitude.current; |
|
|
|
|
|
|
|
|
sensor["value"] = String(buffer); |
|
|
sensor["units"] = _sensorUnits(magnitude.type); |
|
|
sensor["units"] = _sensorUnits(magnitude.type); |
|
|
sensor["description"] = magnitude.sensor->slot(magnitude.local); |
|
|
sensor["description"] = magnitude.sensor->slot(magnitude.local); |
|
|
|
|
|
|
|
@ -138,18 +142,21 @@ void sensorSetup() { |
|
|
|
|
|
|
|
|
// Load sensors
|
|
|
// Load sensors
|
|
|
#if DHT_SUPPORT
|
|
|
#if DHT_SUPPORT
|
|
|
{ |
|
|
|
|
|
_sensors.push_back(new SensorDHT(DHT_PIN, DHT_TYPE)); |
|
|
|
|
|
|
|
|
#include "sensors/DHTSensor.h"
|
|
|
|
|
|
_sensors.push_back(new DHTSensor(DHT_PIN, DHT_TYPE)); |
|
|
#if DHT_PULLUP
|
|
|
#if DHT_PULLUP
|
|
|
pinMode(DHT_PIN, INPUT_PULLUP); |
|
|
pinMode(DHT_PIN, INPUT_PULLUP); |
|
|
#endif
|
|
|
#endif
|
|
|
} |
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#if ANALOG_SUPPORT
|
|
|
|
|
|
#include "sensors/AnalogSensor.h"
|
|
|
|
|
|
_sensors.push_back(new AnalogSensor(ANALOG_PIN)); |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
// Read magnitudes
|
|
|
// Read magnitudes
|
|
|
for (unsigned char i=0; i<_sensors.size(); i++) { |
|
|
for (unsigned char i=0; i<_sensors.size(); i++) { |
|
|
|
|
|
|
|
|
SensorBase * sensor = _sensors[i]; |
|
|
|
|
|
|
|
|
BaseSensor * sensor = _sensors[i]; |
|
|
DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str()); |
|
|
DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str()); |
|
|
|
|
|
|
|
|
for (unsigned char k=0; k<sensor->count(); k++) { |
|
|
for (unsigned char k=0; k<sensor->count(); k++) { |
|
@ -164,9 +171,9 @@ void sensorSetup() { |
|
|
new_magnitude.current = 0; |
|
|
new_magnitude.current = 0; |
|
|
new_magnitude.filtered = 0; |
|
|
new_magnitude.filtered = 0; |
|
|
if (type == MAGNITUDE_EVENTS) { |
|
|
if (type == MAGNITUDE_EVENTS) { |
|
|
new_magnitude.filter = new AggregatorMovingAverage(SENSOR_REPORT_EVERY); |
|
|
|
|
|
|
|
|
new_magnitude.filter = new MovingAverageFilter(SENSOR_REPORT_EVERY); |
|
|
} else { |
|
|
} else { |
|
|
new_magnitude.filter = new AggregatorMedian(); |
|
|
|
|
|
|
|
|
new_magnitude.filter = new MedianFilter(); |
|
|
} |
|
|
} |
|
|
_magnitudes.push_back(new_magnitude); |
|
|
_magnitudes.push_back(new_magnitude); |
|
|
|
|
|
|
|
|