Lopy can't reconnect to access point after connection is lost.



  • Hi I wrote some code for my Lopy that should reconnect to my access point after the connection is lost. I'm using the latest firmware version 1.5.0.b2.

    Here's my code:

    import network
    from network import WLAN
    import utime
    
    class Wlan():
        
        _ssid = "ssid"
        _passw = "secret"
        wlan = network.WLAN(mode=WLAN.STA)
        connected = False
        
        
        def __init__(self, ssid, passw):
            self._ssid = ssid
            self._passw = passw
            self.connected = self.wlan.isconnected()
            self.connect_to_hotspot()
            
        def connect_to_hotspot(self):
            while not self.connected:
                print('scanning: ')
                networks = self.wlan.scan()
                for net in networks:
                    print(net.ssid)
                    
                    if net.ssid == self._ssid:  
                        print('found %s' % self._ssid)
                        self.wlan.connect(self._ssid, auth=(WLAN.WPA2, self._passw))
                        while not self.wlan.isconnected():
                            utime.sleep_ms(50)
                        print(self.wlan.ifconfig())
                        self.connected = True;
                
                if not self.connected:
                    utime.sleep_ms(1000)
                    print('not connected')
                    
            print('connected')
            
            
    
    wlan = Wlan("secret",  "ssid")
    
    while wlan.wlan.isconnected():
        pass
    
    print('wlan failed try to reconnect')
    wlan.wlan.deinit()
    wlan.connected = False
    utime.sleep(5)
    wlan.wlan.init(mode=WLAN.STA)
    wlan.connect_to_hotspot()
    

    The code runs and connects to my access point. After the connection is lost. I see that the Lopy is trying to reconnect and lists all the wlans. However, the Lopy can't find the network it previous connected to doesn’t show up. Even after disabling the wlan and enabling it again, it still can't find the right wireless network.

    If I cut the power for a view seconds and reboot, the wireless network is found and the Lopy connects.

    What’s going on? Please help.


Log in to reply
 

Pycom on Twitter