peterhinch / micropython-mqtt

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

Starting a MQTTClient with an already working wifi connection fails with Wifi internal error. #59

Closed Molaire closed 2 years ago

Molaire commented 2 years ago

I discovered this while having the webrepl start in my boot.py, earlier than the MQTTClient start. Running on micropython 1.16-135 on ESP32.

mac --> 24:0a:c4:ec:ab:1c
connecting to network...
('192.168.0.28', '255.255.255.0', '192.168.0.1', '12.123.123.123')
WebREPL daemon started on ws://192.168.0.28:8266
Started webrepl in normal mode
E (4600131) wifi:sta is connecting, return error
WebREPL daemon started on ws://192.168.0.28:8266
Started webrepl in normal mode
Traceback (most recent call last):
  File "main.py", line 69, in <module>
  File "uasyncio/core.py", line 1, in run
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in run_until_complete
  File "main.py", line 50, in main
  File "mqtt_as.py", line 485, in wifi_connect
OSError: Wifi Internal Error
MicroPython v1.16-135-gaecb697c7-dirty on 2021-07-28; ESP32 module with ESP32
Type "help()" for more information.
>>> 

If I override the default wifi_connect, it works fine:

class MyMQTTClient(MQTTClient):

    async def wifi_connect(self):
        if not self._sta_if.isconnected():
            return await super().wifi_connect()
kevinkk525 commented 2 years ago

I remember something about an update in the ESP-IDF causing this new behaviour when one is trying to connect even though the WLAN is already connected. For now I'd suggest just not manually connecting the WIFI. Just start your webrepl and let mqtt_as do all the wifi connect. The webrepl should work either way.