From 9cd5b7132b618d43ea918e7468256134f0b3713a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 31 Dec 2023 20:49:22 +0700 Subject: [PATCH] mqtt_dbg --- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 3 ++- ESPMegaPRO-firmware/src/main.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp index 2384bfc..995351d 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -454,7 +454,8 @@ MqttConfig *ESPMegaIoT::getMqttConfig() bool ESPMegaIoT::mqttConnected() { - return mqtt_connected; + //return mqtt_connected; + return mqtt.connected(); } bool ESPMegaIoT::networkConnected() diff --git a/ESPMegaPRO-firmware/src/main.cpp b/ESPMegaPRO-firmware/src/main.cpp index 8a07e59..887c441 100644 --- a/ESPMegaPRO-firmware/src/main.cpp +++ b/ESPMegaPRO-firmware/src/main.cpp @@ -4,6 +4,7 @@ #include // #define FRAM_DEBUG +#define MQTT_DEBUG // Demo PLC firmware using the ESPMegaPRO OOP library @@ -122,4 +123,21 @@ void loop() espmega.dumpFRAMtoSerialASCII(0, 500); } #endif + +// Every 5 seconds, publish "I'm alive" to MQTT +#ifdef MQTT_DEBUG + static uint32_t last_mqtt_publish = 0; + if (millis() - last_mqtt_publish >= 5000) + { + last_mqtt_publish = millis(); + espmega.iot->publishToTopic("/espmegai/alive", "true"); + } + static uint32_t last_mqtt_status = 0; + if (millis() - last_mqtt_status >= 1000) + { + last_mqtt_status = millis(); + Serial.print("MQTT Status: "); + Serial.println(espmega.iot->mqttConnected() ? "Connected" : "Disconnected"); + } +#endif } \ No newline at end of file