def wlscan():
    #activate WLAN
    import network, time
    wl = network.WLAN(network.STA_IF)

    if not wl.active():
        wl.active(True)

    return wl.scan()            

### select WLAN to connect to
def wlbestSSID():
    import network, time
    wl = network.WLAN(network.STA_IF)

    if wl.active():
        wlist=list()
        for w in wlscan():
            wlist.append([w[3],w[0].decode()])
        wlist.sort(reverse=True)
        return wlist[0][1]
    else:
        return []

### try to connect to WLAN
def wlconnect(SSID,pwd):
    import network, time
    wl = network.WLAN(network.STA_IF)


    if wl.active():
        print("debug: wlan active")
        wl.connect(SSID,pwd)
    else:
        print("debug: wlan DEactive")
        wl.active(True)
        wl.connect(SSID,pwd)

    time.sleep(6)
    if wl.isconnected() or wl.status() == 5:
        print("debug: connected")
        return True
    else:
        wl.disconnect()
        print("debug: not connected")
        return False
