<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to restart WLAN connection?]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I'm trying to enable/disable Wi Fi module pressing a button, in order to reduce power consuption.</p>
<p dir="auto">Here is a code snippet of the wlan initialization (the functions are declared inside a class, thus the <strong>self</strong> keyword is written):</p>
<pre><code>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(&quot;WLAN connected!&quot;)

def wlan_deinit(self):
    self.wlan.deinit()
</code></pre>
<p dir="auto">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 <strong>wlan_deinit()</strong> the power consumption decreases, as expected. But when I call <strong>wlan_init()</strong>, the module stays forever inside the <strong>while</strong> 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.</p>
<p dir="auto">If I do the same thing with the device working as an Acess Point, I don't experience these problems.</p>
<pre><code>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)
</code></pre>
<p dir="auto">Any suggestion? Maybe it is a firmware issue?</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.pycom.io/topic/3378/how-to-restart-wlan-connection</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 12:04:20 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/3378.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 14 Jun 2018 14:53:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to restart WLAN connection? on Thu, 14 Jun 2018 14:53:04 GMT]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I'm trying to enable/disable Wi Fi module pressing a button, in order to reduce power consuption.</p>
<p dir="auto">Here is a code snippet of the wlan initialization (the functions are declared inside a class, thus the <strong>self</strong> keyword is written):</p>
<pre><code>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(&quot;WLAN connected!&quot;)

def wlan_deinit(self):
    self.wlan.deinit()
</code></pre>
<p dir="auto">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 <strong>wlan_deinit()</strong> the power consumption decreases, as expected. But when I call <strong>wlan_init()</strong>, the module stays forever inside the <strong>while</strong> 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.</p>
<p dir="auto">If I do the same thing with the device working as an Acess Point, I don't experience these problems.</p>
<pre><code>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)
</code></pre>
<p dir="auto">Any suggestion? Maybe it is a firmware issue?</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.pycom.io/post/20293</link><guid isPermaLink="true">https://forum.pycom.io/post/20293</guid><dc:creator><![CDATA[lsoria]]></dc:creator><pubDate>Thu, 14 Jun 2018 14:53:04 GMT</pubDate></item></channel></rss>