Browse Source

Simple digital sensor

fastled
Xose Pérez 7 years ago
parent
commit
cb3297183d
10 changed files with 1963 additions and 1871 deletions
  1. +1
    -0
      code/espurna/config/general.h
  2. +24
    -10
      code/espurna/config/sensors.h
  3. BIN
      code/espurna/data/index.html.gz
  4. +18
    -0
      code/espurna/filters/MaxFilter.h
  5. +11
    -2
      code/espurna/sensor.ino
  6. +2
    -1
      code/espurna/sensors/AnalogSensor.h
  7. +1
    -0
      code/espurna/sensors/BaseSensor.h
  8. +55
    -0
      code/espurna/sensors/DigitalSensor.h
  9. +1841
    -1842
      code/espurna/static/index.html.gz.h
  10. +10
    -16
      code/html/custom.js

+ 1
- 0
code/espurna/config/general.h View File

@ -794,6 +794,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define SENSOR_PM2dot5_TOPIC "pm2dot5" #define SENSOR_PM2dot5_TOPIC "pm2dot5"
#define SENSOR_PM10_TOPIC "pm10" #define SENSOR_PM10_TOPIC "pm10"
#define SENSOR_ANALOG_TOPIC "analog" #define SENSOR_ANALOG_TOPIC "analog"
#define SENSOR_DIGITAL_TOPIC "digital"
#define SENSOR_EVENTS_TOPIC "events" #define SENSOR_EVENTS_TOPIC "events"
#define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT) #define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT)


+ 24
- 10
code/espurna/config/sensors.h View File

@ -103,6 +103,28 @@
#define DS18B20_RESOLUTION 9 #define DS18B20_RESOLUTION 9
//--------------------------------------------------------------------------------
// Digital sensor
// Enable support by passing DIGITAL_SUPPORT=1 build flag
//--------------------------------------------------------------------------------
#ifndef DIGITAL_SUPPORT
#define DIGITAL_SUPPORT 0
#endif
#ifndef DIGITAL_PIN
#define DIGITAL_PIN 2
#endif
#ifndef DIGITAL_PIN_MODE
#define DIGITAL_PIN_MODE INPUT_PULLUP
#endif
#ifndef DIGITAL_DEFAULT_STATE
#define DIGITAL_DEFAULT_STATE 1
#endif
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Analog sensor // Analog sensor
// Enable support by passing ANALOG_SUPPORT=1 build flag // Enable support by passing ANALOG_SUPPORT=1 build flag
@ -116,12 +138,10 @@
#define ANALOG_PIN 0 #define ANALOG_PIN 0
#endif #endif
#ifndef ANALOG_UPDATE_INTERVAL
#define ANALOG_UPDATE_INTERVAL 60000
#ifndef ANALOG_PIN_MODE
#define ANALOG_PIN_MODE INPUT
#endif #endif
#define ANALOG_TOPIC "analog"
#if ANALOG_SUPPORT #if ANALOG_SUPPORT
#undef ADC_VCC_ENABLED #undef ADC_VCC_ENABLED
#define ADC_VCC_ENABLED 0 #define ADC_VCC_ENABLED 0
@ -148,13 +168,7 @@
#define COUNTER_INTERRUPT_MODE RISING // RISING, FALLING, BOTH #define COUNTER_INTERRUPT_MODE RISING // RISING, FALLING, BOTH
#endif #endif
#ifndef COUNTER_UPDATE_INTERVAL
#define COUNTER_UPDATE_INTERVAL 5000 // Update counter every this millis
#endif
#define COUNTER_REPORT_EVERY 12 // Report counter every this updates (1 minute)
#define COUNTER_DEBOUNCE 50 // Do not register events within less than 10 millis #define COUNTER_DEBOUNCE 50 // Do not register events within less than 10 millis
#define COUNTER_TOPIC "counter" // Default topic for MQTT, API and InfluxDB
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Energy Monitor // Energy Monitor


BIN
code/espurna/data/index.html.gz View File


+ 18
- 0
code/espurna/filters/MaxFilter.h View File

@ -0,0 +1,18 @@
// -----------------------------------------------------------------------------
// Aggregator base class
// -----------------------------------------------------------------------------
#pragma once
#include <vector>
#include "BaseFilter.h"
class MaxFilter : public BaseFilter {
public:
virtual double result() {
return max();
}
};

+ 11
- 2
code/espurna/sensor.ino View File

