miketeachman / micropython-adafruit-mqtt-esp8266

Using MQTT to Publish/Subscribe to adafruit io. MicroPython/CircuitPython implementation on ESP8266/ESP32
56 stars 17 forks source link

Recover from network issues #4

Closed rftestman1 closed 3 years ago

rftestman1 commented 5 years ago

Hi Mike, Thanks for the previous help getting this working on my project. Works great except if there are connectivity problems after the device is in the while loop subscribing and publishing.

The documentation for umqtt robust seems to imply that this condition is automatically accounted for. Can you offer any suggestions for recovering from this?

Best regards, Chris

miketeachman commented 5 years ago

Hi Chris, Can you tell me what happens in the while loop? e.g. does an exception happen? or does it just 'hang' and show no errors?

rftestman1 commented 5 years ago

Hi Mike, I think I worked out the issues but am still interested in how you would solve it.

If WIFI is good but Connection to the network/broker is lost the SW hangs up. If WIFI is lost during operation an exception is thrown. Regards, Chris

miketeachman commented 5 years ago

I often take a simple approach and perform a hard reset with machine.reset(). It's a bit of a "hammer" solution, but often in MicroPython I don't know the root cause of a dropped connection, or I don't have a clear and reliable path to recovering.

I have a solar installation running with MQTT that deep sleeps most of the time. Occasionally, an exception happens on waking when the program tries to connect to the MQTT broker. In that case, I just put the ESP8266 back into deep sleep. On wake up the device resets and resumes MQTT activities. One downside is a lost sensor data point.

Here is also an asyncio based MQTT implementation that would be worth checking out: https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as/README.md

rftestman1 commented 5 years ago

Hi Mike, that's interesting since that's exactly how I solved the issue. I'll check out the asyncio approach.

Thanks for the feedback.