Fastest WLAN connect with AP fallback

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Fastest WLAN connect with AP fallback

Post by devnull » Thu Feb 23, 2017 12:41 pm

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())


Post Reply