wlan.connect timeout
wlan.connect timeout
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?
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
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.
After a deep sleep use hard_reset=False and after a hard reset use True.
Enjoy!
(*) edited: typo in the code...
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
Enjoy!
(*) edited: typo in the code...
Last edited by ernitron on Fri Dec 02, 2016 4:35 pm, edited 1 time in total.
Re: wlan.connect timeout
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?
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
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: wlan.connect timeout
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.
Index to my micropython libraries.
Re: wlan.connect timeout
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
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
According to my experience the connect() call will timeout after about 15 seconds.
Re: wlan.connect timeout
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?
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
Sorry. I should have looked into my code. That stops waiting for a connect after about 15 seconds and assumes a fail.