256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

Reconnecting process #235

Closed manolodd closed 3 years ago

manolodd commented 3 years ago

Hi,

I've a question regarding the reconnection of mqtt client after a wifi disconnection in ESP8266 standalone. Should I code an specific logic to reconnect mqtt client after a network disconnection? What I do is:

Setup: Connect to wifi Connect MQTT and subscribe.

Loop Check whether there is still a wifi connection --> Otherwise, wait for it to be reconnected (autoReconnect is enabled) Check whether the mqtt client is connected publish --> [1] call mqtt client loop

And that works fine. Except when I turn off my wifi router that a exception is thrown and the nodemcu is rst when [1].

Before asking about this exception, I would like to know if I'm doing it well. Does the mqtt client auto-reconnect after the wifi auto-reconnect itself? Which is the state of a mqtt cliente when the network is disconnected?

If I comment all code related to MQTT and compile the sketch everything is OK. Wifi detects that I turned off my router and wait until it is turned on again to auto-reconnect and finally the network connectivity is re-established.

Thank you so much

manolodd commented 3 years ago

By the way... this is the exception

stack>>>

ctx: cont sp: 3ffffd10 end: 3fffffc0 offset: 01a0 3ffffeb0: 00000018 00000000 3ffee720 40202cb0
3ffffec0: 3ffe8733 00000000 002e2e2e 402088e9
3ffffed0: 3ffe8733 00006846 3ffe834c 4020b814
3ffffee0: 00000002 00000000 3fff1714 3fffff60
3ffffef0: 40206b74 3ffee8f0 3ffe8732 40206b80
3fffff00: 40206b74 3ffee8f0 3ffe8732 40206e4d
3fffff10: 3fffff30 3ffee8f0 3fff1714 40206f14
3fffff20: 3fffff30 3ffee8f0 3fff1714 40202825
3fffff30: 69766564 2f736563 6d6f7266 69766564
3fffff40: 6e2f6563 6d65646f 382d7563 42414143
3fffff50: 38433735 00344232 3ffee71c 40202786
3fffff60: 3fff1200 0027002f 80ffff00 3fff1700
3fffff70: 0027002f 80fee720 3ffee7ec 402026e0
3fffff80: 3fffff60 00000000 00000001 40100154
3fffff90: 3fffdad0 00000000 3ffee93c 3ffee97c
3fffffa0: 3fffdad0 00000000 3ffee93c 40207a40
3fffffb0: feefeffe feefeffe 3ffe8538 40100b81
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3584, room 16 tail 0 chksum 0xb0 csum 0xb0 v2843a5ac ~ld

manolodd commented 3 years ago

My fault!

Sorry, I was creating the TLS 1.2 connection using BearSSL in a local context and after a disconnection the reference of X509 certificates was lost.

Anyway, after lots of attempts I'm almost sure I've to code a MQTT reconnect logic.

Thank for you patience.