wlan.connect timeout

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jamesb
Posts: 13
Joined: Tue Nov 29, 2016 3:31 am

wlan.connect timeout

Post by jamesb » Thu Dec 01, 2016 8:15 pm

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?

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: wlan.connect timeout

Post by ernitron » Thu Dec 01, 2016 10:39 pm

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...
Last edited by ernitron on Fri Dec 02, 2016 4:35 pm, edited 1 time in total.

jamesb
Posts: 13
Joined: Tue Nov 29, 2016 3:31 am

Re: wlan.connect timeout

Post by jamesb » Fri Dec 02, 2016 2:07 am

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?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: wlan.connect timeout

Post by Roberthh » Fri Dec 02, 2016 2:20 pm

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.

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

Re: wlan.connect timeout

Post by pythoncoder » Sun Dec 04, 2016 9:14 am

On occasion it can be very slow. In my testing I ended up with a timeout of 15s.
Peter Hinch
Index to my micropython libraries.

jamesb
Posts: 13
Joined: Tue Nov 29, 2016 3:31 am

Re: wlan.connect timeout

Post by jamesb » Tue Dec 06, 2016 12:03 am

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.

karunt
Posts: 4
Joined: Tue Dec 29, 2020 7:24 pm

Re: wlan.connect timeout

Post by karunt » Tue Dec 29, 2020 8:23 pm

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?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: wlan.connect timeout

Post by Roberthh » Tue Dec 29, 2020 8:34 pm

According to my experience the connect() call will timeout after about 15 seconds.

karunt
Posts: 4
Joined: Tue Dec 29, 2020 7:24 pm

Re: wlan.connect timeout

Post by karunt » Tue Dec 29, 2020 8:48 pm

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?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: wlan.connect timeout

Post by Roberthh » Tue Dec 29, 2020 9:12 pm

Sorry. I should have looked into my code. That stops waiting for a connect after about 15 seconds and assumes a fail.

Post Reply