esprfid / esp-rfid

ESP8266 RFID (RC522, PN532, Wiegand, RDM6300) Access Control system featuring WebSocket, JSON, NTP Client, Javascript, SPIFFS
MIT License
1.35k stars 423 forks source link

MQTT Does not reconnect #149

Closed koffienl closed 6 years ago

koffienl commented 6 years ago

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:

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 commented 6 years ago

Thank you for sharing your findings.

omersiar commented 6 years ago

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

koffienl commented 6 years ago

@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 commented 6 years ago

https://github.com/omersiar/esp-rfid/releases/tag/0.8.2