Browse Source

Tentative to sensor manifest

fastled
Xose Pérez 6 years ago
parent
commit
c975b11226
16 changed files with 119 additions and 33 deletions
  1. +8
    -0
      code/espurna/config/general.h
  2. +2
    -2
      code/espurna/config/sensors.h
  3. +26
    -5
      code/espurna/sensor.ino
  4. +2
    -2
      code/espurna/sensors/AnalogSensor.h
  5. +52
    -5
      code/espurna/sensors/BMX280Sensor.h
  6. +13
    -3
      code/espurna/sensors/BaseSensor.h
  7. +2
    -2
      code/espurna/sensors/DHTSensor.h
  8. +1
    -1
      code/espurna/sensors/DallasSensor.h
  9. +2
    -2
      code/espurna/sensors/DigitalSensor.h
  10. +1
    -1
      code/espurna/sensors/EmonADC121Sensor.h
  11. +1
    -1
      code/espurna/sensors/EmonADS1X15Sensor.h
  12. +2
    -2
      code/espurna/sensors/EmonAnalogSensor.h
  13. +2
    -2
      code/espurna/sensors/EventSensor.h
  14. +2
    -2
      code/espurna/sensors/MHZ19Sensor.h
  15. +1
    -1
      code/espurna/sensors/PMSX003Sensor.h
  16. +2
    -2
      code/espurna/sensors/SI7021Sensor.h

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

