peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

Subscriptions lost after network re-connect #46

Closed Lars-O-Knudsen closed 3 years ago

Lars-O-Knudsen commented 3 years ago

I have recently started using the mqtt library. I periodically experience that the wifi re-connects. I dont know why this happens. After re-connection the publishing continues without problems, but it seems all subscriptions are lost. The device becomes unresponsive to messages published by server. I have copied in a piece of log below where the reset occurs.

Is there a way to avoid this or make automatic re-subscriptions?

Log extract:

RAM free 80016 alloc 31152 {"unitId":"kumme_stue","sensor":"ms-2","pin":32,"value":163.45 } {"unitId":"kumme_stue","sensor":"ms-1","pin":35,"value":2024.78 } loop 49 RAM free 79296 alloc 31872 loop 50 RAM free 79296 alloc 31872 loop 51 I (783748) wifi:state: run -> init (0) I (783748) wifi:pm stop, total sleep time: 673555049 us / 773555826 us

I (783748) wifi:new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 I (783758) wifi: STA_DISCONNECTED, reason:8 I (784878) wifi:new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1 I (784878) wifi:state: init -> auth (b0) I (784878) wifi:state: auth -> assoc (0) I (784888) wifi:state: assoc -> run (10) I (784918) wifi:connected with Grootsvej10 IV, aid = 2, channel 6, BW20, bssid = f4:17:b8:81:f1:b2 I (784918) wifi:security: WPA2-PSK, phy: bgn, rssi: -89 I (784928) wifi:pm start, type: 1

I (784928) network: CONNECTED I (784948) wifi:AP's beacon interval = 102400 us, DTIM period = 1 I (785828) tcpip_adapter: sta ip: 192.168.1.222, mask: 255.255.255.0, gw: 192.168.1.254 I (785828) network: GOT_IP Checking WiFi integrity. Got reliable connection Connecting to broker. Connected to broker. Reconnect OK! MQTT Client connected loop 52 loop 53 RAM free 79968 alloc 31200 {"unitId":"kumme_stue","sensor":"ms-2","pin":32,"value":161.8718 } {"unitId":"kumme_stue","sensor":"ms-1","pin":35,"value":2010.1 } loop 54 RAM free 79952 alloc 31216 loop 55 {"unitId":"kumme_stue","sensor":"ms-2","pin":32,"value":152.5946 } {"unitId":"kumme_stue","sensor":"ms-1","pin":35,"value":2003.66 } RAM free 79952 alloc 31216 loop 56 loop 57

kevinkk525 commented 3 years ago

By default the client uses a clean session, which means that all subscriptions are lost on reconnects. You could use it with clean=False but that also means that the device will receive all messages that occured during downtime, which could overwhelm the device (I never tested this option). The way I handle reconnects is that my mqtt client just subscribes to all topics again every time the connection is established. (You can take a look at my implementation that inherits from mqtt_as here but it is a bit complex as it is part of a bigger project) It might be worth thinking about only doing a clean session after a certain configurable period of downtime but the easiest and safest way is to use a clean session and always resubscribe to all topics.

As for wifi outages I found out that most of my outages occured due to the router switching the frequency. So setting a fixed frequency and disabling auto-band changes (or whatever it's called in english) might prevent most reconnects.

Lars-O-Knudsen commented 3 years ago

Thank you for your advice.That cleared things up for me :-)