eky3zhz8bht4wux
Posts: 2
Joined: Tue Jun 06, 2023 2:50 pm

WiFi switching

Tue Jun 06, 2023 3:06 pm

I'm having issues WiFi switching and I didn't find solution so far. What is the right way of doing this?

example code:

Code: Select all

from network import WLAN, STA_IF
from time import sleep

WLANS = {'ssid1': 'password1',  'ssid2': 'password2'}

wlan = WLAN(STA_IF)
wlan.active(True)

def connect(ssid, password):
    wlan.connect(ssid, password)
    while wlan.isconnected() == False:
        #print('Waiting for connection...')
        sleep(1)
    print("connected")

for ssid in WLANS.keys():
    print(ssid)
    connect(ssid, WLANS[ssid])
    sleep(1)
    
but works only for the first connection.

hippy
Posts: 14339
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: WiFi switching

Tue Jun 06, 2023 5:40 pm

Not done it myself but I would imagine you have to at least issue a 'disconnect' before issuing a further 'connect'.

eky3zhz8bht4wux
Posts: 2
Joined: Tue Jun 06, 2023 2:50 pm

Re: WiFi switching

Tue Jun 06, 2023 8:44 pm

Yes but my real life scenario is to start in one WiFi range, move elsewhere and connect to the different WiFi. So far I found best solution that when detect that WiFi disconnects so machine.reset() but that is just a workaround

Return to “General”