From 949b4f40d1138bead10c4fcc2207b2514f78712e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 19 Jan 2023 23:14:25 +0100 Subject: [PATCH] control led over mqtt --- include/ha.h | 9 +++++++++ include/mqtt.h | 30 +++++++++++++++++++++++++----- platformio.ini | 8 ++++---- src/esp_clock.cpp | 3 +++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/include/ha.h b/include/ha.h index 1950674..48ba942 100644 --- a/include/ha.h +++ b/include/ha.h @@ -76,4 +76,13 @@ namespace Ha { jsonDoc["command_topic"] = topic; serializeJson(jsonDoc, output); } + + void buildLedConfig(char(&output)[JSON_SIZE], const char* topic) { + StaticJsonDocument jsonDoc; + buildDeviceConfig(jsonDoc); + jsonDoc["name"] = "ESP Clock Led"; + jsonDoc["unique_id"] = "esp_clock_led"; + jsonDoc["command_topic"] = topic; + serializeJson(jsonDoc, output); + } } diff --git a/include/mqtt.h b/include/mqtt.h index bfcebb0..dcac782 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -36,30 +36,33 @@ namespace Mqtt { const char* bmpTopic = "homeassistant/sensor/esp_clock/state"; const char* restartTopic = "homeassistant/button/esp_clock/restart"; + const char* ledTopic = "homeassistant/switch/esp_clock/led/set"; + const char* espClockTopic = "homeassistant/+/esp_clock/#"; void connect() { client.connect(); } void disconnect() { + client.unsubscribe(espClockTopic); client.disconnect(); } void publishTempConfig() { char message[JSON_SIZE]; - Ha::buildSensorConfig(message, Ha::TemperatureConfig{"Living room Temperature", "living_room_temperature", bmpTopic}); + Ha::buildSensorConfig(message, Ha::TemperatureConfig{ "Living room Temperature", "living_room_temperature", bmpTopic }); client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message); } void publishPressureConfig() { char message[JSON_SIZE]; - Ha::buildSensorConfig(message, Ha::PressureConfig{"Living room Pressure", "living_room_pressure", bmpTopic}); + Ha::buildSensorConfig(message, Ha::PressureConfig{ "Living room Pressure", "living_room_pressure", bmpTopic }); client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message); } void publishAltitudeConfig() { char message[JSON_SIZE]; - Ha::buildSensorConfig(message, Ha::AltitudeConfig{"Living room Altitude", "living_room_altitude", bmpTopic}); + Ha::buildSensorConfig(message, Ha::AltitudeConfig{ "Living room Altitude", "living_room_altitude", bmpTopic }); client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message); } @@ -69,12 +72,18 @@ namespace Mqtt { client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message); } + void publishLedConfig() { + char message[JSON_SIZE]; + Ha::buildLedConfig(message, ledTopic); + client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message); + } + void publishConfig() { publishTempConfig(); publishPressureConfig(); publishAltitudeConfig(); publishRestartConfig(); - client.subscribe(restartTopic, 0); + publishLedConfig(); ts.deleteTask(tPublishConfig); } @@ -102,9 +111,19 @@ namespace Mqtt { } void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - if (String{ restartTopic }.equals(topic) && String { payload }.equals("PRESS")) { + char msg[len + 1]; + memcpy(msg, payload, len); + msg[len] = 0; + if (String{ restartTopic }.equals(topic) && String { "PRESS" }.equals(msg)) { ESP.restart(); } + if (String{ ledTopic }.equals(topic)) { + if (String{ "ON" }.equals(msg)) { + digitalWrite(LED_BUILTIN, LOW); + } else { + digitalWrite(LED_BUILTIN, HIGH); + } + } } void setup() { @@ -112,6 +131,7 @@ namespace Mqtt { tPublishConfig.enable(); tPublishBmp.enableIfNot(); tPublishCommand.enableDelayed(); + client.subscribe(espClockTopic, 0); tReConnect.disable(); Serial.println("Connected to MQTT"); }); diff --git a/platformio.ini b/platformio.ini index 93247ec..799a95b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,7 +18,7 @@ lib_deps = adafruit/Adafruit GFX Library@^1.10.12 adafruit/Adafruit BusIO@^1.9.8 jchristensen/Timezone@^1.2.4 - ottowinter/AsyncMqttClient-esphome@^0.8.5 + marvinroger/AsyncMqttClient@^0.9.0 crankyoldgit/IRremoteESP8266@^2.7.18 arkhipenko/TaskScheduler@^3.7.0 adafruit/Adafruit Unified Sensor @ ^1.1.4 @@ -29,7 +29,7 @@ build_flags = -D IR=0 -D WIFI_ALWAYS_ON=1 [env:laptop_home] [env:ota_home] -upload_port = esp-clock +upload_port = 192.168.6.191 upload_protocol = espota -upload_flags = - --host_port=10000 +upload_flags = + --host_port=10000 diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 368307d..b8708af 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -26,6 +26,9 @@ void setup() { Serial.begin(9600); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); + Display::setup(); Ota::setup(); Ntp::setup();