From 7f83ca2daf208a2e233ddb03cfc6f26eb7083cf2 Mon Sep 17 00:00:00 2001 From: "Mitchell A. Cox" Date: Sun, 8 Oct 2017 18:01:47 +0200 Subject: [PATCH] added update on change to ds18b20 temperature sensor with default 1.0 degrees change. Saves space on MQTT server as it avoids logging pointless points.. --- code/espurna/config/sensors.h | 8 +++--- code/espurna/ds18b20.ino | 49 +++++++++++++++++++++-------------- code/espurna/espurna.ino | 2 +- code/platformio.ini | 13 +++++----- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index a3498dfc..7aab200b 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -74,12 +74,14 @@ //-------------------------------------------------------------------------------- #ifndef DS18B20_SUPPORT -#define DS18B20_SUPPORT 0 +#define DS18B20_SUPPORT 1 #endif -#define DS18B20_PIN 14 -#define DS18B20_UPDATE_INTERVAL 60000 +#define DS18B20_PIN 2 +#define DS18B20_UPDATE_INTERVAL 5000 #define DS18B20_TEMPERATURE_TOPIC "temperature" +//Will only send MQTT update if the value has changed by this amount (0.0 sends every interval) +#define DS18B20_UPDATE_ON_CHANGE 1.0 //-------------------------------------------------------------------------------- // Internal power montior diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index ebe98160..71c6933d 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -51,6 +51,10 @@ void dsLoop() { // Check if we should read new data static unsigned long last_update = 0; static bool requested = false; + + static double last_temperature = 0.0; + bool send_update = false; + if ((millis() - last_update > DS18B20_UPDATE_INTERVAL) || (last_update == 0)) { if (!requested) { ds18b20.requestTemperatures(); @@ -80,6 +84,12 @@ void dsLoop() { DEBUG_MSG_P(PSTR("[DS18B20] Error reading sensor\n")); } else { + + //If the new temperature is different from the last + if (fabs(t - last_temperature) >= DS18B20_UPDATE_ON_CHANGE) { + last_temperature = t; + send_update = true; + } _dsTemperature = t; @@ -95,25 +105,26 @@ void dsLoop() { getDSTemperatureStr(), (_dsIsConnected ? ((tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF") : "")); - // Send MQTT messages - mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); - - // Send to Domoticz - #if DOMOTICZ_SUPPORT - domoticzSend("dczTmpIdx", 0, _dsTemperatureStr); - #endif - - #if INFLUXDB_SUPPORT - influxDBSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); - #endif - - // Update websocket clients - #if WEB_SUPPORT - char buffer[100]; - snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits); - wsSend(buffer); - #endif - + if (send_update) { + // Send MQTT messages + mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); + + // Send to Domoticz + #if DOMOTICZ_SUPPORT + domoticzSend("dczTmpIdx", 0, _dsTemperatureStr); + #endif + + #if INFLUXDB_SUPPORT + influxDBSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), _dsTemperatureStr); + #endif + + // Update websocket clients + #if WEB_SUPPORT + char buffer[100]; + snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits); + wsSend(buffer); + #endif + } } } diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 8676a259..bc24b2f0 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -43,7 +43,7 @@ void hardwareSetup() { SPIFFS.begin(); #endif - #if defined(EPSLIVE) + #if defined(ESPLIVE) //The ESPLive has an ADC MUX which needs to be configured. pinMode(16, OUTPUT); digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B) diff --git a/code/platformio.ini b/code/platformio.ini index 1dc91d33..81fe3058 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -889,22 +889,21 @@ monitor_baud = 115200 [env:esplive] platform = espressif8266 framework = arduino -board = nodemcuv2 -board_flash_mode = dout +board = d1_mini lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} -build_flags = ${common.build_flags_1m} -DESPLIVE +build_flags = ${common.build_flags} -DESPLIVE +upload_speed = 460800 monitor_baud = 115200 [env:esplive-ota] platform = espressif8266 framework = arduino -board = nodemcuv2 -board_flash_mode = dout +board = d1_mini lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} -build_flags = ${common.build_flags_1m} -DESPLIVE -upload_speed = 115200 +build_flags = ${common.build_flags} -DESPLIVE +upload_speed = 460800 upload_port = "192.168.4.1" upload_flags = --auth=fibonacci --port 8266 monitor_baud = 115200