From e8b8c42959cd8f5e93faeb9ddb0f67e18929c4a9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 11 Jul 2023 13:14:57 +0200 Subject: [PATCH] publish state of led --- include/mqtt.h | 14 ++++++++------ src/esp_clock.cpp | 7 +++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index b8fb634..085d196 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -55,11 +55,17 @@ namespace Mqtt { new Ha::AltitudeSensor{"Living room Altitude", "altitude"} }; + Ha::Command* ledMqtt = (new Ha::Switch{"ESP Clock Led", "led", + [](const char* msg) { + String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); + } + })->withStateTopic(); + Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness", [](const char* msg) { Display::Brightness::set(String{ msg }.toInt()); } - })->withStateTopic(); + })->withStateTopic(); Ha::Command* switches[] = { new Ha::Button{"ESP Clock Restart", "restart", @@ -67,11 +73,7 @@ namespace Mqtt { if (String { "PRESS" }.equals(msg)) ESP.restart(); } }, - new Ha::Switch{"ESP Clock Led", "led", - [](const char* msg) { - String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); - } - }, + ledMqtt, new Ha::Switch{"ESP Clock Format 24h", "format_24h", [](const char* msg) { if (String{ "ON" }.equals(msg)) { diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index da8f654..7d9c4ff 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -3,6 +3,7 @@ void checkWifiCallback(); void onWifiConnected(); void onButtonPressed(); +void onLed(); void onButtonCallback(); #include @@ -20,6 +21,7 @@ Task tButton(TASK_IMMEDIATE, TASK_ONCE, onButtonCallback, &ts); #include "ir.h" #define BUTTON D3 +#define LED D4 void setup() { @@ -40,6 +42,7 @@ void setup() { pinMode(BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); + attachInterrupt(digitalPinToInterrupt(LED), onLed, CHANGE); } void loop() { @@ -70,6 +73,10 @@ ICACHE_RAM_ATTR void onButtonPressed() { tButton.restart(); } +ICACHE_RAM_ATTR void onLed() { + Mqtt::publish(Mqtt::ledMqtt->stateTopic, digitalRead(LED) ? "OFF" : "ON"); +} + void onButtonCallback() { Display::displayTemp(Bmp::data.readTemp()); }