From 91571eafccb329ec1e7c96320c9b7a974bb426ea Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sat, 19 Aug 2017 22:22:36 +0200 Subject: [PATCH] Limit number of MQTT connection tries as proposed by @nimbl at https://github.com/toblum/McLighting/issues/22 --- Arduino/McLighting/McLighting.ino | 10 ++++++---- Arduino/McLighting/definitions.h | 4 ++++ Arduino/McLighting/request_handlers.h | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 53b5cc2..1a9dd38 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -303,7 +303,7 @@ void setup() { String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 4); String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 5); - DBG_OUTPUT_PORT.printf("Connect %s %d\n", mqtt_host, String(mqtt_port).toInt()); + DBG_OUTPUT_PORT.printf("MQTT active: %s:%d\n", mqtt_host, String(mqtt_port).toInt()); mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt()); mqtt_client.setCallback(mqtt_callback); @@ -532,16 +532,18 @@ void setup() { void loop() { server.handleClient(); webSocket.loop(); + #ifdef ENABLE_OTA ArduinoOTA.handle(); #endif #ifdef ENABLE_MQTT - if (mqtt_host != "" && String(mqtt_port).toInt() > 0) { + if (mqtt_host != "" && String(mqtt_port).toInt() > 0 && mqtt_reconnect_retries < MQTT_MAX_RECONNECT_TRIES) { if (!mqtt_client.connected()) { - mqtt_reconnect(); + mqtt_reconnect(); + } else { + mqtt_client.loop(); } - mqtt_client.loop(); } #endif diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 93311ff..41bd8b6 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -9,6 +9,10 @@ const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname #define ENABLE_MQTT // If defined, enable MQTT client code. #ifdef ENABLE_MQTT #define MQTT_MAX_PACKET_SIZE 256 + #define MQTT_MAX_RECONNECT_TRIES 4 + + int mqtt_reconnect_retries = 0; + char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 89c9fe8..c0819fb 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -390,11 +390,12 @@ void checkForRequests() { void mqtt_reconnect() { // Loop until we're reconnected - while (!mqtt_client.connected()) { - DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); + while (!mqtt_client.connected() && mqtt_reconnect_retries < MQTT_MAX_RECONNECT_TRIES) { + mqtt_reconnect_retries++; + DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES); // Attempt to connect if (mqtt_client.connect(mqtt_clientid, mqtt_user, mqtt_pass)) { - DBG_OUTPUT_PORT.println("connected!"); + DBG_OUTPUT_PORT.println("MQTT connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1]; strcpy(message, "McLighting ready: "); @@ -413,5 +414,8 @@ void checkForRequests() { delay(5000); } } + if (mqtt_reconnect_retries >= MQTT_MAX_RECONNECT_TRIES) { + DBG_OUTPUT_PORT.printf("MQTT connection failed, giving up after %d tries ...\n", mqtt_reconnect_retries); + } } #endif