#pragma once #include #include #include "devices.h" #define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_PORT 1883 namespace Mqtt { AsyncMqttClient client; void publishInit(); 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::Button* buttons[] = { (new Ha::Button{"Restart", "restart", [](const char* msg) { if (String { "PRESS" }.equals(msg)) ESP.restart(); } })->asDevice(gatewayDevice) }; 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() }; Ha::Sensor* sensors[] = { SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")), OilSensorBuilder::build("7") }; void publishComponentConfig(Ha::Component& component) { StaticJsonDocument jsonDoc; component.buildConfig(jsonDoc); char message[JSON_SIZE]; serializeJson(jsonDoc, message); publish(component.configTopic, message); } void publishInit() { Ha::publisher = publish; for (List::Container* c = Ha::Component::components.first; c; c = c->next) { publishComponentConfig(*c->t); } ts.deleteTask(tPublishInit); } void publishCleanupConfig() { for (List::Container* c = Ha::Component::components.first; c; c = c->next) { publish(c->t->configTopic, ""); } } 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); } }