How to restart WLAN connection?



  • Hi all,

    I'm trying to enable/disable Wi Fi module pressing a button, in order to reduce power consuption.

    Here is a code snippet of the wlan initialization (the functions are declared inside a class, thus the self keyword is written):

    def wlan_start(self, wlan_conf, network_conf):
         # First way I've tried
         self.wlan = WLAN(mode = WLAN.STA)
         # Second way I've tried
         self.wlan.init( mode = WLAN.STA )
          # Third way I've tried
         self.wlan.init()
         self.wlan.mode( WLAN.STA )
         ##########
         self.wlan.ifconfig(config = (network_conf['ip'], network_conf['netmask'], network_conf['gateway'], '8.8.8.8') )
         self.wlan.antenna(WLAN.EXT_ANT)
         self.wlan.connect( wlan_conf['ssid'], auth=(WLAN.WPA2, wlan_conf['pass']), timeout = network_conf['timeout'] )
         while not self.wlan.isconnected():
             machine.idle()
         print("WLAN connected!")
    
    def wlan_deinit(self):
        self.wlan.deinit()
    

    The first time I run my code, the LoPy4 connects to my network without a problem with the three ways I've mentioned. When I invoke wlan_deinit() the power consumption decreases, as expected. But when I call wlan_init(), the module stays forever inside the while loop, and I have to reset the device manually. I don't know if there is another way of doing it. The documentation doesn't explain anything else.

    If I do the same thing with the device working as an Acess Point, I don't experience these problems.

    self.wlan.init( mode = WLAN.AP, ssid = wlan_conf['ap_ssid'], auth = (WLAN.WPA2, wlan_conf['ap_pass']) )
    self.wlan.ifconfig(id = 1, config = (network_conf['ip'], network_conf['netmask'], network_conf['gateway'], '8.8.8.8') )
    self.wlan.antenna(WLAN.EXT_ANT)
    

    Any suggestion? Maybe it is a firmware issue?

    Thanks in advance!



Pycom on Twitter