espMqttClient: Reconnect does not work if the broker is offline for a longer time (>1,5h)

I currently face the problem, that if the broker goes offline the library tries to reconnect for about 1,5h (a second test took about 5 hours until it stopped) and then it stops reconnecting. It seems that the disconnect event is missing. I’ve implemented it as in your previous examples using a timer in the disconnect event. (Please see code samples below)

If I stop the machine with the broker I get the following output (as expected):

21:40:47.157 > Disconnected from MQTT.
21:40:47.159 > Disconnect reason:TCP_DISCONNECTED
21:40:48.509 > Fetch inverter: 116480145268
21:40:48.530 > Request SystemConfigPara
21:40:49.159 > Connecting to MQTT...
21:40:52.174 > Disconnected from MQTT.
21:40:52.176 > Disconnect reason:TCP_DISCONNECTED
21:40:54.176 > Connecting to MQTT...
21:40:57.186 > Disconnected from MQTT.
21:40:57.189 > Disconnect reason:TCP_DISCONNECTED
21:40:59.188 > Connecting to MQTT...
21:41:02.202 > Disconnected from MQTT.
21:41:02.204 > Disconnect reason:TCP_DISCONNECTED

This repeats again and again… If I start the broker quick enough (lets say after 10minutes) everything works fine again. For testing purposes I just let the broker offline and got the following behavior:

22:15:49.367 > Connecting to MQTT...
22:15:52.377 > Disconnected from MQTT.
22:15:52.398 > Disconnect reason:TCP_DISCONNECTED
22:15:54.380 > Connecting to MQTT...
22:15:56.580 > Request SystemConfigPara
22:15:57.390 > Disconnected from MQTT.
22:15:57.393 > Disconnect reason:TCP_DISCONNECTED
22:15:59.392 > Connecting to MQTT...
22:16:02.402 > Disconnected from MQTT.
22:16:02.405 > Disconnect reason:TCP_DISCONNECTED
22:16:04.404 > Connecting to MQTT...

Here the output just stops… no more reconnect attempts. The connection state returns false: https://github.com/tbnobody/OpenDTU/blob/9b0d2ff25f48fd39155894351d4069fba5900efa/src/MqttSettings.cpp#L150

Which platform, esp8266 or esp32? ESP32 Do you use TLS or not? NO TLS Do you use an IDE (Arduino, Platformio…)? PlatformIO Which version of the Arduino framework? espressif32@6.3.1

Example code Connect: https://github.com/tbnobody/OpenDTU/blob/9b0d2ff25f48fd39155894351d4069fba5900efa/src/MqttSettings.cpp#L127 Disconnect event: https://github.com/tbnobody/OpenDTU/blob/9b0d2ff25f48fd39155894351d4069fba5900efa/src/MqttSettings.cpp#L52-L81

Other Remarks Not sure why you removed the example with the timer in the disconnect event. I could also add a workaround to check wether the connection state is disconnected and the last connect attempt is older then e.g. 1 minute. I am just not sure what should be the right behavior.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 21 (19 by maintainers)

Most upvoted comments

I have just applied this patch:

diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp
index bae592e..256fc2f 100644
--- a/src/MqttClient.cpp
+++ b/src/MqttClient.cpp
@@ -101,7 +101,7 @@ bool MqttClient::disconnected() const {
 }

 bool MqttClient::connect() {
-  bool result = true;
+  bool result = false;
   if (_state == State::disconnected) {
     EMC_SEMAPHORE_TAKE();
     if (_addPacketFront(_cleanSession,
@@ -114,17 +114,17 @@ bool MqttClient::connect() {
                         _willPayloadLength,
                         (uint16_t)(_keepAlive / 1000),  // 32b to 16b doesn't overflow because it comes from 16b orignally
                         _clientId)) {
+      result = true;
+      _state = State::connectingTcp1;
       #if defined(ARDUINO_ARCH_ESP32)
       if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
         vTaskResume(_taskHandle);
       }
       #endif
-      _state = State::connectingTcp1;
     } else {
       EMC_SEMAPHORE_GIVE();
       emc_log_e("Could not create CONNECT packet");
       _onError(0, Error::OUT_OF_MEMORY);
-      result = false;
     }
     EMC_SEMAPHORE_GIVE();
   }

Now waiting for something to happen (or not) 😃

Fixed in https://github.com/bertmelis/espMqttClient/releases/tag/v1.4.4

Please reopen if the fix doesn’t hold.

  • the return value has to be initialized to false and set to true only when successful (bug)
  • state has to be set before resuming the task (just-in-case-improvement)

Could you test with these changes? I can’t prepare a PR or branch myself. Still away from computer.