tuanpmt / esp_mqtt

MQTT client library for ESP8266
http://tuanpm.net/post/esp_mqtt/
MIT License
1.15k stars 401 forks source link

Disconnect before reconnecting #96

Closed harryd100 closed 8 years ago

harryd100 commented 8 years ago

Hi, Since last change "Disconnect before reconnecting" I have an issue when my ESP roams to another AP. Although Wifi comes up and I have an IP address, MQTT is not working anymore. After a reset everything works again. After removing the changes from "Disconnect before reconnecting" it works again, also when the ESP roams to another AP.

lorenzph commented 8 years ago

Do you disconnect the previous connection (via MQTT_Disconnect) after the connection to the other API is established?

This change dropped the MQTT_Disconnect call which was happening inside MQTT_Connect if the connection was already established. The call was dropped as disconnection itself is a multi-step process:

  1. Call to espconn_disconnect
  2. Wait for mqtt_tcpclient_discon_cb and act according to the current state

If the connection is disconnected without waiting for the invocation mqtt_tcpclient_discon_cb, the disconnection callback may be invoked at any point in time. Depending on the current state this may lead to spurious reconnects.

harryd100 commented 8 years ago

Hi, I'm pretty much use the same code as in the library example, so i don't know what you mean with "Do you disconnect the previous connection (via MQTT_Disconnect) after the connection to the other API is established?". Since removing the "Disconnect before reconnecting" code-change all my devices work flawlessly for weeks now

lorenzph commented 8 years ago

I guess you're referring to https://github.com/tuanpmt/esp_mqtt/blob/master/user/user_main.c? If that's the case can you try if adding a MQTT_Disconnect(&mqttClient); before the call to MQTT_Connect(&mqttClient) in line 46 mitigates the issue?

I'll try to reproduce this locally in the next couple of days.

harryd100 commented 8 years ago

Hi, Just tested this new commit and seems to work much better. Roaming between AP's now reconnects the ESP again. Thanks for fixing.