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: 61
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: 5956
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
Index to my micropython libraries.

manseekingknowledge
Posts: 61
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: 5956
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
Index to my micropython libraries.

jf_vt
Posts: 9
Joined: Thu Nov 25, 2021 5:15 pm

Re: sta_if.isconnected() unexpectedly returns false

Post by jf_vt » Tue Mar 15, 2022 3:11 am

Hi
I join this thread a few years later because I dont understant how to use this method
More specifically, I cannot find a configuration where sta_if.isconnected return TRUE even when the esp8266 is obviously connected to the wlan and perform as expected otherwise.

my setup: dedicated WLAN ( no internet connection)
esp8266 attached to sensors and broadcast their values through mqtt.

code is plain vanilla mostly copied from the internet. ( and it works..)

while trying to build some resilience, the sensors -50+ on the network- stop broadcasting on average once every other week all at the same time probably because of some micro power outage, . I would like to detect that and reconnect automatically.

While trying to debug/resolve this issue I did some test and was able to have the esp8266 to check sta_if.isconnected and broadcast its value =FALSE on MQTT .
I NEVER received a TRUE and this is the reason of this post.

I also checked that I can ping the esp8266 and I see it on the dhcp table. So for all practical purposes the esp8266 are connected.

SO is this method really working at all or do I misunderstand its use?
Is it because the WLAN is not connected to the internet?
I could not find anything on the micropython documentation on these issues

By the way I carefully reads the notes from pythoncoder linked in this thread ( great work by the way ).
It provide a different approach to solve the problem. I am considering to adapt it using ping on esp8266 to check the network connection and reconnect when needed .

I would still love to understand how to get sta_if.isconected =TRUE
Thanks

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

Re: sta_if.isconnected() unexpectedly returns false

Post by pythoncoder » Tue Mar 15, 2022 8:56 am

I'm foxed by this. My standard script for initiating a connection on an ESP8266 uses this and has never given me any trouble:

Code: Select all

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    ap = network.WLAN(network.AP_IF) # create access-point interface
    ap.active(False)         # deactivate the interface
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('wifi_name', 'password')
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())
    a = sta_if.config('mac')
    print('MAC {:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(a[0],a[1],a[2],a[3],a[4]))
Peter Hinch
Index to my micropython libraries.

jf_vt
Posts: 9
Joined: Thu Nov 25, 2021 5:15 pm

Re: sta_if.isconnected() unexpectedly returns false

Post by jf_vt » Fri Mar 18, 2022 6:57 pm

Hi
@ pythoncoder
Thanks for this
I have the same basic function ( that I might have taken from you a few years back :-)

With respect to my initial question
I tried something different
I tested my esp8266 connection on a a "high end" wifi router that I normally use for other purposes.
I had bought a cheap brandeless router at a supermarket sale to setup my sensor network...

when I connect an esp8266 on the high end router is.connected return TRUE as it should ( and always return false on the cheaper router that otherwise works perfectly for its purpose. Same board same software in both tests).

There is definitively an issue regarding how the esp8266 communicates with the router but it appears it is more on the router side.
I am going to upgrade my sensor's network router and the problem will be solved.

fangis
Posts: 12
Joined: Mon Jul 24, 2017 11:03 pm
Contact:

Re: sta_if.isconnected() unexpectedly returns false

Post by fangis » Sat Jun 10, 2023 1:26 am

hello friends

In my case too I was unable to use isconnected() to verify the connection state in a script,
the result would be the same no matter if connected or disconnected.
Why it does happen , I have no idea.
when testing this same function in the repl, it seemed to work.
The workaround was to do an actual connection to some site and verify if it got some data or not.

Post Reply