peterhinch / micropython-mqtt

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

Example program unclean.py #60

Closed beetlegigg closed 2 years ago

beetlegigg commented 2 years ago

With ref to my TinyPico ESP32 board: When I control C to quit to the unclean.py example program to experiment such as changing the subscription topic, when I run the program again I always get: Connection failed. Wifi Internal Error

After a bit of juggling I found that including an 'except' for ctrl C in the try / except that runs the program to include a statement to make the wifi inactive solves this and the program will now always restart ok

My amended try except is shown below try: asyncio.run(main(client))

except KeyboardInterrupt: print('control C') client._sta_if.active(False)

finally: # Prevent LmacRxBlk:1 errors. client.close() asyncio.new_event_loop()

So I mention this as an issue in case it helps others who may be loosing hair because of continually having to unplug their board just to re-run the program after some small experiment. And also I mention it in case this is addition is deemed to be a silly thing to do. (yes I'm a beginner), and if its OK then maybe it could be incorporated into the example code.

Whilst I'm here, I will say a big thanks for this program mqtt program, and also for the ESP8266 wifi bridge program (and image) for the rpi-pico. It works just great. Sad I did not see this program before buying an Adafruit airlift board to try to do what I can now do with on of my old Wemos Esp8266 boards. PS an ESP8266 image for the mqtt-as (as you do for the bridge) would help a lot for dummies like me who have not had much luck in creating there own frozen byte code image.

peterhinch commented 2 years ago

That change is OK but it's simpler to put it in the finally clause:

finally:
    client.close()
    client._sta_if.active(False)
    asyncio.new_event_loop()

I'll have to think about this because it applies to all the demos. Perhaps I should implement a client.shutdown method which issues:

    self.close()
    self._sta_if.active(False)

then change all the demos to call this.

Did you try a soft reset (ctrl-d) prior to running the code again? I usually do this as a matter of course, which is maybe why I haven't seen this.

beetlegigg commented 2 years ago

Hi Peter,

A Ctrl D before running the program again still gives a ‘WIFI internal error’ message

I do the programming in Thonny which may make a difference I guess.

Actually I don't usually stop the program with a Ctrl C, but use the Thonny Stop button. However using the stop button, whilst not printing ‘control C’ as per my exception instructions, it does appear to act on the client._sta_if.active(false) as the program can then be restarted without getting the 'WIFI internal error' message.

I have now got rid of my Ctrl C exception and put the code into the finally: statement. Thanks for the tip.

Thanks BG

On 24 Sep 2021, at 10:09, Peter Hinch @.***> wrote:

That change is OK but it's simpler to put it in the finally clause:

finally: client.close() client._sta_if.active(False) asyncio.new_event_loop() I'll have to think about this because it applies to all the demos. Perhaps I should implement a client.shutdown method which issues:

self.close()
self._sta_if.active(False)

then change all the demos to call this.

Did you try a soft reset (ctrl-d) prior to running the code again? I usually do this as a matter of course, which is maybe why I haven't seen this.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/peterhinch/micropython-mqtt/issues/60#issuecomment-926471620, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIHTXILSWJZOB54TW2SG4ZLUDQ553ANCNFSM5EUYXVMA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

peterhinch commented 2 years ago

I have pushed an update. This renames the existing .close method to ._close. The public .close method calls ._close before setting the station interface inactive. This has the merit of requiring no change to docs, test scripts or user code.

Please could you run my original test script in your usual way, verifying that you can re-run it without lockups, and report the outcome.

beetlegigg commented 2 years ago

Hi Peter I have tested the clean.py and unclean.py, the only 2 examples I have played with before, and they both can be stopped and restarted without the dreaded WiFi Internal Error message So issue is solved, and thanks for your speedy fix, much appreciated. Rgs

On 24 Sep 2021, at 17:41, Peter Hinch @.***> wrote:

I have pushed an update. This renames the existing .close method to ._close. The public .close method calls ._close before setting the station interface inactive. This has the merit of requiring no change to docs, test scripts or user code.

Please could you run my original test script in your usual way, verifying that you can re-run it without lockups, and report the outcome.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/peterhinch/micropython-mqtt/issues/60#issuecomment-926771787, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIHTXIIEABCAPIM72YETEALUDSS2ZANCNFSM5EUYXVMA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

peterhinch commented 2 years ago

Thank you for testing.