Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT Does not reconnect #149

Closed
koffienl opened this issue Aug 30, 2018 · 4 comments
Closed

MQTT Does not reconnect #149

koffienl opened this issue Aug 30, 2018 · 4 comments
Labels

Comments

@koffienl
Copy link

koffienl commented Aug 30, 2018

Tested with 0.8.0 , but looking at the 0.8.1 code I think it will be the same.
When the mqtt server is down or for whatever reason the mqtt connection is lost, there will be a line in the eventlog: Disconnected from MQTT server | TCP_DISCONNECTED but reconnect never works.

I added some debug writes to the eventlog to see where it fails, but I can't find the issue. After discovering the disconnect a reconnect is scheduled for after 60 seconds. The attempt is made, but the connect is never arrived at the mqtt server, resulting in a loop to try to reconnect.
Looking at some issues of the used AsyncMqttClient lib, I'm feeling the issue might be in the lib?

To summarize:

  • ESP boots up and connect to mosquitto server
  • Kill mosquitto server
  • ESP confirms it is disconnect and schedules a reconnect
  • Power up momsquitto again
  • All other clients are reconnecting
  • After 60 seconds no incoming connection from the ESP on the mosquitto server
  • Falls in a loop
WARN | mqtt | Disconnected from MQTT server | TCP_DISCONNECTED | 2018-08-30-19:12:28
WARN | mqtt | call mqttclient.connect | ROO_DEBUG | 2018-08-30-19:12:15
WARN | mqtt | set schedule 60s to reconnect | ROO_DEBUG | 2018-08-30-19:11:15
WARN | mqtt | Disconnected from MQTT server | TCP_DISCONNECTED | 2018-08-30-19:11:15
WARN | mqtt | call mqttclient.connect | ROO_DEBUG | 2018-08-30-19:11:02
WARN | mqtt | Disconnected from MQTT server | TCP_DISCONNECTED | 2018-08-30-19:10:02
WARN | mqtt | set schedule 60s to reconnect | ROO_DEBUG | 2018-08-30-19:10:02
WARN | mqtt | call mqttclient.connect | ROO_DEBUG | 2018-08-30-19:09:49
WARN | mqtt | set schedule 60s to reconnect | ROO_DEBUG | 2018-08-30-19:08:49
WARN | mqtt | Disconnected from MQTT server | TCP_DISCONNECTED | 2018-08-30-19:08:49

UPDATE : After a tonload of testing, debugging and pulling my hair out, I managed to have a reconnect!
In the onMqttDisconnect function I added setting (again) the server to connect to, right before calling the ticker:
mqttClient.setServer("192.168.2.6", 1883);

It seems that somehow magically the library forgets it's server connection (but remembers the user / pass).
Easiest solution would be the re-read the json and set the server from the config, or on booting up push the server settings in global variables and use it in the onMqttDisconnect.

For now I have a quick dirty ptach by simply hardcoding the broker address.

@omersiar
Copy link
Collaborator

omersiar commented Sep 1, 2018

Thank you for sharing your findings.

@omersiar omersiar added the bug label Sep 2, 2018
omersiar added a commit that referenced this issue Sep 3, 2018
@omersiar
Copy link
Collaborator

omersiar commented Sep 3, 2018

@koffienl Please see this commit e15b7a8, is this ok?

@koffienl
Copy link
Author

koffienl commented Sep 3, 2018

@omersiar I adopted your changes for setting the char *mhs and int global and setting the values on the startup. Replaced my hardcoded setserver with mqttClient.setServer(mhs, mport); just before calling mqttReconnectTimer.once(60, connectToMqtt);
Works as expected. It does a reconnect properly. Thanks :)

@omersiar
Copy link
Collaborator

omersiar commented Sep 5, 2018

@omersiar omersiar closed this as completed Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants