publish brightness changes on MQTT

This commit is contained in:
Nicu Hodos 2023-07-10 00:01:40 +02:00
parent c2cc10c2cd
commit ad955e5a7d
5 changed files with 50 additions and 8 deletions

View File

@ -1,3 +1,5 @@
#pragma once
#include <Adafruit_BMP280.h> #include <Adafruit_BMP280.h>
namespace Bmp { namespace Bmp {

View File

@ -5,6 +5,7 @@
#include <Adafruit_I2CDevice.h> #include <Adafruit_I2CDevice.h>
#include <SPI.h> #include <SPI.h>
#include "ntp_time.h" #include "ntp_time.h"
#include "mqtt.h"
#define DISPLAY_ADDRESS 0x70 #define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS_MIN 0 #define BRIGHTNESS_MIN 0
@ -15,6 +16,13 @@
#define DISPLAY_TIME 2000 #define DISPLAY_TIME 2000
#define DISPLAY_TEMP_TIME 5000 #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 { namespace Display {
void displayColon(); void displayColon();
@ -47,16 +55,17 @@ namespace Display {
} }
} }
void setBrightness(unsigned int value) { void setBrightness(uint8_t value) {
brightness = value % (BRIGHTNESS_MAX+1); brightness = value % (BRIGHTNESS_MAX+1);
clockDisplay.setBrightness(brightness); clockDisplay.setBrightness(brightness);
tPublishBrightness.restart();
} }
void changeBrightness(bool increase) { void changeBrightness(bool increase) {
increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP); increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP);
} }
void adjustBrightness() { void updateBrightness() {
if (currentHour > 8 && currentHour < 17) { if (currentHour > 8 && currentHour < 17) {
setBrightness(BRIGHTNESS_DAY); setBrightness(BRIGHTNESS_DAY);
} else { } else {
@ -68,6 +77,7 @@ namespace Display {
if (colonOn) { if (colonOn) {
if (currentHour != hour()) { if (currentHour != hour()) {
currentHour = hour(); currentHour = hour();
updateBrightness();
if (currentHour == 8) Wifi::reconnect(); if (currentHour == 8) Wifi::reconnect();
} }
if (currentMin != minute()) { if (currentMin != minute()) {

View File

@ -1,3 +1,5 @@
#pragma once
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "display.h" #include "display.h"
@ -9,11 +11,13 @@ namespace Ha {
struct Component { struct Component {
const char* name; const char* name;
const char* id; const char* id;
const char* type;
char configTopic[TOPIC_SIZE]; char configTopic[TOPIC_SIZE];
Component(const char* name, const char* id, const char* type) { Component(const char* name, const char* id, const char* type) {
this->name = name; this->name = name;
this->id = id; this->id = id;
this->type = type;
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id); sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id);
} }
@ -41,6 +45,7 @@ namespace Ha {
typedef void (*onMessage)(const char* msg); typedef void (*onMessage)(const char* msg);
struct Command : Component { struct Command : Component {
char commandTopic[TOPIC_SIZE]; char commandTopic[TOPIC_SIZE];
char stateTopic[TOPIC_SIZE];
onMessage f; onMessage f;
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type) { 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 { void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc); Component::buildConfig(jsonDoc);
jsonDoc["command_topic"] = commandTopic; 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;
} }
}; };

View File

@ -1,7 +1,11 @@
#pragma once
#include <queue> #include <queue>
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "ha.h" #include "ha.h"
#include "bmp.h"
#include "display.h"
#define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_HOST IPAddress(192, 168, 5, 11)
#define MQTT_PORT 1883 #define MQTT_PORT 1883
@ -36,6 +40,10 @@ namespace Mqtt {
const char* espClockTopic = "homeassistant/+/esp_clock/#"; const char* espClockTopic = "homeassistant/+/esp_clock/#";
bool connected() {
return client.connected();
}
void connect() { void connect() {
client.connect(); client.connect();
} }
@ -51,6 +59,12 @@ namespace Mqtt {
new Ha::AltitudeSensor{"Living room Altitude", "altitude"} 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[] = { Ha::Command* switches[] = {
new Ha::Button{"ESP Clock Restart", "restart", new Ha::Button{"ESP Clock Restart", "restart",
[](const char* msg) { [](const char* msg) {
@ -73,11 +87,7 @@ namespace Mqtt {
} }
} }
}, },
new Ha::Brightness{"ESP Clock Brightness", "brightness", brightnessMqtt
[](const char* msg) {
Display::setBrightness(String{ msg }.toInt());
}
}
}; };
void publishComponentConfig(Ha::Component& component) { void publishComponentConfig(Ha::Component& component) {
@ -97,6 +107,7 @@ namespace Mqtt {
for (Ha::Component* cmp : switches) { for (Ha::Component* cmp : switches) {
publishComponentConfig(*cmp); publishComponentConfig(*cmp);
} }
// publishBrightness(Display::brightness);
ts.deleteTask(tPublishConfig); ts.deleteTask(tPublishConfig);
} }
@ -111,6 +122,12 @@ namespace Mqtt {
client.publish(Ha::Sensor::stateTopic, 0, true, message); 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() { void publishCommand() {
if (uint8_t cmd = commands.getCurrent()) { if (uint8_t cmd = commands.getCurrent()) {
char message[32]; char message[32];
@ -140,6 +157,7 @@ namespace Mqtt {
tPublishConfig.enable(); tPublishConfig.enable();
tPublishBmp.enableIfNot(); tPublishBmp.enableIfNot();
tPublishCommand.enableDelayed(); tPublishCommand.enableDelayed();
tPublishBrightness.enable();
client.subscribe(espClockTopic, 0); client.subscribe(espClockTopic, 0);
tReConnect.disable(); tReConnect.disable();
Serial.println("Connected to MQTT"); Serial.println("Connected to MQTT");
@ -148,6 +166,7 @@ namespace Mqtt {
tReConnect.enableDelayed(); tReConnect.enableDelayed();
tPublishCommand.disable(); tPublishCommand.disable();
tPublishBmp.disable(); tPublishBmp.disable();
tPublishBrightness.disable();
Serial.println("Disconnected from MQTT"); Serial.println("Disconnected from MQTT");
}); });
client.onMessage(onMessage); client.onMessage(onMessage);

View File

@ -12,10 +12,10 @@ Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
Task tButton(TASK_IMMEDIATE, TASK_ONCE, onButtonCallback, &ts); Task tButton(TASK_IMMEDIATE, TASK_ONCE, onButtonCallback, &ts);
#include "wifi.h" #include "wifi.h"
#include "mqtt.h"
#include "display.h" #include "display.h"
#include "bmp.h" #include "bmp.h"
#include "ntp_time.h" #include "ntp_time.h"
#include "mqtt.h"
#include "ota.h" #include "ota.h"
#include "ir.h" #include "ir.h"