peterhinch / micropython-mqtt

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

wifi disconnect on ctrl-c via webrepl on Esp32 #99

Closed ui3o-zone closed 1 year ago

ui3o-zone commented 1 year ago

Hi,

my first issue, and not even mine. So,thx for guiding me through. And thx for the great work around micropython asyncio!

When applying ctrl-c via webrepl, wifi connection seems to interrupt, thus loosing webrepl connection. A friend of mine, whom I asked for help (thought the problem was on my side), found out about that and created a modified mqtt_as.py, which works beautifully for 2 weeks now. Since he's not deep into the code, it's not clear if other functionality would be impacted by those alterations. Pls advise, if I shall send the modified code for inspection.

Secondly, it was adviced to check connectivity against the IP of the mqtt-Broker rather an outside address like 8.8.8.8, since iot devices are frequently prohibited to connect to the internet. Sounds comprehensible for me.

Thx for considering those two topics for future versions of mqtt_as.py

ebolisa commented 1 year ago

In my opinion, iot devices are not connected to keyboards unless they are undergoing a test phase. In that case, I’d want the code to halt, including the wifi connection. That’s what ctr-c was meant for. Otherwise, makes no sense its use, at least to me.

And, most of the iot devices use external mqtt servers, otherwise, make no sense not being able to ctr them when away. So, if they do connect to internet, what’s the difference if they ping a dns vs. an external mqtt server address? (Unless you want to know if the server is up) If in your case internet is not used, change the dns address to your local mqtt. I do that all the time when testing.

ui3o-zone commented 1 year ago

So, as a newbie I May Need some know-how sharing on webrepl. What's it's main usage? I thought it's mainly about programming the device OTA (well, at least that's the use case I'm longing for). Therefore it'd be helpful, if the device keeps the wifi connection during ctrl-c in order to upload new files and soft-reboot via ctrl-d.

As to the connectivity check: checking against the user's mqtt-Broker's IP should work either case. for those using a web based broker as well as for those limiting the divices from the internet and providing a local broker.

peterhinch commented 1 year ago

The purpose of wan_ok is to check for internet connectivity: it therefore makes no sense to point it at a local broker. If an IOT device doesn't have internet access, the application should not use wan_ok. It is not used internally by mqtt_as, it is there as a user convenience primarily for systems with a remote broker.

Use with webrepl or other network-based code is unsupported. The module is intended for autonomous hardware and its management of the WiFi interface is likely to conflict with other code. For example a key purpose of mqtt_as is to recover from WiFi outages; webrepl will fail under these conditions.