boot.py WiFi script



  • We want to share with you this boot.py script that we've been using internally in our development boards, in hope that you find it useful.

    A short list of what it does:

    • Configures the serial port at 115200 bauds (needed for Pymakr to connect over serial).
    • Tries to connect to a list of known access points.
    • If it doesn't find a known network, it fallbacks to access point mode, so you can connect straight to your board as it comes from the factory.

    Please modify the first line to match your known networks.

    Here is the script:

    known_nets = [('home_ssid', 'home_password'), ('office_ssid', 'office_password')] # change this line to match your WiFi settings
    
    import machine
    import os
    
    uart = machine.UART(0, 115200) # disable these two lines if you don't want serial access
    os.dupterm(uart)
    
    if machine.reset_cause() != machine.SOFT_RESET: # needed to avoid losing connection after a soft reboot
        from network import WLAN
        wl = WLAN()
    
        # save the default ssid and auth
        original_ssid = wl.ssid()
        original_auth = wl.auth()
    
        wl.mode(WLAN.STA)
    
        available_nets = wl.scan()
        nets = frozenset([e.ssid for e in available_nets])
    
        known_nets_names = frozenset([e[0] for e in known_nets])
        net_to_use = list(nets & known_nets_names)
    
        try:
            net_to_use = net_to_use[0]
            pwd = dict(known_nets)[net_to_use]
            sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
            wl.connect(net_to_use, (sec, pwd), timeout=10000)
        except:
            wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)
    

  • Pybytes Beta

    It doen't work when using a serial connection to the LoPy.
    When I tried a wifi connection it's able to run and seems to work fine.


  • Pybytes Beta

    I try to run this script from Pymakr first but nothing happens and REPL window of LoPy becomes dead.
    So I thought it has something to do with the script and removed the dupterm part (because it's already in my boot.py and not needed from Pymakr). Still the same result.
    What could be the problem?



  • I think it's a little easier to use the dict directly:

    known_nets = {
        # SSID : PSK (passphrase)
        'home_ssid': 'home_password',
        'office_ssid': 'office_password',
    } # change this dict to match your WiFi settings
    
    import machine
    import os
    
    # disable these two lines if you don't want serial access
    uart = machine.UART(0, 115200)
    os.dupterm(uart)
    
    # test needed to avoid losing connection after a soft reboot
    if machine.reset_cause() != machine.SOFT_RESET: 
        from network import WLAN
        wl = WLAN()
    
        # save the default ssid and auth
        original_ssid = wl.ssid()
        original_auth = wl.auth()
    
        wl.mode(WLAN.STA)
    
        for ssid, bssid, sec, channel, rssi in wl.scan():
            # Note: could choose priority by rssi if desired
            try:
                wl.connect(ssid, (sec, known_nets[ssid]), timeout=10000)
                break
            except KeyError:
                pass # unknown SSID
        else:
            wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)
    

    Exercise left to do: try other recognized nets if association fails with one. Another possible feature is picking a less contended channel.


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.