From fa11355767c2779d1ac5fe146054ab7f3523b480 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 31 Dec 2023 14:57:25 +0100 Subject: [PATCH 1/4] read temp every minute and publish only if difference is more than 0.2 --- include/mqtt.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/mqtt.h b/include/mqtt.h index 250cae9..614475e 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -19,7 +19,7 @@ namespace Mqtt { void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts); Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts); - Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); + Task tPublishBmp(TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); AsyncMqttClient client; @@ -120,7 +120,11 @@ namespace Mqtt { } void publishBmp280() { + static float lastTemp = 0; Bmp::data.readAll(); + if (abs(lastTemp - Bmp::data.temp) <= 0.2) return; + lastTemp = Bmp::data.temp; + Display::displayTemp(Bmp::data.temp); StaticJsonDocument<255> jsonDoc; jsonDoc["temperature"] = Bmp::data.temp; jsonDoc["pressure"] = Bmp::data.pressure; From 23de23a2e09a279923b5f248ce90973e491ec01f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 31 Dec 2023 14:57:58 +0100 Subject: [PATCH 2/4] move read temp logic into main --- include/mqtt.h | 8 -------- src/esp_clock.cpp | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index 614475e..b752d52 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -19,7 +19,6 @@ namespace Mqtt { void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts); Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts); - Task tPublishBmp(TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); AsyncMqttClient client; @@ -120,11 +119,6 @@ namespace Mqtt { } void publishBmp280() { - static float lastTemp = 0; - Bmp::data.readAll(); - if (abs(lastTemp - Bmp::data.temp) <= 0.2) return; - lastTemp = Bmp::data.temp; - Display::displayTemp(Bmp::data.temp); StaticJsonDocument<255> jsonDoc; jsonDoc["temperature"] = Bmp::data.temp; jsonDoc["pressure"] = Bmp::data.pressure; @@ -163,7 +157,6 @@ namespace Mqtt { Display::onHourFormatChanged(hourFormatMqtt->publishState); client.onConnect([](bool sessionPresent) { tPublishInit.enable(); - tPublishBmp.enableIfNot(); tPublishCommand.enableDelayed(); client.subscribe(espClockTopic, 0); tReConnect.disable(); @@ -172,7 +165,6 @@ namespace Mqtt { client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { tReConnect.enableDelayed(); tPublishCommand.disable(); - tPublishBmp.disable(); Serial.println("Disconnected from MQTT"); }); client.onMessage(onMessage); diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index fb31b65..fe7fe4f 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -27,6 +27,14 @@ Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() { Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { Mqtt::ledMqtt->publishState(); }, &ts); +Task tReadBmp(TASK_MINUTE, TASK_FOREVER, []() { + static float lastTemp = 0; + Bmp::data.readAll(); + if (abs(lastTemp - Bmp::data.temp) <= 0.2) return; + lastTemp = Bmp::data.temp; + Display::displayTemp(Bmp::data.temp); + Mqtt::publishBmp280(); +}, &ts, true); void setup() { From bdefbf23bc54f07181bf2e6d962c6d73afeb39b6 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 2 Jan 2024 22:02:46 +0100 Subject: [PATCH 3/4] publish any change in temperature, but display only if difference is more than 0.2 --- src/esp_clock.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index fe7fe4f..f95dd8e 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -29,12 +29,15 @@ Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { }, &ts); Task tReadBmp(TASK_MINUTE, TASK_FOREVER, []() { static float lastTemp = 0; + float temp = Bmp::data.temp; Bmp::data.readAll(); - if (abs(lastTemp - Bmp::data.temp) <= 0.2) return; - lastTemp = Bmp::data.temp; - Display::displayTemp(Bmp::data.temp); + if (temp == Bmp::data.temp) return; Mqtt::publishBmp280(); -}, &ts, true); + if (abs(lastTemp - Bmp::data.temp) > 0.2) { + lastTemp = Bmp::data.temp; + Display::displayTemp(Bmp::data.temp); + } +}, &ts); void setup() { @@ -55,6 +58,8 @@ void setup() { pinMode(BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); attachInterrupt(digitalPinToInterrupt(LED_BUILTIN), onLed, CHANGE); + + tReadBmp.enable(); } void loop() { From 2bb9de1f6141cf3204383a6d8cd165271f9a65f0 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 2 Jan 2024 22:06:31 +0100 Subject: [PATCH 4/4] simplify display time task: - drawTime onEnable - disable drawColon onDisable - don't display time at startup, by default temperature is displayed now --- include/display.h | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/include/display.h b/include/display.h index 1369549..21218cd 100644 --- a/include/display.h +++ b/include/display.h @@ -12,16 +12,20 @@ #define BRIGHTNESS_STEP 1 #define BRIGHTNESS_NIGHT BRIGHTNESS_MIN #define BRIGHTNESS_DAY 11 -#define DISPLAY_TIME 2000 -#define DISPLAY_TEMP_TIME 5000 +#define SECONDS(value) value*1000 namespace Display { - void display(); - Task tDisplay(500, TASK_FOREVER, display, &ts, true); + void displayTime(); + void drawTime(); + void drawColon(bool); + Task tDisplayTime(500, TASK_FOREVER, displayTime, &ts, false, []() { + drawTime(); + return true; + }, []() { + drawColon(false); + }); - int currentHour = -1; - int currentMin = -1; bool hourFormat24 = false; void (*hourFormatChangedCallback)(); void onHourFormatChanged(void (*f)()) { @@ -71,6 +75,7 @@ namespace Display { void drawColon(bool colonOn) { if (colonOn) { + static int currentHour = -1; if (currentHour != hour()) { currentHour = hour(); if (currentHour == 8) { @@ -81,6 +86,7 @@ namespace Display { Brightness::set(BRIGHTNESS_NIGHT); } } + static int currentMin = -1; if (currentMin != minute()) { currentMin = minute(); drawTime(); @@ -89,8 +95,8 @@ namespace Display { clockDisplay.drawColon(colonOn); } - void display() { - static bool colonOn = false; + void displayTime() { + static bool colonOn = true; drawColon(colonOn); clockDisplay.writeDisplay(); @@ -99,30 +105,24 @@ namespace Display { } void displayTemp(float value) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.printFloat(value, 1); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TEMP_TIME); + tDisplayTime.enableDelayed(SECONDS(10)); } void displayValue(uint8_t value) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.print(value, HEX); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TIME); + tDisplayTime.enableDelayed(SECONDS(2)); } void displayText(const char c[]) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.println(c); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TIME); + tDisplayTime.enableDelayed(SECONDS(2)); } void changeHourFormat24(bool format24) { @@ -134,7 +134,5 @@ namespace Display { void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(Brightness::current); - drawTime(); - display(); } }