@ -318,6 +318,14 @@ PROGMEM const char* const custom_reset_string[] = {
#define API_BUFFER_SIZE 15 // Size of the buffer for HTTP GET API responses
#define API_REAL_TIME_VALUES 0 // Show filtered/median values by default (0 => median, 1 => real time)
// -----------------------------------------------------------------------------
// UI
// -----------------------------------------------------------------------------
#define UI_TAG_INPUT 0
#define UI_TAG_CHECKBOX 1
#define UI_TAG_SELECT 2
// -----------------------------------------------------------------------------
// MDNS & LLMNR
// -----------------------------------------------------------------------------


+ 2
- 2
code/espurna/config/sensors.h View File

@ -71,7 +71,7 @@
//--------------------------------------------------------------------------------
#ifndef ANALOG_SUPPORT
#define ANALOG_SUPPORT 0
#define ANALOG_SUPPORT 1
#endif
#if ANALOG_SUPPORT
@ -85,7 +85,7 @@
//--------------------------------------------------------------------------------
#ifndef BMX280_SUPPORT
#define BMX280_SUPPORT 0
#define BMX280_SUPPORT 1
#endif
#ifndef BMX280_ADDRESS


+ 26
- 5
code/espurna/sensor.ino View File

@ -115,7 +115,7 @@ void _sensorConfigure() {
#if WEB_SUPPORT
void _sensorWebSocketOnSend(JsonObject& root) {
void _sensorWebSocketSendData(JsonObject& root) {
char buffer[10];
bool hasTemperature = false;
@ -145,6 +145,26 @@ void _sensorWebSocketOnSend(JsonObject& root) {
}
void _sensorWebSocketStart(JsonObject& root) {
/*
// Sensors manifest
JsonObject& manifest = root.createNestedObject("manifest");
#if BMX280_SUPPORT
BMX280Sensor::manifest(manifest);
#endif
// Sensors configuration
JsonArray& sensors = root.createNestedArray("sensors");
for (unsigned char i; i<_sensors.size(); i++) {
JsonObject& sensor = sensors.createNestedObject();
sensor["id"] = i;
_sensors[i]->getConfig(sensor);
}
*/
}
void _sensorAPISetup() {
for (unsigned char magnitude_id=0; magnitude_id<_magnitudes.size(); magnitude_id++) {
@ -177,7 +197,7 @@ void _sensorPre() {
_sensors[i]->pre();
if (!_sensors[i]->status()) {
DEBUG_MSG("[SENSOR] Error reading data from %s (error: %d)\n",
_sensors[i]->name().c_str(),
_sensors[i]->description().c_str(),
_sensors[i]->error()
);
}
@ -325,7 +345,7 @@ void _magnitudesInit() {
BaseSensor * sensor = _sensors[i];
DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str());
DEBUG_MSG("[SENSOR] %s\n", sensor->description().c_str());
if (sensor->error() != 0) DEBUG_MSG("[SENSOR] -> ERROR %d\n", sensor->error());
for (unsigned char k=0; k<sensor->count(); k++) {
@ -400,7 +420,8 @@ void sensorSetup() {
#if WEB_SUPPORT
// Websockets
wsOnSendRegister(_sensorWebSocketOnSend);
wsOnSendRegister(_sensorWebSocketStart);
wsOnSendRegister(_sensorWebSocketSendData);
wsOnAfterParseRegister(_sensorConfigure);
// API
@ -525,7 +546,7 @@ void sensorLoop() {
_sensorPost();
#if WEB_SUPPORT
wsSend(_sensorWebSocketOnSend);
wsSend(_sensorWebSocketSendData);
#endif
}


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

@ -30,13 +30,13 @@ class AnalogSensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
return String("ANALOG @ GPIO0");
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


+ 52
- 5
code/espurna/sensors/BMX280Sensor.h View File

@ -10,6 +10,8 @@
#include "BaseSensor.h"
#include <SparkFunBME280.h>
#define BMX280_NAME "BME280 / BMP280"
#define BMX280_KEY "bme280"
#define BMX280_CHIP_BMP280 0x58
#define BMX280_CHIP_BME280 0x60
@ -17,13 +19,16 @@ class BMX280Sensor : public BaseSensor {
public:
static unsigned char addresses[2];
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
void setAddress(unsigned char address) {
if (_address != address) _dirty = true;
if (_address == address) return;
_address = address;
_dirty = true;
}
// ---------------------------------------------------------------------
@ -44,8 +49,7 @@ class BMX280Sensor : public BaseSensor {
_chip = 0;
// I2C auto-discover
unsigned char addresses[] = {0x76, 0x77};
_address = lock_i2c(_address, sizeof(addresses), addresses);
_address = lock_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;
// Init
@ -54,7 +58,7 @@ class BMX280Sensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", _chip == BMX280_CHIP_BME280 ? "BME280" : "BMP280", _address);
return String(buffer);
@ -62,7 +66,7 @@ class BMX280Sensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index
@ -122,6 +126,45 @@ class BMX280Sensor : public BaseSensor {
return 0;
}
// Load the configuration manifest
static void manifest(JsonObject& sensors) {
char buffer[10];
JsonObject& sensor = sensors.createNestedObject(BMX280_KEY);
sensor["name"] = BMX280_NAME;
JsonArray& fields = sensor.createNestedArray("fields");
{
JsonObject& field = fields.createNestedObject();
field["tag"] = UI_TAG_SELECT;
field["name"] = "address";
field["label"] = "Address";
JsonArray& options = field.createNestedArray("options");
{
JsonObject& option = options.createNestedObject();
option["name"] = "auto";
option["value"] = 0;
}
for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
JsonObject& option = options.createNestedObject();
snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
option["name"] = String(buffer);
option["value"] = BMX280Sensor::addresses[i];
}
}
};
void getConfig(JsonObject& root) {
root["key"] = BMX280_KEY;
root["address"] = getAddress();
};
void setConfig(JsonObject& root) {
if (root.containsKey("address")) setAddress(root["address"]);
};
protected:
void init() {
@ -212,3 +255,7 @@ class BMX280Sensor : public BaseSensor {
unsigned long _measurement_delay;
};
// Static inizializations
unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};

+ 13
- 3
code/espurna/sensors/BaseSensor.h View File

@ -6,6 +6,7 @@
#pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
typedef enum magnitude_t {
@ -71,7 +72,7 @@ class BaseSensor {
virtual void post() {}
// Descriptive name of the sensor
virtual String name() {}
virtual String description() {}
// Descriptive name of the slot # index
virtual String slot(unsigned char index) {}
@ -82,6 +83,15 @@ class BaseSensor {
// Current value for slot # index
virtual double value(unsigned char index) {}
// Retrieve current instance configuration
virtual void getConfig(JsonObject& root) {};
// Save current instance configuration
virtual void setConfig(JsonObject& root) {};
// Load the configuration manifest
static void manifest(JsonObject& root) {};
// Specific for I2C sensors
unsigned char lock_i2c(unsigned char address, size_t size, unsigned char * addresses) {
@ -125,14 +135,14 @@ class BaseSensor {
// Interrupt attach callback
void attached(unsigned char gpio) {
#if SENSOR_DEBUG
DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, name().c_str());
DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, description().c_str());
#endif
}
// Interrupt detach callback
void detached(unsigned char gpio) {
#if SENSOR_DEBUG
DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, name().c_str());
DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, description().c_str());
#endif
}


+ 2
- 2
code/espurna/sensors/DHTSensor.h View File

@ -59,7 +59,7 @@ class DHTSensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio);
return String(buffer);
@ -67,7 +67,7 @@ class DHTSensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


+ 1
- 1
code/espurna/sensors/DallasSensor.h View File

@ -137,7 +137,7 @@ class DallasSensor : public BaseSensor {
// Descriptive name of the sensor
String name() {
String description() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "Dallas @ GPIO%d", _gpio);
return String(buffer);


+ 2
- 2
code/espurna/sensors/DigitalSensor.h View File

@ -58,7 +58,7 @@ class DigitalSensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
return String(buffer);
@ -66,7 +66,7 @@ class DigitalSensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


+ 1
- 1
code/espurna/sensors/EmonADC121Sensor.h View File

@ -101,7 +101,7 @@ class EmonADC121Sensor : public EmonAnalogSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[30];
snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
return String(buffer);


+ 1
- 1
code/espurna/sensors/EmonADS1X15Sensor.h View File

@ -198,7 +198,7 @@ class EmonADS1X15Sensor : public EmonSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[30];
snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 @ I2C (0x%02X)", _type == ADS1X15_CHIP_ADS1015 ? 0 : 1, _address);
return String(buffer);


+ 2
- 2
code/espurna/sensors/EmonAnalogSensor.h View File

@ -53,13 +53,13 @@ class EmonAnalogSensor : public EmonSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
return String("EMON @ ANALOG @ GPIO0");
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


+ 2
- 2
code/espurna/sensors/EventSensor.h View File

@ -73,7 +73,7 @@ class EventSensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
return String(buffer);
@ -82,7 +82,7 @@ class EventSensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
(void) index;
return name();
return description();
}
// Type for slot # index


+ 2
- 2
code/espurna/sensors/MHZ19Sensor.h View File

@ -74,7 +74,7 @@ class MHZ19Sensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[28];
snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
return String(buffer);
@ -82,7 +82,7 @@ class MHZ19Sensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


+ 1
- 1
code/espurna/sensors/PMSX003Sensor.h View File

@ -69,7 +69,7 @@ class PMSX003Sensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[28];
snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
return String(buffer);


+ 2
- 2
code/espurna/sensors/SI7021Sensor.h View File

@ -84,7 +84,7 @@ class SI7021Sensor : public BaseSensor {
}
// Descriptive name of the sensor
String name() {
String description() {
char buffer[25];
snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", chipAsString().c_str(), _address);
return String(buffer);
@ -92,7 +92,7 @@ class SI7021Sensor : public BaseSensor {
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
return description();
}
// Type for slot # index


Loading…
Cancel
Save