What is the difference between an active and connected WLAN?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
blippy
Posts: 1
Joined: Thu May 09, 2019 3:07 pm

What is the difference between an active and connected WLAN?

Post by blippy » Mon Sep 16, 2019 11:18 am

<r>I'm using an ESP32. <br/>
<br/>
I have seen the online documentation for establishing an WLAN connection, but I don't think it's 100%. The typical description is:
<CODE><s>

Code: Select all

</s><i>
</i>    sta = network.WLAN(network.STA_IF)
    if not sta.isconnected():
        print('connecting to network...')
        sta.active(True)
        sta.connect(settings.wifi_essid, settings.wifi_password)
        while not sta.isconnected():
		sleep_ms(delay_ms)
<e>
</e></CODE>

It usually starts OK, but if the MCU has been on a long time, connections can drop. The problem is, if you try to use that code fragment again if you think it has been disconnected, then a connection might never be re-established. <br/>
<br/>
My "fix" is to basically start a watchdog timer on that section of code, and reboot the MCU on a timeout. That "takes care" of the problem, but isn't particularly desirable. Is there a more robust way of doing that?<br/>
<br/>
What does it actually mean to say that sta_if is active anyway? Surely it's either connected or it isn't. Why does it need to be activated.<br/>
<br/>
Might this be a better approach:
<CODE><s>

Code: Select all

</s><i>
</i>sta = network.WLAN(network.STA_IF)
def make_connection(sta, delay_ms = 100):
	if not sta.isconnected():
		print("Connecting to network...")
		st.active(True)
		sta.connect(settings.wifi_essid, settings.wifi_password)
        	while not sta.isconnected():
			sleep_ms(delay_ms)

make_connection(sta)
# do something
# ... MUCH TIME PASSES ...
make_connection(sta)
# use the network
<e>
</e></CODE></r>

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: What is the difference between an active and connected WLAN?

Post by jimmo » Mon Sep 16, 2019 12:29 pm

Think of "active" as if the wifi device is actually turned on. When it's not active, the ESP32 can actually put that part of the chip to sleep, reducing power consumption. On other MicroPython boards (e.g. the PYBD), the wifi is handled by a separate chip, so MicroPython can actually turn off the power to the chip. Maybe "enable" might have been a better name?

Connected means that the wifi actually has an established connection to an access point, i.e. it's done the authentication and is now part of the network.

In theory, the API could have been designed differently such that connect() automatically made the device active (and so would scan()). I don't have a good explanation for why it wasn't done that way.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: What is the difference between an active and connected WLAN?

Post by jimmo » Wed Oct 16, 2019 2:18 am

WiFi uses evenly spaced channels at different frequencies within the 2.4GHz or 5GHz band (in the same way that FM radio stations use different channels in the ~100MHz band). Your access point will typically choose a channel which is used for your entire network. Different devices all run at the same channel. (I'm deliberately saying channel not frequency, because with the sort of modulation used in WiFi e.g. OFDM, there's no simple "carrier" frequency like there is with FM or AM radio).

5GHz is mainly about two things:
- It has different transmission properties, so you might get better coverage in your house.
- It allows higher data rate. You may have heard of 802.11ac "gigabit wifi" which only works on the 5GHz band.

Other appliances (bluetooth, cordless phones, etc) also use the 2.4GHz band, but the interference is fairly minimal due to the different way they modulate the signal. Bluetooth works much more like FM radio -- GFSK with a evenly spaced frequencies throughout the 2.4GHz band, with the modulation designed so that the spectral width is narrow enough to not interfere with devices on different channels.

The reason that 2.4GHz was originally used was that it's not used by any other things (e.g. TV, Radio, etc) because that's where microwave ovens operate (and also they have harmonics at 5GHz etc). Nowadays modern radio protocols are fairly immune to the interference from microwaves so they can reasonably happily co-exist.

As far as MicroPython is concerned, I'm not aware of any MicroPython devices with 5GHz radios.

Post Reply