Connect to wifi



  • Hello, im new on Lopy4 and im doing some little test, with the documentation guide i made some basic functions but when i trie to connect the wifi just dont work, here is the code:

    from network import WLAN
    
    wlan = WLAN(mode=WLAN.STA)
    wlan.connect("my net", auth=(WLAN.WPA2, "my net key"), timeout=5000)
    
    while wlan.isconnected():
        print("network")
    
    while not wlan.isconnected():
        print("no network")
    

    i know that could be better but is just a quick test
    With the code of the documentation, the console say "is connect" but nothing change in the modem page.

    Some suggestion or idea?



  • @ErickM said in Connect to wifi:

    VS say missing imports

    VS do not know pycom libraries at all, so do not worry.



  • Thanks for the anwers, but now i know that my problem is the libraries, for some reason what idk, when i put the network lib, VS say missing imports, im trying to solve that.



  • @livius Well spotted. In my specific case, that is what I want. But it is useful to point that out to the OP.



  • @g0hww
    with your above code, you can hang in not connected state in endless loop.
    You have timeout=5000 but loop is endless:

    while not wlan.isconnected():
                machine.idle()
    

    i do this in this way, below 5second timeout. Put it after wlan.connect:

    chrono = Timer.Chrono()
    chrono.start()
    while not wlan.isconnected():
    	machine.idle() # save power while waiting
    	if chrono.read()>5: #5sedcond timeout
    		break


  • Here is my code

    def wificonnect(wlan=WLAN()):
        if machine.reset_cause() != machine.SOFT_RESET:
            wlan.init(mode=WLAN.STA)
    
        if not wlan.isconnected():
            wlan.connect(config.wifi_ssid, auth=(WLAN.WPA2, config.wifi_pass), timeout=5000)
            while not wlan.isconnected():
                machine.idle()
            print("Connected to wifi with IP address: " + wlan.ifconfig()[0])
    

    Edit: Actually, it might be the case that when your first while loop is encountered, the wifi has not yet been connected, so it moves on from there to the second loop, says it is not connected and then completes when it is connected. Sorry, it is early here and I don't have much time due to work, so I didn't fully appreciate what you had posted.



  • Maybe yielding some processor time in your while loops perhaps, by sleeping?


Log in to reply
 

Pycom on Twitter