# Badger 2040 W: Use multiple networks?

**URL:** <https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627>\
**Category:** Support\
**Created:** [November 25, 2024, 2:22am UTC](https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627 "2024-11-25T02:22:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiwi](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Kiwi](https://forums.pimoroni.com/u/Kiwi)\
**Post date:** [November 25, 2024, 2:22am UTC](https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627/1 "2024-11-25T02:22:19Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![MikeB\_50](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MikeB\_50](https://forums.pimoroni.com/u/MikeB_50)\
**Post date:** [December 30, 2024, 3:36pm UTC](https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627/2 "2024-12-30T15:36:43Z")

</div>

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:

```auto
ssids = 'ssid1', 'ssid2', 'ssid3'
pswds = 'password1', 'password2', 'password3'

```

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Kiwi](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Kiwi](https://forums.pimoroni.com/u/Kiwi)\
**Post date:** [January 4, 2025, 1:40pm UTC](https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627/3 "2025-01-04T13:40:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MikeB\_50](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MikeB\_50](https://forums.pimoroni.com/u/MikeB_50)\
**Post date:** [January 4, 2025, 1:47pm UTC](https://forums.pimoroni.com/t/badger-2040-w-use-multiple-networks/26627/4 "2025-01-04T13:47:15Z")

</div>

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.
