sta_if.isconnected() unexpectedly returns false

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
manseekingknowledge
Posts: 50
Joined: Sun Oct 29, 2017 5:14 pm

sta_if.isconnected() unexpectedly returns false

Post by manseekingknowledge » Sun Aug 11, 2019 3:17 am

MicroPython v1.11-223-g71ba86b4c

In my main processing loop seen below, I check between every loop iteration to verify there is a valid Wi-Fi connection:

Code: Select all

async def main_loop():
    sta_if = network.WLAN(network.STA_IF)
    sock = None
    poller = None

    while True:
        try:
            # This continuously try to create a Wi-Fi connection
            while not sta_if.isconnected():
                # connect to wifi

            # This loop continuously tries to initialize the socket and poller
            while sta_if.isconnected() and (sock is None or poller is None):
                # initialize sock
                # initialize poller

            # This continuously polls the socket
            while sta_if.isconnected() and sock and poller:
                await uasyncio.sleep(0)

                events = poller.poll(10)
                for s, flags in events:
                    # process event

        except Exception as e:
            # handle error
I'm about 15 feet away from a Ubiquiti UAP-AC-PRO access point and I have a very good connection. I do get faint signals from some of my neighbor's Wi-Fi networks, but the 2.4GHz band in my area isn't very saturated, not like you would see at an apartment complex or anything like that. Still, calls to sta_if.isconnected() are returning false for some reason and my program keeps reconnecting to my Wi-Fi network. Roughly 10 times in the last 24 hours. Any ideas on what might be causing this? Also, what does "ets_task" stand for and what is the other information on those "ets_task" lines?

Code: Select all

#7 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 4658 seconds: connected to the manseekingknowledge_test Wi-Fi network

#8 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 6605 seconds: connected to the manseekingknowledge_test Wi-Fi network

#9 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 6910 seconds: connected to the manseekingknowledge_test Wi-Fi network

#10 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 7806 seconds: connected to the manseekingknowledge_test Wi-Fi network

#11 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 21411 seconds: connected to the manseekingknowledge_test Wi-Fi network

#12 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 35288 seconds: connected to the manseekingknowledge_test Wi-Fi network

#13 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 36769 seconds: connected to the manseekingknowledge_test Wi-Fi network

#14 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 39043 seconds: connected to the manseekingknowledge_test Wi-Fi network

#15 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 52228 seconds: connected to the manseekingknowledge_test Wi-Fi network

#16 ets_task(4020f4d0, 28, 3fff9038, 10)
connecting to the manseekingknowledge_test Wi-Fi network
running for 55125 seconds: connected to the manseekingknowledge_test Wi-Fi network
I am connected to the ESP from my PC directly via USB and I'm also powering the ESP via a reliable desktop power supply just to be sure there isn't a USB power issue.

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

Re: sta_if.isconnected() unexpectedly returns false

Post by pythoncoder » Sun Aug 11, 2019 6:22 am

I suggest you read my notes on this.
Peter Hinch

manseekingknowledge
Posts: 50
Joined: Sun Oct 29, 2017 5:14 pm

Re: sta_if.isconnected() unexpectedly returns false

Post by manseekingknowledge » Sun Aug 11, 2019 2:08 pm

pythoncoder wrote:
Sun Aug 11, 2019 6:22 am
I suggest you read my notes on this.
Excellent write up! I'm actually doing all the things you talked about already. I'm just surprised that so many outages are considered normal.

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

Reliable WiFi links

Post by pythoncoder » Mon Aug 12, 2019 5:13 am

It came as a major surprise to me when I couldn't maintain a link using official MQTT in the next room from the AP! I think we are spoiled by the superb WiFi support on computing devices which hide brief outages very successfully. On baremetal devices like ESPx and Pyboard D there is no OS to do this for you (OK, ESP32 has an OS but in this context it's minimal).

It soon became evident to me that design of the official MQTT library takes no account of this issue. Providing support for resilient connections proved surprisingly difficult. The micropython-iot project started out as an attempt to demonstrate the bare minimum amount of code required for a resilient connection, in the hope of using the lessons learned to simplify the resilient MQTT solution. The outcome demonstrated that the complexity really is required to cope with all possible failure modes (at least all the ones I could think of).

With many contributions from Kevin Köck the micropython-iot project developed into something more substantial. But in terms of the basic task of maintaining a resilient WiFi link either we're missing something fundamental or it really is a nontrivial problem. To date nobody has come up and said "Hey, mate, this is easy. Here's how to do it...".
Peter Hinch

Post Reply