peterhinch / micropython-mqtt

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

ESP32 can't connect to wifi after soft reset #133

Closed pwilga closed 4 months ago

pwilga commented 4 months ago

Working on the https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as/range.py example

It seems that on MP 1.22.1 mqtt_as can't establish Wifi connection after soft reboot:

MPY: soft reboot
Connection failed. Wi-Fi connect timed out
MicroPython v1.22.1 on 2024-01-05; Generic ESP32 module with ESP32
Type "help()" for more information.
>>>

The issue seems to be in the wifi_connect() method

    async def wifi_connect(self, quick=False):
     ....

        else:
            s.active(True)
            ...
            s.connect(self._ssid, self._wifi_pw)

            for _ in range(
                60
            ): 

                await asyncio.sleep(1)

                if s.isconnected():
                    break
                if ESP32:

                    if s.status() != network.STAT_CONNECTING:  # 1001  # t's still in the STAT_IDLE state ! (so status is 1000)
                        break

To solve the issue I have added following state checks:

                if ESP32:
                    if s.status() not in [
                         network.STAT_CONNECTING,
                         network.STAT_IDLE,
                    ]: 
                        break

I'm not sure whether it should be fixed in that way (rely on the IDLE state might be tricky?)

import machine
machine.reset()

works just fine (without any changes)

pwilga commented 4 months ago

retested on micropython 1.21.0 - works without any issues

peterhinch commented 4 months ago

Evidently we were bot working on this for the last few hours :) I made the same discovery in response to #132 and pushed an update which does something similar.

pwilga commented 4 months ago

indeed, awesome thank you !