decouple mqtt:

- register onConnected and onDisconnected callbacks
- allow optional Scheduler
- publishInit is independent on Scheduler
This commit is contained in:
Nicu Hodos 2024-05-22 13:42:52 +02:00
parent 4693969aff
commit c03c3d03fd
3 changed files with 34 additions and 33 deletions

View File

@ -1,13 +1,15 @@
#include <TaskScheduler.h> #include <TaskScheduler.h>
#define MQTT_HOST IPAddress(192, 168, 5, 11)
#define MQTT_PORT 1883
using namespace std; using namespace std;
Scheduler ts; Scheduler ts;
void turnLed(uint8_t led, bool on = true) { #include "devices.h"
on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH); #include "mqtt.h"
} #include "ota.h"
#include "wifi.h" #include "wifi.h"
namespace Board { namespace Board {
@ -19,6 +21,10 @@ namespace Board {
} }
}, &ts); }, &ts);
void turnLed(uint8_t led, bool on = true) {
on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH);
}
void setup() { void setup() {
// Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY); // Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
@ -27,9 +33,12 @@ namespace Board {
turnLed(RED_LED, false); turnLed(RED_LED, false);
turnLed(BLUE_LED); turnLed(BLUE_LED);
Mqtt::setup(&ts,
[] {turnLed(BLUE_LED, false);},
[] {turnLed(BLUE_LED);}
);
Wifi::setup(); Wifi::setup();
Ota::setup(); Ota::setup();
Mqtt::setup();
tReadCommand.enable(); tReadCommand.enable();
} }

View File

@ -2,27 +2,20 @@
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "devices.h"
#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#"
#define MQTT_HOST IPAddress(192, 168, 5, 11)
#define MQTT_PORT 1883
namespace Mqtt { namespace Mqtt {
AsyncMqttClient client; AsyncMqttClient client;
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []{ Task tReConnect(TASK_MINUTE, TASK_FOREVER, []{
Serial.println("Connecting to MQTT..."); Serial.println("Connecting to MQTT...");
client.connect(); client.connect();
}, &ts); });
void publishInit();
Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts);
const char* mainTopic = "homeassistant/+/rc-gateway/#";
void disconnect() { void disconnect() {
client.unsubscribe(mainTopic); client.unsubscribe(MAIN_TOPIC);
client.disconnect(); client.disconnect();
} }
@ -31,12 +24,12 @@ namespace Mqtt {
} }
void publishInit() { void publishInit() {
#if MQTT_CLEANUP static bool firstTime = true;
Ha::publisher = publish; if (firstTime) {
Component::components.forEach([](Component* c) { c->publishConfig(); }); Component::components.forEach([](Component* c) { c->publishConfig(); });
AbstractBuilder::deleteAll(); AbstractBuilder::deleteAll();
#endif firstTime = false;
ts.deleteTask(tPublishInit); }
} }
void publishCleanupConfig() { void publishCleanupConfig() {
@ -53,18 +46,20 @@ namespace Mqtt {
if (cmd) cmd->onCommand(msg); if (cmd) cmd->onCommand(msg);
} }
void setup() { void setup(Scheduler* ts = nullptr, void(*onConnected)() = nullptr, void(*onDisconnected)() = nullptr) {
client.onConnect([](bool sessionPresent) { if (ts) ts->addTask(tReConnect);
tPublishInit.enable(); Ha::publisher = publish;
client.subscribe(mainTopic, 0); client.onConnect([onConnected](bool sessionPresent) {
publishInit();
client.subscribe(MAIN_TOPIC, 0);
tReConnect.disable(); tReConnect.disable();
Serial.println("Connected to MQTT"); Serial.println("Connected to MQTT");
turnLed(BLUE_LED, false); if (onConnected) onConnected();
}); });
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { client.onDisconnect([onDisconnected](AsyncMqttClientDisconnectReason reason) {
tReConnect.enableDelayed(); tReConnect.enableDelayed();
Serial.println("Disconnected from MQTT"); Serial.println("Disconnected from MQTT");
turnLed(BLUE_LED); if (onDisconnected) onDisconnected();
}); });
client.onMessage(onMessage); client.onMessage(onMessage);
client.setServer(MQTT_HOST, MQTT_PORT); client.setServer(MQTT_HOST, MQTT_PORT);

View File

@ -1,8 +1,6 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include "mqtt.h"
#include "ota.h"
#include "credentials.h" #include "credentials.h"
namespace Wifi { namespace Wifi {
@ -30,12 +28,11 @@ namespace Wifi {
printStatus(); printStatus();
tReconnect.cancel(); tReconnect.cancel();
Ota::tLoop.enable(); Ota::tLoop.enable();
Mqtt::tReConnect.restart(); Mqtt::tReConnect.enable();
}); });
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
Serial.println("Disconnected from network."); Serial.println("Disconnected from network.");
tReconnect.restartDelayed();
}); });