From 167606a19ef4e373b20cbb1fc1ce9f0001f7fa95 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Mon, 6 Dec 2021 14:58:28 +0100 Subject: [PATCH] extract Mqtt functionality outside Ir --- include/ir.h | 43 ++++++------------------------------------- include/mqtt.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/esp_clock.cpp | 6 +++--- 3 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 include/mqtt.h diff --git a/include/ir.h b/include/ir.h index 0d534a7..7161f78 100644 --- a/include/ir.h +++ b/include/ir.h @@ -16,23 +16,8 @@ namespace Ir { decode_results results; bool avrOn = false; - AsyncMqttClient mqttClient; - - std::queue commands; uint8_t lastCommand = 0x9F; - void publishCommand() { - if (!commands.empty() && mqttClient.connected()) { - char message[32]; - sprintf(message, "%X", commands.front()); - if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { - Serial.print(commands.front(), HEX); - Serial.println(); - commands.pop(); - } - } - } - bool readCommand() { bool newCommand = false; if (irrecv.decode(&results)) { @@ -41,7 +26,7 @@ namespace Ir { Serial.print(results.command, HEX); Serial.println(); lastCommand = results.command; - commands.push(results.command); + Mqtt::commands.push(results.command); newCommand = true; } irrecv.resume(); // Receive the next value @@ -49,52 +34,36 @@ namespace Ir { return newCommand; } - uint8_t getCurrentCommand() { - return commands.empty() ? 0 : commands.front(); - } - void setup() { - mqttClient.setServer(MQTT_HOST, MQTT_PORT); - Serial.println("Connecting to MQTT..."); - mqttClient.connect(); - irrecv.enableIRIn(); // Start the receiver } void loop() { if (readCommand()) { Display::displayValue(lastCommand); - // Display::displayColon(false); - delay(1000); switch (lastCommand) { case 0x9F: avrOn = false; - Ntp::lastConnectedTime = now(); break; case 0xC4: case 0xD0: case 0xC0: avrOn = true; - if (WiFi.status() == WL_DISCONNECTED) { - Wifi::reconnect(); - // connect on wifi connected - mqttClient.connect(); - } + Wifi::reconnect(); break; default: break; } } - if (!avrOn && getCurrentCommand() == 0xC7) { + if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) { Display::changeBrightness(true); - commands.pop(); + Mqtt::commands.pop(); } - if (!avrOn && getCurrentCommand() == 0xC8) { + if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) { Display::changeBrightness(false); - commands.pop(); + Mqtt::commands.pop(); } - publishCommand(); } } diff --git a/include/mqtt.h b/include/mqtt.h new file mode 100644 index 0000000..06e9590 --- /dev/null +++ b/include/mqtt.h @@ -0,0 +1,42 @@ +#include +#include + +#define MQTT_HOST IPAddress(192, 168, 5, 138) +#define MQTT_PORT 1883 + +namespace Mqtt { + + void publishCommand(); + Task tPublish(TASK_SECOND, TASK_FOREVER, Mqtt::publishCommand, &ts); + + AsyncMqttClient client; + + std::queue commands; + + void publishCommand() { + if (!commands.empty() && client.connected()) { + char message[32]; + sprintf(message, "%X", commands.front()); + if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { + Serial.print(commands.front(), HEX); + Serial.println(); + commands.pop(); + } + } + } + + uint8_t getCurrentCommand() { + return commands.empty() ? 0 : commands.front(); + } + + void setup() { + client.onConnect([](bool sessionPresent) { + tPublish.enableDelayed(); + }); + client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { + tPublish.disable(); + }); + client.setServer(MQTT_HOST, MQTT_PORT); + Serial.println("Connecting to MQTT..."); + } +} diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 41bb091..1092ba9 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -9,6 +9,7 @@ StatusRequest wifiConnected; #include "wifi.h" #include "ntp_time.h" +#include "mqtt.h" #include "ota.h" #include "display.h" #include "ir.h" @@ -27,12 +28,10 @@ void setup() { Serial.begin(9600); Display::setup(); - Ota::setup(); - Ntp::setup(); - Ir::setup(); + Mqtt::setup(); hourChanged.setWaiting(); dayChanged.setWaiting(); @@ -56,6 +55,7 @@ void wifiConnectedCallback() { Ntp::lastConnectedTime = newTime; } tOta.enable(); + Mqtt::client.connect(); } void otaCallback() {