134 lines
4.5 KiB
C++
134 lines
4.5 KiB
C++
#pragma once
|
|
|
|
#include <AsyncMqttClient.h>
|
|
#include <ArduinoJson.h>
|
|
#include "devices.h"
|
|
|
|
|
|
#define MQTT_HOST IPAddress(192, 168, 5, 11)
|
|
#define MQTT_PORT 1883
|
|
|
|
namespace Mqtt {
|
|
|
|
AsyncMqttClient client;
|
|
|
|
void publishInit();
|
|
void publishBme280();
|
|
void disconnect();
|
|
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
|
Serial.println("Connecting to MQTT...");
|
|
client.connect();
|
|
}, &ts);
|
|
Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts);
|
|
|
|
const char* mainTopic = "homeassistant/+/rc-gateway/#";
|
|
|
|
void disconnect() {
|
|
client.unsubscribe(mainTopic);
|
|
client.disconnect();
|
|
}
|
|
|
|
uint16_t publish(const char* topic, const char* message) {
|
|
return client.publish(topic, 0, true, message);
|
|
}
|
|
|
|
Ha::Sensor* sensors[] = {
|
|
// new Ha::TemperatureSensor{"Temperature", "temperature"},
|
|
// new Ha::HumiditySensor{"Humidity", "humidity"},
|
|
// new Ha::PressureSensor{"Pressure", "pressure"},
|
|
// new Ha::AltitudeSensor{"Altitude", "altitude"}
|
|
};
|
|
|
|
Ha::Button* buttons[] = {
|
|
new Ha::Button{"Restart", "restart",
|
|
[](const char* msg) {
|
|
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
|
}
|
|
}
|
|
};
|
|
|
|
Ha::Switch* switches[] = {
|
|
(new PollinSwitch{"Meeting sensor", "00001", 1})->withArea("Dining room")->withStateTopic(),
|
|
(new PollinSwitch{"Fire Tv", "00001", 2})->withArea("Living room")->withStateTopic(),
|
|
(new PollinSwitch{"Diningroom player", "00001", 3})->withArea("Dining room")->withStateTopic(),
|
|
(new PollinSwitch{"Train", "11111", 4})->withArea("Playroom")->withStateTopic(),
|
|
(new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }})->withArea("Basement")->withStateTopic(),
|
|
(new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }})->withArea("Basement")->withStateTopic()
|
|
};
|
|
|
|
void publishComponentConfig(Ha::Component& component) {
|
|
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
|
component.buildConfig(jsonDoc);
|
|
|
|
char message[JSON_SIZE];
|
|
serializeJson(jsonDoc, message);
|
|
|
|
publish(component.configTopic, message);
|
|
}
|
|
|
|
void publishInit() {
|
|
// for (Ha::Component* cmp : sensors) {
|
|
// publishComponentConfig(*cmp);
|
|
// }
|
|
Ha::publisher = publish;
|
|
for (Ha::Component* cmp : buttons) {
|
|
publishComponentConfig(*cmp);
|
|
}
|
|
for (Ha::Switch* cmp : switches) {
|
|
cmp->addToMap();
|
|
publishComponentConfig(*cmp);
|
|
}
|
|
ts.deleteTask(tPublishInit);
|
|
}
|
|
|
|
void publishCleanupConfig() {
|
|
for (List<Ha::Component>::Container* c = Ha::Component::configs.first; c; c = c->next) {
|
|
publish(c->t->configTopic, "");
|
|
}
|
|
}
|
|
|
|
void publishBme280() {
|
|
// StaticJsonDocument<255> jsonDoc;
|
|
// jsonDoc["temperature"] = Bme::data.temp;
|
|
// jsonDoc["humidity"] = Bme::data.humidity;
|
|
// jsonDoc["pressure"] = Bme::data.pressure;
|
|
// jsonDoc["altitude"] = Bme::data.altitude;
|
|
// char message[255];
|
|
// serializeJson(jsonDoc, message);
|
|
// publish(Ha::Sensor::stateTopic, message);
|
|
}
|
|
|
|
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
|
char msg[len + 1];
|
|
memcpy(msg, payload, len);
|
|
msg[len] = 0;
|
|
for (Ha::Button* cmd : buttons) {
|
|
if (String{ cmd->commandTopic }.equals(topic) && cmd->f != nullptr) {
|
|
cmd->f(msg);
|
|
return;
|
|
}
|
|
}
|
|
for (Ha::Switch* cmd : switches) {
|
|
if (String{ cmd->commandTopic }.equals(topic)) {
|
|
cmd->onCommand(msg);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
client.onConnect([](bool sessionPresent) {
|
|
tPublishInit.enable();
|
|
client.subscribe(mainTopic, 0);
|
|
tReConnect.disable();
|
|
Serial.println("Connected to MQTT");
|
|
});
|
|
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
|
tReConnect.enableDelayed();
|
|
Serial.println("Disconnected from MQTT");
|
|
});
|
|
client.onMessage(onMessage);
|
|
client.setServer(MQTT_HOST, MQTT_PORT);
|
|
}
|
|
}
|