diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index ddd65dfe..bdcf5b34 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -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 // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 1fe33040..110aaa58 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -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 diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 2f7c7d20..0bc76326 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -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; kcount(); 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 } diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index 9e3b657b..dcdc90cc 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -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 diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 67a1137d..25aa8be1 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -10,6 +10,8 @@ #include "BaseSensor.h" #include +#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}; diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 483ff16b..b1e02da2 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -6,6 +6,7 @@ #pragma once #include +#include 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 } diff --git a/code/espurna/sensors/DHTSensor.h b/code/espurna/sensors/DHTSensor.h index 228c4698..96328b4d 100644 --- a/code/espurna/sensors/DHTSensor.h +++ b/code/espurna/sensors/DHTSensor.h @@ -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 diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index d3559cce..76cb94f0 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -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); diff --git a/code/espurna/sensors/DigitalSensor.h b/code/espurna/sensors/DigitalSensor.h index a72d5350..6c856f39 100644 --- a/code/espurna/sensors/DigitalSensor.h +++ b/code/espurna/sensors/DigitalSensor.h @@ -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 diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 48e090b6..ccea1a9b 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -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); diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index 7d67655f..f3130eea 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -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); diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index d3c5ba76..49bbf2ad 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -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 diff --git a/code/espurna/sensors/EventSensor.h b/code/espurna/sensors/EventSensor.h index f0f126a4..f6c4534f 100644 --- a/code/espurna/sensors/EventSensor.h +++ b/code/espurna/sensors/EventSensor.h @@ -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 diff --git a/code/espurna/sensors/MHZ19Sensor.h b/code/espurna/sensors/MHZ19Sensor.h index 3c206e1c..bde3a2b2 100644 --- a/code/espurna/sensors/MHZ19Sensor.h +++ b/code/espurna/sensors/MHZ19Sensor.h @@ -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 diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index 6520f5bd..2d3a92c3 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -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); diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index 5c59ee1e..88e72760 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -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