@ -7,6 +7,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/ */
#include <vector> #include <vector>
#include "filters/MaxFilter.h"
#include "filters/MedianFilter.h" #include "filters/MedianFilter.h"
#include "filters/MovingAverageFilter.h" #include "filters/MovingAverageFilter.h"
#include "sensors/BaseSensor.h" #include "sensors/BaseSensor.h"
@ -49,6 +50,7 @@ String _sensorTopic(magnitude_t type) {
if (type == MAGNITUDE_ENERGY) return String(SENSOR_ENERGY_TOPIC); if (type == MAGNITUDE_ENERGY) return String(SENSOR_ENERGY_TOPIC);
if (type == MAGNITUDE_ENERGY_DELTA) return String(SENSOR_ENERGY_DELTA_TOPIC); if (type == MAGNITUDE_ENERGY_DELTA) return String(SENSOR_ENERGY_DELTA_TOPIC);
if (type == MAGNITUDE_ANALOG) return String(SENSOR_ANALOG_TOPIC); if (type == MAGNITUDE_ANALOG) return String(SENSOR_ANALOG_TOPIC);
if (type == MAGNITUDE_DIGITAL) return String(SENSOR_DIGITAL_TOPIC);
if (type == MAGNITUDE_EVENTS) return String(SENSOR_EVENTS_TOPIC); if (type == MAGNITUDE_EVENTS) return String(SENSOR_EVENTS_TOPIC);
if (type == MAGNITUDE_PM1dot0) return String(SENSOR_PM1dot0_TOPIC); if (type == MAGNITUDE_PM1dot0) return String(SENSOR_PM1dot0_TOPIC);
if (type == MAGNITUDE_PM2dot5) return String(SENSOR_PM2dot5_TOPIC); if (type == MAGNITUDE_PM2dot5) return String(SENSOR_PM2dot5_TOPIC);
@ -252,7 +254,12 @@ void sensorInit() {
#if ANALOG_SUPPORT #if ANALOG_SUPPORT
#include "sensors/AnalogSensor.h" #include "sensors/AnalogSensor.h"
sensorRegister(new AnalogSensor(ANALOG_PIN));
sensorRegister(new AnalogSensor(ANALOG_PIN, ANALOG_PIN_MODE));
#endif
#if DIGITAL_SUPPORT
#include "sensors/DigitalSensor.h"
sensorRegister(new DigitalSensor(DIGITAL_PIN, DIGITAL_PIN_MODE, DIGITAL_DEFAULT_STATE));
#endif #endif
#if EMON_ANALOG_SUPPORT #if EMON_ANALOG_SUPPORT
@ -308,7 +315,9 @@ void sensorSetup() {
new_magnitude.filtered = 0; new_magnitude.filtered = 0;
new_magnitude.reported = 0; new_magnitude.reported = 0;
new_magnitude.min_change = 0; new_magnitude.min_change = 0;
if (type == MAGNITUDE_EVENTS) {
if (type == MAGNITUDE_DIGITAL) {
new_magnitude.filter = new MaxFilter();
} else if (type == MAGNITUDE_EVENTS) {
new_magnitude.filter = new MovingAverageFilter(SENSOR_REPORT_EVERY); new_magnitude.filter = new MovingAverageFilter(SENSOR_REPORT_EVERY);
} else { } else {
new_magnitude.filter = new MedianFilter(); new_magnitude.filter = new MedianFilter();


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

@ -11,8 +11,9 @@ class AnalogSensor : public BaseSensor {
public: public:
AnalogSensor(unsigned char gpio): BaseSensor() {
AnalogSensor(unsigned char gpio, int mode = INPUT): BaseSensor() {
_gpio = gpio; _gpio = gpio;
pinMode(_gpio, mode);
_count = 1; _count = 1;
} }


+ 1
- 0
code/espurna/sensors/BaseSensor.h View File

@ -24,6 +24,7 @@ typedef enum magnitude_t {
MAGNITUDE_POWER_FACTOR, MAGNITUDE_POWER_FACTOR,
MAGNITUDE_ANALOG, MAGNITUDE_ANALOG,
MAGNITUDE_DIGITAL,
MAGNITUDE_EVENTS, MAGNITUDE_EVENTS,
MAGNITUDE_PM1dot0, MAGNITUDE_PM1dot0,


+ 55
- 0
code/espurna/sensors/DigitalSensor.h View File

@ -0,0 +1,55 @@
// -----------------------------------------------------------------------------
// DHT Sensor
// -----------------------------------------------------------------------------
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
class DigitalSensor : public BaseSensor {
public:
DigitalSensor(unsigned char gpio, int mode = INPUT, bool default_state = false): BaseSensor() {
_gpio = gpio;
_default = default_state;
pinMode(_gpio, mode);
_count = 1;
}
// Descriptive name of the sensor
String name() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
return String(buffer);
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
}
// Type for slot # index
magnitude_t type(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return MAGNITUDE_DIGITAL;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
protected:
unsigned char _gpio;
bool _default;
};

+ 1841
- 1842
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 10
- 16
code/html/custom.js View File

@ -28,22 +28,14 @@ function initMessages() {
} }
function sensorType(type) { function sensorType(type) {
if (type == 1) return "Temperature";
if (type == 2) return "Humidity";
if (type == 3) return "Pressure";
if (type == 4) return "Current";
if (type == 5) return "Voltage";
if (type == 6) return "Active Power";
if (type == 7) return "Apparent Power";
if (type == 8) return "Reactive Power";
if (type == 9) return "Energy";
if (type == 10) return "Energy (delta)";
if (type == 11) return "Power Factor";
if (type == 12) return "Analog";
if (type == 13) return "Events";
if (type == 14) return "PM1.0"
if (type == 15) return "PM2.5"
if (type == 16) return "PM10"
var types = [
"Temperature", "Humidity", "Pressure",
"Current", "Voltage", "Active Power", "Apparent Power",
"Reactive Power", "Energy", "Energy (delta)", "Power Factor",
"Analog", "Digital", "Events",
"PM1.0", "PM2.5", "PM10"
];
if (1 <= type && type <= types.length) return types[type-1];
return null; return null;
} }
@ -627,6 +619,8 @@ function rfbSend() {
function processData(data) { function processData(data) {
console.log(data);
// title // title
if ("app_name" in data) { if ("app_name" in data) {
var title = data.app_name; var title = data.app_name;


Loading…
Cancel
Save