mqtt_as: program structure questions

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
grafalex
Posts: 6
Joined: Sun Mar 04, 2018 4:53 pm

mqtt_as: program structure questions

Post by grafalex » Sun Mar 04, 2018 5:15 pm

Hi All,

I am building an application based on ESP8266 board and mqtt_as framework. I learned examples bundled with https://github.com/peterhinch/micropython-mqtt library, but I still have a few questions.

1) Most (if not all) examples run main functions like this:

[code]
try:
loop.run_until_complete(main(client))
finally:
client.close() # Prevent LmacRxBlk:1 errors
[/code]

My device should work 24/7 without a supervision. I do not want it stop at some point and switch to REPL - it should reliably reboot/reconnect after any kinds of outage.

Is there any specific reason for not using loop.run_forever() ?

2) Main function usually looks like this:

[code]
async def main(self):
try:
wifi_connect()
except:
return

while True:
# do something periodically
await asyncio.sleep(60)
[/code]

Does this mean that if there is no wifi connection at startup (e.g. restoring after global power outage for both device and wifi router) device will never connect?

3) Looking at mqtt_as sources I see that ssid and wifi_pw are passed to a connect function only in case of ESP32. Surprisingly it somehow works for ESP8266, but I can't understand the mechanism behind. There are comments like 'ESP8266 stores wifi credentials', but how does it works in case of a fresh module?

User avatar
pythoncoder
Posts: 3247
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mqtt_as: program structure questions

Post by pythoncoder » Tue Mar 06, 2018 7:49 am

There is no reason why you shouldn't use run_forever. The run_until_complete and try-except block were both for debugging. run_until_complete allowed me to quit at will. The exception is important because if you interrupt the program with ctrl-C and data is still being thrown at it, the socket will still be open and the ESP8266 will produce those errors until you reset it. So I recommend keeping that structure at least until it's fully debugged.

Re the other questions the ESP32 and the ESP8266 behave very differently at the hardware/vendor code level. The ESP32 requires WiFi login credentials every time it's powered up, as you might expect. The ESP8266 stores them in Flash. So say you get a new, out of the box, ESP8266, install MicroPython on it, and log into your WiFi network. If you power cycle it, the device will automatically reconnect to your LAN. It is considered a bad idea to explicitly connect every time (as you do on an ESP32) because repeatedly updating the Flash eventually degrades it.

So on the ESP8266 my code only attempts an explicit connection if the default one fails (the device could have been moved to another LAN).
Peter Hinch

grafalex
Posts: 6
Joined: Sun Mar 04, 2018 4:53 pm

Re: mqtt_as: program structure questions

Post by grafalex » Wed Mar 07, 2018 7:55 pm

Hi Peter,

Thank you very much for your answers. This makes things more clear.

I did not know ESP stores WiFi password. This might be tricky to set password for every new board (I mean non-development boards, e.g. sonoff). Not a big deal, though

User avatar
pythoncoder
Posts: 3247
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mqtt_as: program structure questions

Post by pythoncoder » Fri Mar 09, 2018 8:52 am

Before investing too much time in Sonoff you might like to read this.
Peter Hinch

Post Reply