publish brightness changes on MQTT
This commit is contained in:
parent
c2cc10c2cd
commit
ad955e5a7d
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <Adafruit_BMP280.h>
|
||||
|
||||
namespace Bmp {
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <SPI.h>
|
||||
#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()) {
|
||||
|
||||
11
include/ha.h
11
include/ha.h
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#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);
|
||||
|
||||
@ -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"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user