I noticed that my ESP32 board running a pre-release of MicroPython 1.12 (27th of July) stops sending messages via MQTT after a WiFi disconnect. I am able to reproduce the problem by manually disconnect (from the routers Web interface). The following code shows the behavior:
Code: Select all
import network
wl = network.WLAN(network.STA_IF)
wl.active(1)
wl.connect(...)
from umqtt.robust import MQTTClient
mqttc = MQTTClient(...)
mqttc.DEBUG = True
mqttc.connect()
<disconnnect>
mqttc.publish("/test", "1")
Code: Select all
I (2936506) wifi: STA_DISCONNECTED, reason:205
I (2936626) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2936626) wifi:state: init -> auth (b0)
I (2936636) wifi:state: auth -> init (1100)
I (2936636) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2936636) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(118,)
I (2938696) wifi: STA_DISCONNECTED, reason:205
I (2938816) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2938816) wifi:state: init -> auth (b0)
I (2938826) wifi:state: auth -> init (1100)
I (2938826) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2938826) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(118,)
I (2940886) wifi: STA_DISCONNECTED, reason:205
I (2941006) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2941016) wifi:state: init -> auth (b0)
I (2941016) wifi:state: auth -> init (1100)
I (2941016) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2941016) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(23,)
I (2943076) wifi: STA_DISCONNECTED, reason:205
I (2943206) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2943206) wifi:state: init -> auth (b0)
I (2943206) wifi:state: auth -> init (1100)
I (2943206) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2943216) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(23,)
What I found is that triggering the garbage collector does fix the problem, so adding something like this in the reconnect() exception handler can work around the problem:
Code: Select all
if e.args[0] == 23:
gc.collect()
--
Stefan