diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 110aaa58..6b41cd53 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1,6 +1,6 @@ -// ----------------------------------------------------------------------------- -// SENSORS -// ----------------------------------------------------------------------------- +// ============================================================================= +// SENSORS - General data +// ============================================================================= #define SENSOR_DEBUG 0 // Debug sensors @@ -25,53 +25,82 @@ #define HUMIDITY_MIN_CHANGE 0 // Minimum humidity change to report #endif -#define SENSOR_TEMPERATURE_DECIMALS 1 -#define SENSOR_HUMIDITY_DECIMALS 0 -#define SENSOR_PRESSURE_DECIMALS 2 -#define SENSOR_ANALOG_DECIMALS 0 -#define SENSOR_EVENTS_DECIMALS 0 -#define SENSOR_CURRENT_DECIMALS 3 -#define SENSOR_VOLTAGE_DECIMALS 0 -#define SENSOR_POWER_DECIMALS 0 -#define SENSOR_POWER_FACTOR_DECIMALS 0 -#define SENSOR_ENERGY_DECIMALS 0 -#define SENSOR_PM1dot0_DECIMALS 0 -#define SENSOR_PM2dot5_DECIMALS 0 -#define SENSOR_PM10_DECIMALS 0 -#define SENSOR_CO2_DECIMALS 0 - -#define SENSOR_UNKNOWN_TOPIC "unknown" -#define SENSOR_TEMPERATURE_TOPIC "temperature" -#define SENSOR_HUMIDITY_TOPIC "humidity" -#define SENSOR_PRESSURE_TOPIC "pressure" -#define SENSOR_CURRENT_TOPIC "current" -#define SENSOR_VOLTAGE_TOPIC "voltage" -#define SENSOR_ACTIVE_POWER_TOPIC "power" -#define SENSOR_APPARENT_POWER_TOPIC "apparent" -#define SENSOR_REACTIVE_POWER_TOPIC "reactive" -#define SENSOR_POWER_FACTOR_TOPIC "factor" -#define SENSOR_ENERGY_TOPIC "energy" -#define SENSOR_ENERGY_DELTA_TOPIC "energy_delta" -#define SENSOR_PM1dot0_TOPIC "pm1dot0" -#define SENSOR_PM2dot5_TOPIC "pm2dot5" -#define SENSOR_PM10_TOPIC "pm10" -#define SENSOR_ANALOG_TOPIC "analog" -#define SENSOR_DIGITAL_TOPIC "digital" -#define SENSOR_EVENTS_TOPIC "events" -#define SENSOR_CO2_TOPIC "co2" - #define HUMIDITY_NORMAL 0 #define HUMIDITY_COMFORTABLE 1 #define HUMIDITY_DRY 2 #define HUMIDITY_WET 3 //-------------------------------------------------------------------------------- +// Magnitudes +//-------------------------------------------------------------------------------- + +#define MAGNITUDE_TEMPERATURE_DECIMALS 1 +#define MAGNITUDE_HUMIDITY_DECIMALS 0 +#define MAGNITUDE_PRESSURE_DECIMALS 2 +#define MAGNITUDE_ANALOG_DECIMALS 0 +#define MAGNITUDE_EVENTS_DECIMALS 0 +#define MAGNITUDE_CURRENT_DECIMALS 3 +#define MAGNITUDE_VOLTAGE_DECIMALS 0 +#define MAGNITUDE_POWER_DECIMALS 0 +#define MAGNITUDE_POWER_FACTOR_DECIMALS 0 +#define MAGNITUDE_ENERGY_DECIMALS 0 +#define MAGNITUDE_PM1dot0_DECIMALS 0 +#define MAGNITUDE_PM2dot5_DECIMALS 0 +#define MAGNITUDE_PM10_DECIMALS 0 +#define MAGNITUDE_CO2_DECIMALS 0 + +#define MAGNITUDE_UNKNOWN_TOPIC "unknown" +#define MAGNITUDE_TEMPERATURE_TOPIC "temperature" +#define MAGNITUDE_HUMIDITY_TOPIC "humidity" +#define MAGNITUDE_PRESSURE_TOPIC "pressure" +#define MAGNITUDE_CURRENT_TOPIC "current" +#define MAGNITUDE_VOLTAGE_TOPIC "voltage" +#define MAGNITUDE_ACTIVE_POWER_TOPIC "power" +#define MAGNITUDE_APPARENT_POWER_TOPIC "apparent" +#define MAGNITUDE_REACTIVE_POWER_TOPIC "reactive" +#define MAGNITUDE_POWER_FACTOR_TOPIC "factor" +#define MAGNITUDE_ENERGY_TOPIC "energy" +#define MAGNITUDE_ENERGY_DELTA_TOPIC "energy_delta" +#define MAGNITUDE_PM1dot0_TOPIC "pm1dot0" +#define MAGNITUDE_PM2dot5_TOPIC "pm2dot5" +#define MAGNITUDE_PM10_TOPIC "pm10" +#define MAGNITUDE_ANALOG_TOPIC "analog" +#define MAGNITUDE_DIGITAL_TOPIC "digital" +#define MAGNITUDE_EVENTS_TOPIC "events" +#define MAGNITUDE_CO2_TOPIC "co2" + +//-------------------------------------------------------------------------------- +// Sensor ID +// These should remain over time, do not modify them, only add new ones at the end +//-------------------------------------------------------------------------------- + +#define SENSOR_DHT_ID 0x01 +#define SENSOR_DALLAS_ID 0x02 +#define SENSOR_EMON_ANALOG_ID 0x03 +#define SENSOR_EMON_ADC121_ID 0x04 +#define SENSOR_EMON_ADS1X15_ID 0x05 +#define SENSOR_HLW8012_ID 0x06 +#define SENSOR_V9261F_ID 0x07 +#define SENSOR_ECH1560_ID 0x08 +#define SENSOR_ANALOG_ID 0x09 +#define SENSOR_DIGITAL_ID 0x10 +#define SENSOR_EVENTS_ID 0x11 +#define SENSOR_PMSX003_ID 0x12 +#define SENSOR_BMX280_ID 0x13 +#define SENSOR_MHZ19_ID 0x14 +#define SENSOR_SI7021_ID 0x15 + +// ============================================================================= +// Specific data for each sensor +// ============================================================================= + +//------------------------------------------------------------------------------ // Analog sensor // Enable support by passing ANALOG_SUPPORT=1 build flag //-------------------------------------------------------------------------------- #ifndef ANALOG_SUPPORT -#define ANALOG_SUPPORT 1 +#define ANALOG_SUPPORT 0 #endif #if ANALOG_SUPPORT @@ -79,13 +108,13 @@ #define ADC_VCC_ENABLED 0 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // BME280/BMP280 // Enable support by passing BMX280_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef BMX280_SUPPORT -#define BMX280_SUPPORT 1 +#define BMX280_SUPPORT 0 #endif #ifndef BMX280_ADDRESS @@ -102,10 +131,10 @@ #define I2C_SUPPORT 1 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Dallas OneWire temperature sensors // Enable support by passing DALLAS_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef DALLAS_SUPPORT #define DALLAS_SUPPORT 0 @@ -117,10 +146,10 @@ #define DALLAS_RESOLUTION 9 // Not used atm -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // DHTXX temperature/humidity sensor // Enable support by passing DHT_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef DHT_SUPPORT #define DHT_SUPPORT 0 @@ -131,13 +160,13 @@ #endif #ifndef DHT_TYPE -#define DHT_TYPE DHT22 +#define DHT_TYPE DHT_CHIP_DHT22 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Digital sensor // Enable support by passing DIGITAL_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef DIGITAL_SUPPORT #define DIGITAL_SUPPORT 0 @@ -155,9 +184,9 @@ #define DIGITAL_DEFAULT_STATE 1 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Energy Monitor general settings -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #define EMON_MAX_SAMPLES 1000 // Max number of samples to get #define EMON_MAX_TIME 250 // Max time in ms to sample @@ -169,10 +198,10 @@ #define EMON_REPORT_POWER 1 // Report power #define EMON_REPORT_ENERGY 1 // Report energy -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Energy Monitor based on ADC121 // Enable support by passing EMON_ADC121_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef EMON_ADC121_SUPPORT #define EMON_ADC121_SUPPORT 0 // Do not build support by default @@ -185,10 +214,10 @@ #define I2C_SUPPORT 1 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Energy Monitor based on ADS1X15 // Enable support by passing EMON_ADS1X15_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef EMON_ADS1X15_SUPPORT #define EMON_ADS1X15_SUPPORT 0 // Do not build support by default @@ -204,10 +233,10 @@ #define I2C_SUPPORT 1 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Energy Monitor based on interval analog GPIO // Enable support by passing EMON_ANALOG_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef EMON_ANALOG_SUPPORT #define EMON_ANALOG_SUPPORT 0 // Do not build support by default @@ -218,10 +247,10 @@ #define ADC_VCC_ENABLED 0 #endif -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Counter sensor // Enable support by passing EVENTS_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef EVENTS_SUPPORT #define EVENTS_SUPPORT 0 // Do not build with counter support by default @@ -241,10 +270,10 @@ #define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // MHZ19 CO2 sensor // Enable support by passing MHZ19_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef MHZ19_SUPPORT #define MHZ19_SUPPORT 0 @@ -253,10 +282,10 @@ #define MHZ19_RX_PIN 13 #define MHZ19_TX_PIN 15 -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Particle Monitor based on Plantower PMSX003 // Enable support by passing PMSX003_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef PMSX003_SUPPORT #define PMSX003_SUPPORT 0 @@ -265,10 +294,10 @@ #define PMS_RX_PIN 13 #define PMS_TX_PIN 15 -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // SI7021 temperature & humidity sensor // Enable support by passing SI7021_SUPPORT=1 build flag -//-------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ #ifndef SI7021_SUPPORT #define SI7021_SUPPORT 0 @@ -283,6 +312,10 @@ #define I2C_SUPPORT 1 #endif +// ============================================================================= +// Sensor helpers configuration +// ============================================================================= + // ----------------------------------------------------------------------------- // I2C // ----------------------------------------------------------------------------- diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 0bc76326..bf86f50d 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -37,45 +37,45 @@ double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION; // ----------------------------------------------------------------------------- String _sensorTopic(magnitude_t type) { - if (type == MAGNITUDE_TEMPERATURE) return String(SENSOR_TEMPERATURE_TOPIC); - if (type == MAGNITUDE_HUMIDITY) return String(SENSOR_HUMIDITY_TOPIC); - if (type == MAGNITUDE_PRESSURE) return String(SENSOR_PRESSURE_TOPIC); - if (type == MAGNITUDE_CURRENT) return String(SENSOR_CURRENT_TOPIC); - if (type == MAGNITUDE_VOLTAGE) return String(SENSOR_VOLTAGE_TOPIC); - if (type == MAGNITUDE_POWER_ACTIVE) return String(SENSOR_ACTIVE_POWER_TOPIC); - if (type == MAGNITUDE_POWER_APPARENT) return String(SENSOR_APPARENT_POWER_TOPIC); - if (type == MAGNITUDE_POWER_REACTIVE) return String(SENSOR_REACTIVE_POWER_TOPIC); - if (type == MAGNITUDE_POWER_FACTOR) return String(SENSOR_POWER_FACTOR_TOPIC); - if (type == MAGNITUDE_ENERGY) return String(SENSOR_ENERGY_TOPIC); - if (type == MAGNITUDE_ENERGY_DELTA) return String(SENSOR_ENERGY_DELTA_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_PM1dot0) return String(SENSOR_PM1dot0_TOPIC); - if (type == MAGNITUDE_PM2dot5) return String(SENSOR_PM2dot5_TOPIC); - if (type == MAGNITUDE_PM10) return String(SENSOR_PM10_TOPIC); - if (type == MAGNITUDE_CO2) return String(SENSOR_CO2_TOPIC); - return String(SENSOR_UNKNOWN_TOPIC); + if (type == MAGNITUDE_TEMPERATURE) return String(MAGNITUDE_TEMPERATURE_TOPIC); + if (type == MAGNITUDE_HUMIDITY) return String(MAGNITUDE_HUMIDITY_TOPIC); + if (type == MAGNITUDE_PRESSURE) return String(MAGNITUDE_PRESSURE_TOPIC); + if (type == MAGNITUDE_CURRENT) return String(MAGNITUDE_CURRENT_TOPIC); + if (type == MAGNITUDE_VOLTAGE) return String(MAGNITUDE_VOLTAGE_TOPIC); + if (type == MAGNITUDE_POWER_ACTIVE) return String(MAGNITUDE_ACTIVE_POWER_TOPIC); + if (type == MAGNITUDE_POWER_APPARENT) return String(MAGNITUDE_APPARENT_POWER_TOPIC); + if (type == MAGNITUDE_POWER_REACTIVE) return String(MAGNITUDE_REACTIVE_POWER_TOPIC); + if (type == MAGNITUDE_POWER_FACTOR) return String(MAGNITUDE_POWER_FACTOR_TOPIC); + if (type == MAGNITUDE_ENERGY) return String(MAGNITUDE_ENERGY_TOPIC); + if (type == MAGNITUDE_ENERGY_DELTA) return String(MAGNITUDE_ENERGY_DELTA_TOPIC); + if (type == MAGNITUDE_ANALOG) return String(MAGNITUDE_ANALOG_TOPIC); + if (type == MAGNITUDE_DIGITAL) return String(MAGNITUDE_DIGITAL_TOPIC); + if (type == MAGNITUDE_EVENTS) return String(MAGNITUDE_EVENTS_TOPIC); + if (type == MAGNITUDE_PM1dot0) return String(MAGNITUDE_PM1dot0_TOPIC); + if (type == MAGNITUDE_PM2dot5) return String(MAGNITUDE_PM2dot5_TOPIC); + if (type == MAGNITUDE_PM10) return String(MAGNITUDE_PM10_TOPIC); + if (type == MAGNITUDE_CO2) return String(MAGNITUDE_CO2_TOPIC); + return String(MAGNITUDE_UNKNOWN_TOPIC); } unsigned char _sensorDecimals(magnitude_t type) { - if (type == MAGNITUDE_TEMPERATURE) return SENSOR_TEMPERATURE_DECIMALS; - if (type == MAGNITUDE_HUMIDITY) return SENSOR_HUMIDITY_DECIMALS; - if (type == MAGNITUDE_PRESSURE) return SENSOR_PRESSURE_DECIMALS; - if (type == MAGNITUDE_CURRENT) return SENSOR_CURRENT_DECIMALS; - if (type == MAGNITUDE_VOLTAGE) return SENSOR_VOLTAGE_DECIMALS; - if (type == MAGNITUDE_POWER_ACTIVE) return SENSOR_POWER_DECIMALS; - if (type == MAGNITUDE_POWER_APPARENT) return SENSOR_POWER_DECIMALS; - if (type == MAGNITUDE_POWER_REACTIVE) return SENSOR_POWER_DECIMALS; - if (type == MAGNITUDE_POWER_FACTOR) return SENSOR_POWER_FACTOR_DECIMALS; - if (type == MAGNITUDE_ENERGY) return SENSOR_ENERGY_DECIMALS; - if (type == MAGNITUDE_ENERGY_DELTA) return SENSOR_ENERGY_DECIMALS; - if (type == MAGNITUDE_ANALOG) return SENSOR_ANALOG_DECIMALS; - if (type == MAGNITUDE_EVENTS) return SENSOR_EVENTS_DECIMALS; - if (type == MAGNITUDE_PM1dot0) return SENSOR_PM1dot0_DECIMALS; - if (type == MAGNITUDE_PM2dot5) return SENSOR_PM2dot5_DECIMALS; - if (type == MAGNITUDE_PM10) return SENSOR_PM10_DECIMALS; - if (type == MAGNITUDE_CO2) return SENSOR_CO2_DECIMALS; + if (type == MAGNITUDE_TEMPERATURE) return MAGNITUDE_TEMPERATURE_DECIMALS; + if (type == MAGNITUDE_HUMIDITY) return MAGNITUDE_HUMIDITY_DECIMALS; + if (type == MAGNITUDE_PRESSURE) return MAGNITUDE_PRESSURE_DECIMALS; + if (type == MAGNITUDE_CURRENT) return MAGNITUDE_CURRENT_DECIMALS; + if (type == MAGNITUDE_VOLTAGE) return MAGNITUDE_VOLTAGE_DECIMALS; + if (type == MAGNITUDE_POWER_ACTIVE) return MAGNITUDE_POWER_DECIMALS; + if (type == MAGNITUDE_POWER_APPARENT) return MAGNITUDE_POWER_DECIMALS; + if (type == MAGNITUDE_POWER_REACTIVE) return MAGNITUDE_POWER_DECIMALS; + if (type == MAGNITUDE_POWER_FACTOR) return MAGNITUDE_POWER_FACTOR_DECIMALS; + if (type == MAGNITUDE_ENERGY) return MAGNITUDE_ENERGY_DECIMALS; + if (type == MAGNITUDE_ENERGY_DELTA) return MAGNITUDE_ENERGY_DECIMALS; + if (type == MAGNITUDE_ANALOG) return MAGNITUDE_ANALOG_DECIMALS; + if (type == MAGNITUDE_EVENTS) return MAGNITUDE_EVENTS_DECIMALS; + if (type == MAGNITUDE_PM1dot0) return MAGNITUDE_PM1dot0_DECIMALS; + if (type == MAGNITUDE_PM2dot5) return MAGNITUDE_PM2dot5_DECIMALS; + if (type == MAGNITUDE_PM10) return MAGNITUDE_PM10_DECIMALS; + if (type == MAGNITUDE_CO2) return MAGNITUDE_CO2_DECIMALS; return 0; } @@ -149,7 +149,7 @@ void _sensorWebSocketStart(JsonObject& root) { /* // Sensors manifest - JsonObject& manifest = root.createNestedObject("manifest"); + JsonArray& manifest = root.createNestedArray("manifest"); #if BMX280_SUPPORT BMX280Sensor::manifest(manifest); #endif @@ -158,13 +158,21 @@ void _sensorWebSocketStart(JsonObject& root) { JsonArray& sensors = root.createNestedArray("sensors"); for (unsigned char i; i<_sensors.size(); i++) { JsonObject& sensor = sensors.createNestedObject(); - sensor["id"] = i; + sensor["index"] = i; + sensor["id"] = _sensors[i]->getID(); _sensors[i]->getConfig(sensor); } */ } +void _sensorWebSocketReceiveData() { + + //_emon.setCurrentRatio(getSetting("pwrRatioC", EMON_CURRENT_RATIO).toFloat()); + //_power_voltage = getSetting("pwrVoltage", POWER_VOLTAGE).toFloat(); + +} + void _sensorAPISetup() { for (unsigned char magnitude_id=0; magnitude_id<_magnitudes.size(); magnitude_id++) { diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index dcdc90cc..37e4f683 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -18,6 +18,7 @@ class AnalogSensor : public BaseSensor { AnalogSensor(): BaseSensor() { _count = 1; + _sensor_id = SENSOR_ANALOG_ID; } // --------------------------------------------------------------------- diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 25aa8be1..298c164f 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -10,8 +10,6 @@ #include "BaseSensor.h" #include -#define BMX280_NAME "BME280 / BMP280" -#define BMX280_KEY "bme280" #define BMX280_CHIP_BMP280 0x58 #define BMX280_CHIP_BME280 0x60 @@ -25,6 +23,10 @@ class BMX280Sensor : public BaseSensor { // Public // --------------------------------------------------------------------- + BMX280Sensor(): BaseSensor() { + _sensor_id = SENSOR_BMX280_ID; + } + void setAddress(unsigned char address) { if (_address == address) return; _address = address; @@ -127,12 +129,12 @@ class BMX280Sensor : public BaseSensor { } // Load the configuration manifest - static void manifest(JsonObject& sensors) { + static void manifest(JsonArray& sensors) { char buffer[10]; - JsonObject& sensor = sensors.createNestedObject(BMX280_KEY); - sensor["name"] = BMX280_NAME; + JsonObject& sensor = sensors.createNestedObject(); + sensor["sensor_id"] = SENSOR_BMX280_ID; JsonArray& fields = sensor.createNestedArray("fields"); { @@ -157,7 +159,7 @@ class BMX280Sensor : public BaseSensor { }; void getConfig(JsonObject& root) { - root["key"] = BMX280_KEY; + root["sensor_id"] = _sensor_id; root["address"] = getAddress(); }; diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index b1e02da2..0608d4aa 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -90,7 +90,10 @@ class BaseSensor { virtual void setConfig(JsonObject& root) {}; // Load the configuration manifest - static void manifest(JsonObject& root) {}; + static void manifest(JsonArray& root) {}; + + // Sensor ID + unsigned char getID() { return _sensor_id; }; // Specific for I2C sensors unsigned char lock_i2c(unsigned char address, size_t size, unsigned char * addresses) { @@ -120,18 +123,6 @@ class BaseSensor { } - // Return sensor status (true for ready) - bool status() { return _error == 0; } - - // Return sensor last internal error - int error() { return _error; } - - // Number of available slots - unsigned char count() { return _count; } - - // Handle interrupt calls - virtual void handleInterrupt(unsigned char gpio) {} - // Interrupt attach callback void attached(unsigned char gpio) { #if SENSOR_DEBUG @@ -146,6 +137,18 @@ class BaseSensor { #endif } + // Return sensor status (true for ready) + bool status() { return _error == 0; } + + // Return sensor last internal error + int error() { return _error; } + + // Number of available slots + unsigned char count() { return _count; } + + // Handle interrupt calls + virtual void handleInterrupt(unsigned char gpio) {} + protected: // Attach interrupt @@ -154,6 +157,7 @@ class BaseSensor { // Detach interrupt void detach(unsigned char gpio); + unsigned char _sensor_id = 0x00; int _error = 0; bool _dirty = true; unsigned char _count = 0; diff --git a/code/espurna/sensors/DHTSensor.h b/code/espurna/sensors/DHTSensor.h index 96328b4d..fadbb116 100644 --- a/code/espurna/sensors/DHTSensor.h +++ b/code/espurna/sensors/DHTSensor.h @@ -12,10 +12,10 @@ #define DHT_MAX_ERRORS 5 #define DHT_MIN_INTERVAL 2000 -#define DHT11 11 -#define DHT22 22 -#define DHT21 21 -#define AM2301 21 +#define DHT_CHIP_DHT11 11 +#define DHT_CHIP_DHT22 22 +#define DHT_CHIP_DHT21 21 +#define DHT_CHIP_AM2301 21 class DHTSensor : public BaseSensor { @@ -27,6 +27,7 @@ class DHTSensor : public BaseSensor { DHTSensor(): BaseSensor() { _count = 2; + _sensor_id = SENSOR_DHT_ID; } // --------------------------------------------------------------------- @@ -166,7 +167,7 @@ class DHTSensor : public BaseSensor { } // Get humidity from Data[0] and Data[1] - if (_type == DHT11) { + if (_type == DHT_CHIP_DHT11) { _humidity = dhtData[0]; } else { _humidity = dhtData[0] * 256 + dhtData[1]; @@ -174,7 +175,7 @@ class DHTSensor : public BaseSensor { } // Get temp from Data[2] and Data[3] - if (_type == DHT11) { + if (_type == DHT_CHIP_DHT11) { _temperature = dhtData[2]; } else { _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3]; diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index 76cb94f0..9ab1f64d 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -33,6 +33,12 @@ class DallasSensor : public BaseSensor { // Public // --------------------------------------------------------------------- + DallasSensor(): BaseSensor() { + _sensor_id = SENSOR_DALLAS_ID; + } + + // --------------------------------------------------------------------- + void setGPIO(unsigned char gpio) { if (_gpio == gpio) return; _gpio = gpio; diff --git a/code/espurna/sensors/DigitalSensor.h b/code/espurna/sensors/DigitalSensor.h index 6c856f39..4a065338 100644 --- a/code/espurna/sensors/DigitalSensor.h +++ b/code/espurna/sensors/DigitalSensor.h @@ -18,6 +18,7 @@ class DigitalSensor : public BaseSensor { DigitalSensor(): BaseSensor() { _count = 1; + _sensor_id = SENSOR_DIGITAL_ID; } // --------------------------------------------------------------------- diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index ccea1a9b..5e0da6fb 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -39,6 +39,7 @@ class EmonADC121Sensor : public EmonAnalogSensor { EmonADC121Sensor(): EmonAnalogSensor() { _channels = ADC121_CHANNELS; + _sensor_id = SENSOR_EMON_ADC121_ID; init(); } diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index f3130eea..a0b59e24 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -96,7 +96,6 @@ #define ADS1X15_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions #define ADS1X15_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default) - class EmonADS1X15Sensor : public EmonSensor { public: @@ -107,6 +106,7 @@ class EmonADS1X15Sensor : public EmonSensor { EmonADS1X15Sensor(): EmonSensor() { _channels = ADS1X15_CHANNELS; + _sensor_id = SENSOR_EMON_ADS1X15_ID; init(); } diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index 49bbf2ad..19d2b259 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -22,6 +22,7 @@ class EmonAnalogSensor : public EmonSensor { EmonAnalogSensor(): EmonSensor() { _channels = EMON_ANALOG_CHANNELS; + _sensor_id = SENSOR_EMON_ANALOG_ID; init(); } diff --git a/code/espurna/sensors/EventSensor.h b/code/espurna/sensors/EventSensor.h index f6c4534f..8bb7987f 100644 --- a/code/espurna/sensors/EventSensor.h +++ b/code/espurna/sensors/EventSensor.h @@ -18,6 +18,7 @@ class EventSensor : public BaseSensor { EventSensor(): BaseSensor() { _count = 1; + _sensor_id = SENSOR_EVENTS_ID; } ~EventSensor() { diff --git a/code/espurna/sensors/MHZ19Sensor.h b/code/espurna/sensors/MHZ19Sensor.h index bde3a2b2..b767ead5 100644 --- a/code/espurna/sensors/MHZ19Sensor.h +++ b/code/espurna/sensors/MHZ19Sensor.h @@ -31,6 +31,7 @@ class MHZ19Sensor : public BaseSensor { MHZ19Sensor(): BaseSensor() { _count = 1; + _sensor_id = SENSOR_MHZ19_ID; } void setRX(unsigned char pin_rx) { diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index 2d3a92c3..cc6d0d49 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -22,6 +22,7 @@ class PMSX003Sensor : public BaseSensor { PMSX003Sensor(): BaseSensor() { _count = 3; + _sensor_id = SENSOR_PMSX003_ID; } void setRX(unsigned char pin_rx) { diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index 88e72760..6cef7612 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -31,6 +31,12 @@ class SI7021Sensor : public BaseSensor { // Public // --------------------------------------------------------------------- + SI7021Sensor(): BaseSensor() { + _sensor_id = SENSOR_SI7021_ID; + } + + // --------------------------------------------------------------------- + void setAddress(unsigned char address) { if (_address != address) _dirty = true; _address = address; diff --git a/code/html/custom.js b/code/html/custom.js index 29c1a9c2..b7efdd5a 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -10,6 +10,7 @@ var numReconnect = 0; var numReload = 0; var useWhite = false; +var manifest; // ----------------------------------------------------------------------------- // Messages @@ -484,6 +485,11 @@ function initMagnitudes(data) { } +function getManifest(sensor_id) { + for (row of manifest) if (row.sensor_id == sensor_id) return row; + return null; +} + // ----------------------------------------------------------------------------- // Lights // ----------------------------------------------------------------------------- @@ -750,6 +756,10 @@ function processData(data) { return; } + if (key == "manifest") { + manifest = data[key]; + } + // --------------------------------------------------------------------- // WiFi // ---------------------------------------------------------------------