wlan.scan() works fine in REPL, not when ran in Script.



  • Hardware: Wipy 3.0 with v1.20.1.r2 and Expansionboard v3.1 with DFU 0.0.11.

    Pymakr 1.5.6

    Issue: wlan.scan(), when performed in REPL returns a list of networks, these seem to be all the expected networks in range. However, when performing wlan.scan() in main.py, list tends to have only a few and sometimes just one entry. Modem that is expected is right next to the device. (Also tried to move it further away, but no luck.)

    Edit: Also just running the code using the 'Run' button seems to work fine, it just won't run properly from internal memory.



  • Hello

    I found a fix to the problem so i though I would share:

    I am new to Pycom and Python i general however I had the exact same problem. I found out by trial and error that if I put in a decent delay between wlan = WLAN(mode=WLAN.STA) and nets = wlan.scan() I can now find all networks when rebooting the LoPy. Seems like any time.sleep() below 5 seconds is not enough. 7-8 seconds seems to reliably find all ssid's.

    I can only speculate with my limited coding abilities to why the delay is necessary. The scanning is performed when changing the WLAN mode and wlan.scan() is just a container for the result? Or maybe the wlan code is not ready for use so quickly after chaning mode? Who knows... I havent looked into the library.

    /SeaAnt



  • So it turns out, after messaging with Robert, that PyBytes was the culprit, apparently if not using it and trying to start your own WiFi connection, it'll possibly conflict with the connection that PyBytes tries to make even if not using it?

    Two things:
    Why is this not mentioned in the docs? Or did I miss anything?
    And, why doesn't the WiFi somehow disable when PyBytes is not used?

    Anyway, thought I'd leave that info here for others who may also run into this same issue.



  • @robert-hh I'm also trying it now on a LoPy with also the latest firmware, but getting the same issue.



  • @robert-hh I'm confused then, I use this script in boot.py:

    import os
    import machine
    
    uart = machine.UART(0, 115200)
    os.dupterm(uart)
    
    known_nets = {
        '<net>': {'pwd': '<password>'},
        '<net>': {'pwd': '<password>', 'wlan_config':  ('10.0.0.114', '255.255.0.0', '10.0.0.1', '10.0.0.1')}, # (ip, subnet_mask, gateway, DNS_server)
    }
    
    if machine.reset_cause() != machine.SOFT_RESET:
        from network import WLAN
        wl = WLAN()
        wl.mode(WLAN.STA)
        original_ssid = wl.ssid()
        original_auth = wl.auth()
    
        print("Scanning for known wifi nets")
        available_nets = wl.scan()
        nets = frozenset([e.ssid for e in available_nets])
    
        known_nets_names = frozenset([key for key in known_nets])
        net_to_use = list(nets & known_nets_names)
        try:
            net_to_use = net_to_use[0]
            net_properties = known_nets[net_to_use]
            pwd = net_properties['pwd']
            sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
            if 'wlan_config' in net_properties:
                wl.ifconfig(config=net_properties['wlan_config'])
            wl.connect(net_to_use, (sec, pwd), timeout=10000)
            while not wl.isconnected():
                machine.idle() # save power while waiting
            print("Connected to "+net_to_use+" with IP address:" + wl.ifconfig()[0])
    
        except Exception as e:
            print("Failed to connect to any known network, going into AP mode")
            wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)
    

    And with the networks added to the list of known_nets, and I start a server for FTP as well, tried both in main or boot, but it's not working, Advanced IP Scanner nor FileZilla see the device as active. I've been struggling with this issue for days, so if you know what's going wrong, that would help me immensely.



  • @NickBoots Once connected, you will stay connected. There is not so much difference between boot.py and main.py. They are just called one after the other.
    Did you try to wait with the scan() call a second after connecting to WiFi? It could be a timing issue, in that scan() called by REPL or by a script imported by main starts just a little bit later.



  • Doing some more tests, it seems that running the scan from boot.py returns more networks. I avoided running my WiFi stuff from boot as the device seems to drop connection once it's done in boot.py and moves on to main.py.

    If the case is that WiFi should be handled in boot.py, then I would like to know how to do this without losing my connection. Do I just connect it again in main.py?


Log in to reply
 

Pycom on Twitter