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)
I have just applied this patch:
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.
Could you test with these changes? I can’t prepare a PR or branch myself. Still away from computer.