Page 1 of 1

wlan.connect timeout

Posted: Thu Dec 01, 2016 8:15 pm
by jamesb
I'm using an ESP12E module. After deep sleep, WLAN sometimes connects, sometimes doesn't. I'm doing this:

import network
wlan = network.WLAN(network.STA_IF)
wlan.disconnect()
wlan.active(True) # activate the interface
wlan.connect("ssid","password") # connect to an AP
if (not wlan.isconnected()):
time.sleep(1)
if (not wlan.isconnected()):
print ("Couldn't connect")

Is there a timeout parameter available to wlan.connect, or are there other reasons for connection to be intermittent?

Re: wlan.connect timeout

Posted: Thu Dec 01, 2016 10:39 pm
by ernitron
As far as I know connect returns almost immediately and it just triggers the underlying SDK to make the job.

I have developed and use my personal general purpose do_connection routine. It has been tuned and there are some reasons behind it that you probably already know or you will discover if you read enough the forum. Anyway here it is.

Code: Select all

def do_connect(ssid, pwd, hard_reset=True):
    interface = network.WLAN(network.STA_IF)

    # Stage zero: if credential are null disconnect
    if not pwd or not ssid :
        print('Disconnecting')
        interface.active(False)
        return None

    # Stage one: check for default connection
    for t in range(0, 120):
        if interface.isconnected():
            print('Oh Yes! Get connected')
            return interface
        time.sleep_ms(200)
        # Stage two: if not yet connected and after a hard reset activate and connect
        if t == 60 and hard_reset:
            interface.active(True)
            interface.connect(ssid, pwd)

    # No way we are not connected
    print('Cant connect to ', ssid)
    return None
After a deep sleep use hard_reset=False and after a hard reset use True.
Enjoy!

(*) edited: typo in the code...

Re: wlan.connect timeout

Posted: Fri Dec 02, 2016 2:07 am
by jamesb
Thanks for that!
After a bit more digging, it seems it was connecting to my router, but ifconfig revealed the IP addresses were for the router's configuration (10.0.0.xxx). I tried tethering to my phone and it got access to the internet no problems. Not sure why the router wasn't giving it access or how to get around that - maybe I need to hard code the ipconfig?

Re: wlan.connect timeout

Posted: Fri Dec 02, 2016 2:20 pm
by Roberthh
I use a scheme similar to @ernitron, just much simpler. The process of connecting is rather slow and can sometimes take 5 seconds and more, for whatever reaseon. In you first post, you seem to try only once after a second, and that would fail in my setup too.

Re: wlan.connect timeout

Posted: Sun Dec 04, 2016 9:14 am
by pythoncoder
On occasion it can be very slow. In my testing I ended up with a timeout of 15s.

Re: wlan.connect timeout

Posted: Tue Dec 06, 2016 12:03 am
by jamesb
I'm having much better success now after adding a socket.close() after each post. I'm no expert on networking, but I guess the router was becoming unhappy with sockets being opened but not closed.

Re: wlan.connect timeout

Posted: Tue Dec 29, 2020 8:23 pm
by karunt
I'm working on a ESP32 board and am new to both ESP32 and micropython. Does anyone know if there's a straightforward way to cause WLAN.connect to timeout if user provides an incorrect password?

Re: wlan.connect timeout

Posted: Tue Dec 29, 2020 8:34 pm
by Roberthh
According to my experience the connect() call will timeout after about 15 seconds.

Re: wlan.connect timeout

Posted: Tue Dec 29, 2020 8:48 pm
by karunt
So I've been observing the connect() call for more than 2 minutes now and it's still running. This is what my code looks like:

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
ntwrk = 'some network ID from sta_if.scan()
pwd = 'user_entered_password'

def connect_to_web(ntwrk, pwd):

print('Connecting to network ' + str(ntwrk))
sta_if.connect(ntwrk, pwd)
while not sta_if.isconnected():

pass
if sta_if.isconnected():
print('connection status: ', sta_if.status())
print('Connected to network ', str(ntwrk))
print('Network config:', sta_if.ifconfig())

connect_to_web(ntwrk, pwd)


What am I doing wrong over here that prevents sta_if.connect() from timing out and stop retrying after 15 seconds?

Re: wlan.connect timeout

Posted: Tue Dec 29, 2020 9:12 pm
by Roberthh
Sorry. I should have looked into my code. That stops waiting for a connect after about 15 seconds and assumes a fail.