No Wifi connection on boot



  • Hi,

    it tried all example and tutorials but my LoPy is not connecting to wifi after a reset. The problem is always the same - it tries and fails. If i interrupt the script after a failed connection and paste the connect() to the REPL it is connecting fine, so basiclly my settings / credentials are fine.

    here is my current wifi code:

    known_nets = {
            # SSID : PSK (passphrase)
            'waterworld': '​secretkey'
        } # change this dict to match your WiFi settings
    
    # test needed to avoid losing connection after a soft reboot
    if machine.reset_cause() != machine.SOFT_RESET:
        from network import WLAN
        wl = WLAN(mode=WLAN.STA)
    
        while not wl.isconnected():
            print("new round")
            for ssid, bssid, sec, channel, rssi in wl.scan():
                # Note: could choose priority by rssi if desired
    
                try:
                    print("trying to connect to " + ssid)
                    wl.connect(ssid, (sec, known_nets[ssid]), timeout=5000)
    
                    if not wl.isconnected():
                        print("reinit")
                        time.sleep(5)
                        wl.deinit()
                        wl.init(mode=WLAN.STA)
                    break
                except KeyError:
                    pass # unknown SSID
    print(wl.ifconfig())
    

    The funny thing on my code is now, after a reinit, th LoPy doesn't find my network anymore, so after that is goes to an endless loop.

    What am i doing wrong?
    Thanks for help!



  • @livius First i tried it in boot.py - then i moved it to main.py. Both with no success. Then i had an idea. I'm using a FritzBox with Wifi on 2.4 and 5 GHz with the same ssid. I think this is the root cause. I enabled the guest AP and tried to connect to this - and this is working. So at least i have workaround for me! But anyway , thanks a lot for your help!



  • @poebler
    one more question. Where you put this code? In boot.py?
    If yest - move it to main.py and back withresults.



  • @markN Hi, Thanks for your help! I tried it - but again with no success!



  • @poebler said in No Wifi connection on boot:
    I had a similar problem connecting to my home wifi network using the standard wifi code example.
    After some experimentation I found that a short wait immediately after the line containing "wlan.connect(...)" cured the issue in my code. I assume this may be a bug, possibly some kind of internal race condition between wlan.connect() and wlan.isconnected().
    If its the same problem this extra line in your code might help.

                wl.connect(ssid, (sec, known_nets[ssid]), timeout=5000)
                time.sleep(1)


  • @livius tried it. no success :(



  • @poebler said in No Wifi connection on boot:

    if it is not connecting a static IP won't help

    Sometimes it help ;-)



  • @livius I'm at work so i can try it first this evening. But the connect methods returns after the 5 sec timeout and if it is not connecting a static IP won't help :/



  • @poebler
    what if you add debug info

    print(time.ticks_ms())
    wl.connect(ssid, (sec, known_nets[ssid]), timeout=5000)
    print(time.ticks_ms())
    if not wl.isconnected():
              print(time.ticks_ms())
              print("reinit")        
    

    show debug info
    and what if you change connection as static IP?

    # go for fixed IP settings (IP, Subnet, Gateway, DNS)
    wlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '192.168.0.1'))
    

    any change?



  • @livius tried this also - no change :/



  • @poebler
    what happened if you change this line
    wl.connect(ssid, (sec, known_nets[ssid]), timeout=5000) to

    wl.connect(ssid=ssid, auth=(sec, known_nets[ssid]), timeout=5000)
    

    ?



  • @livius 1.7.3.b2



  • @poebler
    i was wrong in first post.
    You do not pass None - you got error on dictionary key access and not tried to connect
    then problem must be somewhere else.

    But first question should be - what is your firmware version?

    os.uname()
    


  • Hi,
    thanks for your reply. This could be a problem - but not the initial one, that i find my network but i'm not able to connect though my credentials are right and a manual connect succededs ...

    Any other ideas?

    Thanks in advance!



  • @poebler
    You loop throught all networks in range
    but you pass parameter only for known network

    wl.connect(ssid, (sec, known_nets[ssid]), timeout=5000)
    

    for unknown you pass None - i suppose this can be a problem here
    i known that you include try except - but here it is reall wrong.
    change your logic and
    check before connect if this is known network if not then do not try to connect to it


Log in to reply
 

Pycom on Twitter