multiple SSIDs

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

multiple SSIDs

Post by atux_null » Tue Aug 07, 2018 1:19 pm

I would like to ask if its possible to have multiple wifi SSIDs and passwdord setup on the system (SSID_1, SSID_2...SSID_N). In case one fails then go to the next one. Here is my current setup of main.py

Code: Select all

import gc
import machine
import webrepl
import network
import time
webrepl.start()
gc.collect()
 
def connect():
     # Create Station interface
     wlan = network.WLAN(network.STA_IF) 
     if not wlan.isconnected():
          print('Connecting...')
 
          # Enable the interface
          wlan.active(True)
          # Connect
          wlan.ifconfig(('192.168.1.89','255.255.255.0','192.168.1.1','8.8.8.8'))
          wlan.connect('NETGEARHome25', 'whateverpasswd')
 
          # Wait till connection is established
          while not wlan.isconnected():
               time.sleep_ms(300)
 
          print('It is now connected.')
 
     else:
          print('Already Connected.')   
 
     print('Network Settings:', wlan.ifconfig())

ap = network.WLAN(network.AP_IF)
ap.active(False)
connect()
webrepl.start()

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

Re: multiple SSIDs

Post by SpotlightKid » Tue Aug 07, 2018 2:24 pm

Just time out when waiting too long for a connection to a wifi network to be established and then try the next one. You can restructure your code a bit to make it easier to add new networks:

Code: Select all

networks = (
    ('NETGEARHome25', 'whateverpasswd', '192.168.1.89', '255.255.255.0','192.168.1.1', '8.8.8.8'),
    ('myotherwifinet', 'somepass', '192.168.2.2', '255.255.255.0', '192.168.2.1', '8.8.8.8'),
    ...
)

def connect(ssid, password, ipaddress, netmask, gateway, dns):
    # Create Station interface
    wlan = network.WLAN(network.STA_IF) 
    if not wlan.isconnected():
        print("Attempting connection to '%s'..." % ssid)
 
        # Enable the interface
        wlan.active(True)
        # Connect
        wlan.connect(ssid, password)

        # Wait till connection is established
        for i in range(10):
            if wlan.isconnected():
                wlan.ifconfig(ipaddress, netmask, gateway, dns)
                break
            time.sleep_ms(500)
        else:
            return None
 
        print("Now connected to '%s'." % ssid)
    else:
        print('Already Connected.')

    return wlan
    

for netinfo in networks:
    wlan = connect(*netinfo)
    if wlan:
        break
else:
    print("No network found.")

atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

Re: multiple SSIDs

Post by atux_null » Wed Aug 08, 2018 1:04 pm

thanks a lot for the reply. i tested the code and now i have the following code as main.py

Code: Select all

import gc
import machine
import webrepl
import network
import time
webrepl.start()
gc.collect()


networks = (
    ('NETGEARHome25', 'whateverpasswd', '192.168.1.89', '255.255.255.0','192.168.1.1', '8.8.8.8'),
    ('myotherwifinet', 'somepass', '192.168.2.89', '255.255.255.0', '192.168.2.1', '8.8.8.8'),
    ('SSID3', 'whateverpasswd', '192.168.4.89', '255.255.255.0','192.168.4.1', '8.8.8.8'),
    ('SSID4', 'somepass', '192.168.7.89', '255.255.255.0', '192.168.7.1', '8.8.8.8'),
)


 
def connect(ssid, password, ipaddress, netmask, gateway, dns):
    # Create Station interface
    wlan = network.WLAN(network.STA_IF) 
    if not wlan.isconnected():
        print("Attempting connection to '%s'..." % ssid)
 
        # Enable the interface
        wlan.active(True)
        # Connect
        wlan.connect(ssid, password)

        # Wait till connection is established
        for i in range(10):
            if wlan.isconnected():
                wlan.ifconfig(ipaddress, netmask, gateway, dns)
                break
            time.sleep_ms(500)
        else:
            return None
 
        print("Now connected to '%s'." % ssid)
    else:
        print('Already Connected.')

    return wlan
    

for netinfo in networks:
    wlan = connect(*netinfo)
    if wlan:
        break
else:
    print("No network found.")

connect()
webrepl.start()

#Disable Access point
ap = network.WLAN(network.AP_IF)
ap.active(False)
Regarding connectivity it seems to work fine. the problems are:
  • cannot disable access point
  • cannot set static ip per connection

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

Re: multiple SSIDs

Post by SpotlightKid » Wed Aug 08, 2018 1:13 pm

Why are you calling connect again after the for-loop and without arguments? This can't work.

Also, just move the code to disable the AP before the for-loop with the connect calls.

And what do you mean you can't set a static IP per connection? That's what the last four entries in the tuples in networks are for. But there are parentheses missing in the call to ifconfig. It takes one tuple as an argument, not several positional parameters.

atux_null
Posts: 20
Joined: Tue Jul 10, 2018 2:16 pm

Re: multiple SSIDs

Post by atux_null » Wed Aug 08, 2018 2:52 pm

Lost it somewhere. It fails to connect and i am getting error:

Code: Select all

Started webrepl in normal mode
Attempting connection to 'NETGEARHome25'...
Traceback (most recent call last):
  File "main.py", line 46, in <module>
  File "main.py", line 32, in connect
TypeError: function expected at most 2 arguments, got 5
here is the code after the changes i tried to do.

Code: Select all

import gc
import machine
import webrepl
import network
import time
webrepl.start()
gc.collect()


networks = (
    ('NETGEARHome25', 'whateverpasswd', '192.168.1.89', '255.255.255.0','192.168.1.1', '8.8.8.8'),
    ('myotherwifinet', 'somepass', '192.168.2.89', '255.255.255.0', '192.168.2.1', '8.8.8.8'),
    ('SSID3', 'whateverpasswd', '192.168.4.89', '255.255.255.0','192.168.4.1', '8.8.8.8'),
    ('SSID4', 'somepass', '192.168.7.89', '255.255.255.0', '192.168.7.1', '8.8.8.8'),
)



 
def connect(ssid, password, ipaddress, netmask, gateway, dns):
    # Create Station interface
    wlan = network.WLAN(network.STA_IF) 
    if not wlan.isconnected():
        print("Attempting connection to '%s'..." % ssid)
 
        # Enable the interface
        wlan.active(True)
        # Connect
        wlan.connect(ssid, password)

        # Wait till connection is established
        for i in range(10):
            if wlan.isconnected():
                wlan.ifconfig(ipaddress, netmask, gateway, dns)
                break
            time.sleep_ms(500)
        else:
            return None
 
        print("Now connected to '%s'." % ssid)
    else:
        print('Already Connected.')

    return wlan
    

for netinfo in networks:
    wlan = connect(*netinfo)
	
    if wlan:
        break
else:
    print("No network found.")

	#Disable Access point
    ap = network.WLAN(network.AP_IF)
    ap.active(False)

webrepl.start()


thanks in advance for your help. Newbie over here :shock:

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

Re: multiple SSIDs

Post by SpotlightKid » Wed Aug 08, 2018 4:46 pm

Please make an effort to find the problem yourself. The exception error message clearly tells you where in your code the error source is located* and what the problem is, which I also already told you in my previous message.

* Though there is a two line offset. I supect the code you posted has two extra empty lines somewhere, compared to the actual code, which produced the error message.

Post Reply