Can I use multiple networks with my Badger? (Of course not at the same time, but have it connect to my home WiFi or my work WiFi instead of having to swap SSIDs/passwords in the file whenever I leave home)
In the WIFI_CONFIG.py file, it has strings for each of those (the SSID and password) but could I put a list instead? If not, could someone help me code something to check for different networks in a list? (I might be able to code that myself, but I don’t know about much of the badgerOS networking MicroPython/libraries)
I don’t have a Badger, but I use the following code fragment on a Pico Plus 2 W to connect to multiple wireless networks and set the time using NTP for the Explorer Kit clock program provided by @Tonygo2, which I power from a USB charger.
It needs tuples ssids and pswds to be added to secrets.py, with entries for the various networks that you wish to connect to:
The code scans the network for available SSIDs and compares them with the ones in the ssids tuple. If a match is found, that ssid and the corresponding password are used to connect to the network. For the comparison, I encode the ssid to convert it to a bytes object, because the scan result is a bytes object.
from time import sleep
import network
import ntptime
try:
from secrets import ssids, pswds
wifi_available = True
except ImportError:
print("Create secrets.py with tuples of ssids and passwords to get time from NTP")
wifi_available = False
# Connect to wifi if available and synchronise the pico RTC time from NTP
def sync_ntp():
if not wifi_available:
return False
# Initialise network interface
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs
# Scan for available network SSIDs
networks = wlan.scan()
network_info = ['x'] # Dummy value to initialise list
ssid = ''
for i in range(len(ssids)):
for network_info in networks:
if ssids[i].encode() == network_info[0]:
ssid = ssids[i]
pswd = pswds[i]
break
if ssid:
break
print(i, ssid, network_info[0])
if not ssid:
return False
# Start connection
wlan.connect(ssid, pswd)
# Wait for connection success or failure
print('Waiting for connection',end='')
max_wait = 60
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('.',end='')
sleep(0.5)
print()
ntp_set = False
if wlan.status() == 3:
print("Connected")
try:
ntptime.settime()
ntp_set = True
print("Setting pico time from NTP")
except OSError:
print("Failed to set pico time from NTP")
wlan.disconnect()
else:
print("Failed to connect")
wlan.active(False)
return ntp_set
The badger uses a file called WIFI_CONFIG.py in badgerOS, but I’m sure that I could use the code you provided with a little tinkering (such as you don’t just “import network” you do something similar to “from badger2040 import network”)
Yes, you could add the ssids and pswds lines to WIFI_CONFIG.py instead of secrets.py.
It looks like badgerOS has its own version of the network library, but I imagine it would have similar functions.