diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 3d0c3f37..5844f2e9 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -36,6 +36,9 @@ #define HUMIDITY_DRY 2 #define HUMIDITY_WET 3 +#define SENSOR_PUBLISH_ADDRESSES 0 // Publish sensor addresses +#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses + //-------------------------------------------------------------------------------- // Sensor ID // These should remain over time, do not modify them, only add new ones at the end @@ -164,10 +167,6 @@ #define DALLAS_RESOLUTION 9 // Not used atm #define DALLAS_READ_INTERVAL 2000 // Force sensor read & cache every 2 seconds -#define DALLAS_PUBLISH_ADDRESSES 0 // Publish sensor addresses -#define DALLAS_ADDRESS_TOPIC "address" // Topic to publish sensor addresses - - //------------------------------------------------------------------------------ // DHTXX temperature/humidity sensor // Enable support by passing DHT_SUPPORT=1 build flag diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 36783959..f7791da1 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -641,15 +641,15 @@ void sensorLoop() { mqttSend(_magnitudeTopic(magnitude.type).c_str(), buffer); } - #if DALLAS_SUPPORT && DALLAS_PUBLISH_ADDRESSES - if (magnitude.sensor->getID() == SENSOR_DALLAS_ID) { - if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) { - mqttSend(DALLAS_ADDRESS_TOPIC, magnitude.global, (((DallasSensor *) magnitude.sensor)->getAddress(magnitude.local)).c_str()); - } else { - mqttSend(DALLAS_ADDRESS_TOPIC, (((DallasSensor *) magnitude.sensor)->getAddress(magnitude.local)).c_str()); - } + #if SENSOR_PUBLISH_ADDRESSES + char topic[32]; + snprintf(topic, sizeof(topic), "%s/%s", SENSOR_ADDRESS_TOPIC, _magnitudeTopic(magnitude.type).c_str()); + if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) { + mqttSend(topic, magnitude.global, magnitude.sensor->address(magnitude.local).c_str()); + } else { + mqttSend(topic, magnitude.sensor->address(magnitude.local).c_str()); } - #endif // DALLAS_SUPPORT && DALLAS_PUBLISH_ADDRESSES + #endif // SENSOR_PUBLISH_ADDRESSES #endif // MQTT_SUPPORT diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index 108a00a2..0742e2bb 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -42,6 +42,11 @@ class AnalogSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String("0"); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index af585629..d34bcb8e 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -150,7 +150,7 @@ class BMX280Sensor : public I2CSensor { void getConfig(JsonObject& root) { root["sensor_id"] = _sensor_id; - root["address"] = getAddress(); + root["address"] = _address; }; void setConfig(JsonObject& root) { diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 2fb469f7..1daf41f3 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -46,6 +46,12 @@ class BaseSensor { // Descriptive name of the sensor virtual String description() {} + // Address of the sensor (it could be the GPIO or I2C address) + virtual String address(unsigned char index) {} + + // Descriptive name of the slot # index + virtual String slot(unsigned char index) {}; + // Type for slot # index virtual unsigned char type(unsigned char index) {} @@ -61,9 +67,6 @@ class BaseSensor { // Load the configuration manifest static void manifest(JsonArray& root) {}; - // Descriptive name of the slot # index - virtual String slot(unsigned char index) {}; - // Sensor ID unsigned char getID() { return _sensor_id; }; diff --git a/code/espurna/sensors/DHTSensor.h b/code/espurna/sensors/DHTSensor.h index f7427968..4fce7e1a 100644 --- a/code/espurna/sensors/DHTSensor.h +++ b/code/espurna/sensors/DHTSensor.h @@ -95,6 +95,11 @@ class DHTSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String(_gpio); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index 955362e6..ed934605 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -171,7 +171,7 @@ class DallasSensor : public BaseSensor { } // Address of the device - String getAddress(unsigned char index) { + String address(unsigned char index) { char buffer[20] = {0}; if (index < _count) { uint8_t * address = _devices[index].address; diff --git a/code/espurna/sensors/DigitalSensor.h b/code/espurna/sensors/DigitalSensor.h index 8ae7f9a4..0ec848b9 100644 --- a/code/espurna/sensors/DigitalSensor.h +++ b/code/espurna/sensors/DigitalSensor.h @@ -72,6 +72,11 @@ class DigitalSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String(_gpio); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/ECH1560Sensor.h b/code/espurna/sensors/ECH1560Sensor.h index 9c7c6089..de200811 100644 --- a/code/espurna/sensors/ECH1560Sensor.h +++ b/code/espurna/sensors/ECH1560Sensor.h @@ -87,6 +87,13 @@ class ECH1560Sensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[6]; + snprintf(buffer, sizeof(buffer), "%i:%i", _clk, _miso); + return String(buffer); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index e355b88e..3ad437df 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -203,6 +203,14 @@ class EmonADS1X15Sensor : public EmonSensor { return String(buffer); } + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[10]; + unsigned char channel = getChannel(index % _ports); + snprintf(buffer, sizeof(buffer), "0x%02X:%i", _address, channel); + return String(buffer); + } + // Type for slot # index unsigned char type(unsigned char index) { if (index < _count) { diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index bbabbe26..6461cef2 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -59,6 +59,11 @@ class EmonAnalogSensor : public EmonSensor { return String("EMON @ ANALOG @ GPIO0"); } + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String("0"); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/EventSensor.h b/code/espurna/sensors/EventSensor.h index 96787dc0..c83c2091 100644 --- a/code/espurna/sensors/EventSensor.h +++ b/code/espurna/sensors/EventSensor.h @@ -86,6 +86,11 @@ class EventSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String(_gpio); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/HLW8012Sensor.h b/code/espurna/sensors/HLW8012Sensor.h index 0ff36183..c3cd3fcb 100644 --- a/code/espurna/sensors/HLW8012Sensor.h +++ b/code/espurna/sensors/HLW8012Sensor.h @@ -167,6 +167,13 @@ class HLW8012Sensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[10]; + snprintf(buffer, sizeof(buffer), "%i:%i:%i", _sel, _cf, _cf1); + return String(buffer); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/I2CSensor.h b/code/espurna/sensors/I2CSensor.h index 73e4522d..5a01971e 100644 --- a/code/espurna/sensors/I2CSensor.h +++ b/code/espurna/sensors/I2CSensor.h @@ -28,6 +28,13 @@ class I2CSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[5]; + snprintf(buffer, sizeof(buffer), "0x%02X", _address); + return String(buffer); + } + protected: // Specific for I2C sensors diff --git a/code/espurna/sensors/MHZ19Sensor.h b/code/espurna/sensors/MHZ19Sensor.h index 41c550a4..2fcc1b85 100644 --- a/code/espurna/sensors/MHZ19Sensor.h +++ b/code/espurna/sensors/MHZ19Sensor.h @@ -94,6 +94,13 @@ class MHZ19Sensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[6]; + snprintf(buffer, sizeof(buffer), "%i:%i", _pin_rx, _pin_tx); + return String(buffer); + } + // Type for slot # index unsigned char type(unsigned char index) { _error = SENSOR_ERROR_OK; diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index e6a3aada..f5662292 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -97,6 +97,13 @@ class PMSX003Sensor : public BaseSensor { return String(); } + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[6]; + snprintf(buffer, sizeof(buffer), "%i:%i", _pin_rx, _pin_tx); + return String(buffer); + } + // Type for slot # index unsigned char type(unsigned char index) { if (index < _count) { diff --git a/code/espurna/sensors/V9261FSensor.h b/code/espurna/sensors/V9261FSensor.h index 874c9e66..928dfcdd 100644 --- a/code/espurna/sensors/V9261FSensor.h +++ b/code/espurna/sensors/V9261FSensor.h @@ -82,6 +82,11 @@ class V9261FSensor : public BaseSensor { return description(); }; + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + return String(_pin_rx); + } + // Loop-like method, call it in your main loop void tick() { _read();