From ccc5843e7bc4376ac64dd60d541113bddc418c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 8 Jan 2018 10:33:54 +0100 Subject: [PATCH] Option to publish Dallas sensor address via MQTT (disabled by default) --- code/espurna/config/sensors.h | 8 ++++++-- code/espurna/sensor.ino | 12 ++++++++++++ code/espurna/sensors/DallasSensor.h | 13 +++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 0f2f006a..3d0c3f37 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -161,8 +161,12 @@ #define DALLAS_PIN 14 #endif -#define DALLAS_RESOLUTION 9 // Not used atm -#define DALLAS_READ_INTERVAL 2000 // Force sensor read & cache every 2 seconds +#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 diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index ab2c16c7..36783959 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -634,11 +634,23 @@ void sensorLoop() { dtostrf(filtered, 1-sizeof(buffer), decimals, buffer); #if MQTT_SUPPORT + if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) { mqttSend(_magnitudeTopic(magnitude.type).c_str(), magnitude.global, buffer); } else { 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()); + } + } + #endif // DALLAS_SUPPORT && DALLAS_PUBLISH_ADDRESSES + #endif // MQTT_SUPPORT #if INFLUXDB_SUPPORT diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index 985fa0f9..955362e6 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -170,6 +170,19 @@ class DallasSensor : public BaseSensor { return String(buffer); } + // Address of the device + String getAddress(unsigned char index) { + char buffer[20] = {0}; + if (index < _count) { + uint8_t * address = _devices[index].address; + snprintf(buffer, sizeof(buffer), "%02X%02X%02X%02X%02X%02X%02X%02X", + address[0], address[1], address[2], address[3], + address[4], address[5], address[6], address[7] + ); + } + return String(buffer); + } + // Descriptive name of the slot # index String slot(unsigned char index) { _error = SENSOR_ERROR_OK;