Multiple networks in boot.py
Multiple networks in boot.py
Anybody has an example for how to have multiple networks in the boot.py so it searches which ssid gives it's password and prints which is connected to. I can do I for one network, but cant add a second to be read automatic.
Re: Multiple networks in boot.py
@blckpstv This could be useful for you.... I had to solve the same problem... change the place of work with other APs
Code: Select all
##...module aps_trusted.py
##APS = {
## "ssid1":"psw1",
## "ssid2":"psw2",
## "ssidN":"pswN"
##}
##...module boot.py
## Create connection to trusted AP
from aps_trusted import APS
from network import WLAN, STA_IF
from esp import osdebug
from time import sleep_ms
def try_connection():
t = 12
while not wlan.isconnected() and t > 0:
print('.', end='')
sleep_ms(500)
t = t - 1
return wlan.isconnected();
osdebug(None)
wlan = WLAN(STA_IF)
wlan.active(True)
print('connecting to last AP', end='')
print(try_connection())
if not wlan.isconnected():
## find all APs
ap_list = wlan.scan()
## sort APs by signal strength
ap_list.sort(key=lambda ap: ap[3], reverse=True)
## filter only trusted APs
ap_list = list(filter(lambda ap: ap[0].decode('UTF-8') in
APS.keys(), ap_list))
for ap in ap_list:
essid = ap[0].decode('UTF-8')
if not wlan.isconnected():
print('connecting to new AP', essid, end='')
wlan.connect(essid, APS[essid])
print(try_connection())
#import webrepl
#webrepl.start()
import gc
gc.collect()
#import blink
Re: Multiple networks in boot.py
Cool! I'm trying it next week at school! Thanks!profra wrote:@blckpstv This could be useful for you.... I had to solve the same problem... change the place of work with other APs
Code: Select all
##...module aps_trusted.py ##APS = { ## "ssid1":"psw1", ## "ssid2":"psw2", ## "ssidN":"pswN" ##} ##...module boot.py ## Create connection to trusted AP from aps_trusted import APS from network import WLAN, STA_IF from esp import osdebug from time import sleep_ms def try_connection(): t = 12 while not wlan.isconnected() and t > 0: print('.', end='') sleep_ms(500) t = t - 1 return wlan.isconnected(); osdebug(None) wlan = WLAN(STA_IF) wlan.active(True) print('connecting to last AP', end='') print(try_connection()) if not wlan.isconnected(): ## find all APs ap_list = wlan.scan() ## sort APs by signal strength ap_list.sort(key=lambda ap: ap[3], reverse=True) ## filter only trusted APs ap_list = list(filter(lambda ap: ap[0].decode('UTF-8') in APS.keys(), ap_list)) for ap in ap_list: essid = ap[0].decode('UTF-8') if not wlan.isconnected(): print('connecting to new AP', essid, end='') wlan.connect(essid, APS[essid]) print(try_connection()) #import webrepl #webrepl.start() import gc gc.collect() #import blink
Re: Multiple networks in boot.py
Thanks very much for this. I am using it with an ESP32. I commented out the 'from esp import osdebug' and 'osdebug(None)' lines. It seems to wo work very well.
Re: Multiple networks in boot.py
Here's v2!
This supports scanning and connecting to hidden networks via MAC identification. Although the stock MicroPython software does NOT return hidden networks in scan(). I do not recommend blindly trying to connect to hidden networks, as connect() writes to the limited write flash memory. I recommend running in main.py over boot, as it takes time to execute. If anyone wants my custom v1.11 firmware that adds hidden SSID scanning, let me know. The file attach size is less than 380k here lol...
Edit: This code may have issues on ESP32 see https://github.com/micropython/micropython/issues/5326
This supports scanning and connecting to hidden networks via MAC identification. Although the stock MicroPython software does NOT return hidden networks in scan(). I do not recommend blindly trying to connect to hidden networks, as connect() writes to the limited write flash memory. I recommend running in main.py over boot, as it takes time to execute. If anyone wants my custom v1.11 firmware that adds hidden SSID scanning, let me know. The file attach size is less than 380k here lol...
Edit: This code may have issues on ESP32 see https://github.com/micropython/micropython/issues/5326
Code: Select all
from network import WLAN, STA_IF, AP_IF, STAT_IDLE, STAT_CONNECTING, STAT_WRONG_PASSWORD, STAT_NO_AP_FOUND, STAT_CONNECT_FAIL, STAT_GOT_IP
from time import sleep
from ubinascii import hexlify
USE_AP = True # Turn On Internal AP After Failed WiFi Station Connection
NETWORKS = [ # SSID, PWD, [MAC] (Optional)
['ssid1', 'pwd1', ['25288b12423d4']],
['ssid2', 'pwd2'],
['ssidN', 'pwdN']
]
station = WLAN(STA_IF)
local_ap = WLAN(AP_IF)
def connectWiFi():
if not station.active():
station.active(True)
if waitForConnection():
return True
aps = station.scan()
aps.sort(key=lambda ap:ap[3], reverse=True)
for ap in aps:
for net in NETWORKS:
found = False
if ap[0].decode('UTF-8') == net[0]:
found = True
elif len(net) == 3:
for mac in net[2]:
if hexlify(ap[1]).decode('UTF-8') == mac:
found = True
break
if found:
station.connect(net[0], net[1])
if waitForConnection():
if USE_AP and local_ap.active():
local_ap.active(False)
return True
if USE_AP and not local_ap.active():
local_ap.active(True)
return False
def waitForConnection():
while station.status() == STAT_CONNECTING:
sleep(0.25)
return station.isconnected()
connectWiFi()