Page 1 of 1

Fastest WLAN connect with AP fallback

Posted: Thu Feb 23, 2017 12:41 pm
by devnull
Does anyone have a faster, more concise, low RAM WLAN connection routine that falls back to AP mode if it can't connect to the wlan ?

Code: Select all

def connect(timeout=10,ssid=None,passkey=None):
	from network import WLAN,STA_IF, AP_IF
	
	sta = WLAN(STA_IF)
	if not sta.isconnected():
		sta.active(True)
        sta.connect(ssid,passkey)
        from time import sleep_ms
        for count in range(timeout*10):
        	if sta.isconnected(): break
        	sleep_ms(100)
        if not sta.isconnected():
        	sta = WLAN(AP_IF)
        	sta.active(True)	
    
	print('Connect msec:',count*100,'ifconfig:',sta.ifconfig())
	return sta

wl = connect(timeout=5,ssid='wlan@mywan',passkey='mypass')
print(wl.ifconfig())