0

I use this code for an asynchronous web server and it works fine:

https://gist.github.com/aallan/3d45a062f26bc425b22a17ec9c81e3b6

EDIT : This link is my only code, so it's easy to reproduce the issue

Problem is, I cant find a way to disable wlan.

The original code is like that: (in different locations, check code)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

So I simply try:

wlan.active(False)

But the web server is still running and print(wlan.active()) returns True...

I tried placing it at MANY locations in the asynchronous web server code, but I coudldnt make it work.

I need to disable the wlan entirely from time to time and I cant make it work... spent the whole day on it.

Thank you!

EDIT 2 : More details after testing

>>> wlan.active(True)
>>> print(wlan.active())
True # as expected

>>> wlan.active(False)
>>> print(wlan.active())
True # ???

>>> wlan.disconnect()
>>> print(wlan.active())
False # ???

wlan.disconnect() seems to put the wlan interface down, which should be what wlan.active(False) does, and it doesnt even do it in fact, because a simple wlan.connect(ssid, password) gets the wlan.active(True) again by itself... so it wasnt really False.

Oh and wlan.active(False) doest not work, at all. There is no scenario where it has any effect.

If someone could explain me that... Thank you

MrTux
  • 32,350
  • 30
  • 109
  • 146
Elmidea
  • 1
  • 1
  • According to https://github.com/micropython/micropython/blob/master/docs/library/network.WLAN.rst `.. method:: WLAN.active([is_active]) Activate ("up") or deactivate ("down") network interface, if boolean argument is passed. Otherwise, query current state if no argument is provided. Most other methods require active interface.` So what you are doing should work. – Jerry Jeremiah Aug 04 '22 at 21:44
  • Can you update your question to include code that reproduces the problem you're asking about? – larsks Aug 04 '22 at 21:52
  • Thank you, the provided link is my only code, so it's easy to reproduce the issue just with that asynchronous web server code – Elmidea Aug 05 '22 at 05:44
  • @larsks I made a second edit (EDIT 2) to give even more details, thanks a lot – Elmidea Aug 05 '22 at 08:56
  • This is probably an issue specific to the Pico W port of micropython. The only thing `WLAN.active(True)` does is load the driver blob for the wifi chip. You are correct that `WLAN.active(False)` does nothing (on STA_IF, it works as expected on AP_IF). – robartsd Nov 12 '22 at 01:01
  • Not sure what you are trying to accomplish by running a webserver with the network connection down. – robartsd Nov 12 '22 at 01:02

1 Answers1

1

I had the same issue, and after some experimentation and digging in the code I found the solution. You have to call the (undocumented) wlan.deinit() method:

def disconnect():
    wlan.disconnect()
    wlan.active(False)
    wlan.deinit()

This will deactivate the wifi chip and reduce the power usage of the board accordingly.

Ben Lund
  • 21
  • 1