From bbcf0a400be0060db6083068eedce53b7f61fba0 Mon Sep 17 00:00:00 2001 From: David Panusch Date: Mon, 22 Apr 2024 10:33:48 +0200 Subject: [PATCH 1/3] Clang formated Discription --- include/mqtt.hpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/include/mqtt.hpp b/include/mqtt.hpp index 1624bdb..c2540b6 100644 --- a/include/mqtt.hpp +++ b/include/mqtt.hpp @@ -134,16 +134,16 @@ void Mqtt::loop() { /* Description: -This function handles incoming MQTT messages received from the broker. It parses -the payload as JSON data and updates the device's state and parameters -accordingly. +This function handles incoming MQTT messages received from the broker. It +parses the payload as JSON data and updates the device's state and +parameters accordingly. Input: -char *topic: A pointer to a character array containing the topic of the received -message. byte *payload: A pointer to an array of bytes containing the payload of -the received message. unsigned int length: The length of the payload in bytes. -Output: +char *topic: A pointer to a character array containing the topic of the +received message. byte *payload: A pointer to an array of bytes containing +the payload of the received message. unsigned int length: The length of the +payload in bytes. Output: None */ @@ -230,11 +230,11 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) { /* Description: -This function is responsible for publishing the current state of the device to -an MQTT topic. It constructs a JSON message containing information about the -device state, such as the power state (ON or OFF), color settings, and -brightness. The constructed JSON message is then published to the MQTT broker on -a specified topic. +This function is responsible for publishing the current state of the device +to an MQTT topic. It constructs a JSON message containing information about +the device state, such as the power state (ON or OFF), color settings, and +brightness. The constructed JSON message is then published to the MQTT +broker on a specified topic. Input: @@ -269,8 +269,8 @@ void Mqtt::sendDiscovery() { This function publishes MQTT discovery messages for Home Assistant, providing configuration details for a light entity. It constructs a JSON - payload according to Home Assistant's MQTT discovery format and publishes it - to the appropriate topic. + payload according to Home Assistant's MQTT discovery format and + publishes it to the appropriate topic. Input: From 096784d82edd02bcf7b898b432802de94f688ec4 Mon Sep 17 00:00:00 2001 From: David Panusch Date: Mon, 22 Apr 2024 10:35:23 +0200 Subject: [PATCH 2/3] Updated MQTT ReInit --- include/clockWork.hpp | 4 ---- include/mqtt.h | 3 +-- include/mqtt.hpp | 45 +++++++++++++++++++------------------------ 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/include/clockWork.hpp b/include/clockWork.hpp index 3bbf149..ddad604 100644 --- a/include/clockWork.hpp +++ b/include/clockWork.hpp @@ -1174,10 +1174,6 @@ void ClockWork::loop(struct tm &tm) { } delay(100); - if (G.mqtt.state && !mqtt.isConnected()) { - mqtt.reInit(); - } - eeprom::write(); break; } diff --git a/include/mqtt.h b/include/mqtt.h index 3f8e291..de49ec3 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -4,7 +4,7 @@ class Mqtt { private: - void reconnect(); + void reInit(); static void callback(char *topic, byte *payload, unsigned int length); public: @@ -12,7 +12,6 @@ class Mqtt { ~Mqtt() = default; void init(); - void reInit(); void loop(); void sendState(); void sendDiscovery(); diff --git a/include/mqtt.hpp b/include/mqtt.hpp index c2540b6..247bd99 100644 --- a/include/mqtt.hpp +++ b/include/mqtt.hpp @@ -6,6 +6,8 @@ #include #define HOMEASSISTANT_DISCOVERY_TOPIC "homeassistant" +#define RETRY_INTERVALL 10000 // Seconds +#define MAX_RETRIES 50 extern WiFiClient client; @@ -83,8 +85,23 @@ None */ void Mqtt::reInit() { - mqttClient.connect(G.mqtt.clientId, G.mqtt.user, G.mqtt.password); - reconnect(); + static uint8_t retryCount = 0; + static ulong lastRetryTime = 0; + + if (millis() - lastRetryTime >= RETRY_INTERVALL) { + retryCount++; + Serial.print("Reconnecting to MQTT Server. Try "); + Serial.println(retryCount); + lastRetryTime = millis(); + + init(); + + // Check if maximum retries reached + if (retryCount >= MAX_RETRIES) { + Serial.println("Maximum retries reached."); + G.mqtt.state = 0; + } + } } //------------------------------------------------------------------------------ @@ -125,7 +142,7 @@ None void Mqtt::loop() { if (!isConnected()) { - reconnect(); + reInit(); } mqttClient.loop(); } @@ -360,25 +377,3 @@ void Mqtt::sendDiscovery() { .c_str(), buffer, true); } - -//------------------------------------------------------------------------------ - -/* Description: - -This function is called upon successful reconnection to the MQTT broker. It -performs post-connection tasks, such as subscribing to specific topics. - -Input: - -None -Output: - -None -*/ - -void Mqtt::reconnect() { - // Subscribe to the desired topic - mqttClient.subscribe((std::string(G.mqtt.topic) + "/cmd").c_str()); - Serial.println("MQTT Connected..."); - delay(100); -} From 0bbee931b2dbd5b33dc8659767a0408c63effe7e Mon Sep 17 00:00:00 2001 From: David Panusch Date: Tue, 23 Apr 2024 00:22:27 +0200 Subject: [PATCH 3/3] Refined MQTT reconnect --- include/mqtt.hpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/include/mqtt.hpp b/include/mqtt.hpp index 247bd99..65fe2e8 100644 --- a/include/mqtt.hpp +++ b/include/mqtt.hpp @@ -6,8 +6,11 @@ #include #define HOMEASSISTANT_DISCOVERY_TOPIC "homeassistant" -#define RETRY_INTERVALL 10000 // Seconds -#define MAX_RETRIES 50 + +#define RETRY_INTERVALL_WITHIN_5_MINUTES 15000 // 15 Seconds +#define MAX_RETRIES_WITHIN_5_MINUTES 20 + +#define RETRY_INTERVALL 3600000 // 1 Hour extern WiFiClient client; @@ -64,8 +67,12 @@ void Mqtt::init() { } else { mqttClient.connect(G.mqtt.clientId, G.mqtt.user, G.mqtt.password); } - delay(100); + delay(50); mqttClient.subscribe((std::string(G.mqtt.topic) + "/cmd").c_str()); + delay(50); + if (isConnected()) { + Serial.println("MQTT Connected"); + } } //------------------------------------------------------------------------------ @@ -86,9 +93,10 @@ None void Mqtt::reInit() { static uint8_t retryCount = 0; - static ulong lastRetryTime = 0; + static uint32_t lastRetryTime = 0; + static uint32_t retryIntervall = RETRY_INTERVALL_WITHIN_5_MINUTES; - if (millis() - lastRetryTime >= RETRY_INTERVALL) { + if (millis() - lastRetryTime >= retryIntervall) { retryCount++; Serial.print("Reconnecting to MQTT Server. Try "); Serial.println(retryCount); @@ -97,9 +105,15 @@ void Mqtt::reInit() { init(); // Check if maximum retries reached - if (retryCount >= MAX_RETRIES) { - Serial.println("Maximum retries reached."); - G.mqtt.state = 0; + if (retryCount >= MAX_RETRIES_WITHIN_5_MINUTES) { + Serial.println("Switched to hourly MQTT connect retry"); + retryIntervall = RETRY_INTERVALL; + retryCount = 0; + } + + if (isConnected()) { + retryCount = 0; + retryIntervall = RETRY_INTERVALL_WITHIN_5_MINUTES; } } }