diff --git a/include/bmp.h b/include/bmp.h index 5ce8f40..95c4ade 100644 --- a/include/bmp.h +++ b/include/bmp.h @@ -1,3 +1,5 @@ +#pragma once + #include namespace Bmp { diff --git a/include/display.h b/include/display.h index c61e68a..c193fff 100644 --- a/include/display.h +++ b/include/display.h @@ -5,6 +5,7 @@ #include #include #include "ntp_time.h" +#include "mqtt.h" #define DISPLAY_ADDRESS 0x70 #define BRIGHTNESS_MIN 0 @@ -15,6 +16,13 @@ #define DISPLAY_TIME 2000 #define DISPLAY_TEMP_TIME 5000 +namespace Mqtt { + void publishBrightness(); + bool connected(); +} + +Task tPublishBrightness(TASK_IMMEDIATE, TASK_ONCE, Mqtt::publishBrightness, &ts, false, Mqtt::connected); + namespace Display { void displayColon(); @@ -47,16 +55,17 @@ namespace Display { } } - void setBrightness(unsigned int value) { + void setBrightness(uint8_t value) { brightness = value % (BRIGHTNESS_MAX+1); clockDisplay.setBrightness(brightness); + tPublishBrightness.restart(); } void changeBrightness(bool increase) { increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP); } - void adjustBrightness() { + void updateBrightness() { if (currentHour > 8 && currentHour < 17) { setBrightness(BRIGHTNESS_DAY); } else { @@ -68,6 +77,7 @@ namespace Display { if (colonOn) { if (currentHour != hour()) { currentHour = hour(); + updateBrightness(); if (currentHour == 8) Wifi::reconnect(); } if (currentMin != minute()) { diff --git a/include/ha.h b/include/ha.h index 0adaf5e..3e7a7b7 100644 --- a/include/ha.h +++ b/include/ha.h @@ -1,3 +1,5 @@ +#pragma once + #include #include "display.h" @@ -9,11 +11,13 @@ namespace Ha { struct Component { const char* name; const char* id; + const char* type; char configTopic[TOPIC_SIZE]; Component(const char* name, const char* id, const char* type) { this->name = name; this->id = id; + this->type = type; sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id); } @@ -41,6 +45,7 @@ namespace Ha { typedef void (*onMessage)(const char* msg); struct Command : Component { char commandTopic[TOPIC_SIZE]; + char stateTopic[TOPIC_SIZE]; onMessage f; Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type) { @@ -54,6 +59,12 @@ namespace Ha { void buildConfig(JsonDocument& jsonDoc) override { Component::buildConfig(jsonDoc); jsonDoc["command_topic"] = commandTopic; + if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; + } + + Command* withStateTopic() { + sprintf(stateTopic, "homeassistant/%s/esp_clock/%s/state", type, id); + return this; } }; diff --git a/include/mqtt.h b/include/mqtt.h index 09c36dc..f4a8190 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -1,7 +1,11 @@ +#pragma once + #include #include #include #include "ha.h" +#include "bmp.h" +#include "display.h" #define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_PORT 1883 @@ -36,6 +40,10 @@ namespace Mqtt { const char* espClockTopic = "homeassistant/+/esp_clock/#"; + bool connected() { + return client.connected(); + } + void connect() { client.connect(); } @@ -51,6 +59,12 @@ namespace Mqtt { new Ha::AltitudeSensor{"Living room Altitude", "altitude"} }; + Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness", + [](const char* msg) { + Display::setBrightness(String{ msg }.toInt()); + } + })->withStateTopic(); + Ha::Command* switches[] = { new Ha::Button{"ESP Clock Restart", "restart", [](const char* msg) { @@ -73,11 +87,7 @@ namespace Mqtt { } } }, - new Ha::Brightness{"ESP Clock Brightness", "brightness", - [](const char* msg) { - Display::setBrightness(String{ msg }.toInt()); - } - } + brightnessMqtt }; void publishComponentConfig(Ha::Component& component) { @@ -97,6 +107,7 @@ namespace Mqtt { for (Ha::Component* cmp : switches) { publishComponentConfig(*cmp); } + // publishBrightness(Display::brightness); ts.deleteTask(tPublishConfig); } @@ -111,6 +122,12 @@ namespace Mqtt { client.publish(Ha::Sensor::stateTopic, 0, true, message); } + void publishBrightness() { + char message[32]; + sprintf(message, "%X", Display::brightness); + client.publish(brightnessMqtt->stateTopic, 0, true, message); + } + void publishCommand() { if (uint8_t cmd = commands.getCurrent()) { char message[32]; @@ -140,6 +157,7 @@ namespace Mqtt { tPublishConfig.enable(); tPublishBmp.enableIfNot(); tPublishCommand.enableDelayed(); + tPublishBrightness.enable(); client.subscribe(espClockTopic, 0); tReConnect.disable(); Serial.println("Connected to MQTT"); @@ -148,6 +166,7 @@ namespace Mqtt { tReConnect.enableDelayed(); tPublishCommand.disable(); tPublishBmp.disable(); + tPublishBrightness.disable(); Serial.println("Disconnected from MQTT"); }); client.onMessage(onMessage); diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index b8708af..da8f654 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -12,10 +12,10 @@ Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts); Task tButton(TASK_IMMEDIATE, TASK_ONCE, onButtonCallback, &ts); #include "wifi.h" +#include "mqtt.h" #include "display.h" #include "bmp.h" #include "ntp_time.h" -#include "mqtt.h" #include "ota.h" #include "ir.h"