ct-Open-Source / Basecamp

An Arduino library to ease the use of the ESP32 in IoT projects
GNU General Public License v3.0
254 stars 48 forks source link

No Reconnect after MQTT Broker restart #62

Open Nephelo opened 6 years ago

Nephelo commented 6 years ago

Hello,

I have a problem that my Basecamp device doesn't automaticall reconnect to the MQTT Broker after the broker was restarted (e.g. after a server update).

Is there a possibility to configure Basecamp to automatically reconnect WiFi and MQTT after a connection lost?

Thank you very much.

obrain17 commented 6 years ago

Hi,

I also found out that polling the "MQTT Disconnect" too frequently is messing up the MQTT Library or TCP connection. Especially no attempts should be made to reconnect while the last reconnect actions have not completely finished. I added a delay of 30 seconds here: vTaskDelay(30000); // wait for all OnConnect Actions are completed In addition we should wait some 2 seconds after a disconnect has been detected: vTaskDelay(2000); // wait until try to connect ^= Timer 2 Seconds in example FullyFeatured-ESP32.ino

All in all polling the flag "mqtt->connected()", which is internally the variable "_connected" is not a good idea. It would be better to use a callback function "onMqttDisconnect" to trigger the reconnect, like it is done in the "FullyFeatured-ESP32.ino" example. I will try and implement this and make a pull-request when I have it ready.

For the time being you might replace the function void Basecamp::MqttHandling(void *mqttPointer) in your "basecamp.cpp" with the one I attached. I intentionally put all original code only in comments, so you can see what I changed.

basecamp_mqtt.zip

If have made several tests, with MQTT broker (mosquitto) stopped and started, LAN connection unplugged, WLAN/WiFi disabled on Fritzbox …. and I always got re-connected afterwards.

obrain17 commented 6 years ago

See Pull Request #64