← index #6036PR #8991
Related · medium · value 0.595
QUERY · ISSUE

ESP32 WiFi AP Channel Width

openby gigawattsopened 2020-05-13updated 2024-05-27
port-esp32

It seems when creating a WiFi AP, it defaults to a 40 MHz channel bandwidth.

import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid="TEST")
ap.config(authmode=0, channel=2)

In the above example, when viewing the broadcasted SSID with an Android app such as "Wifi Analyzer", shows "CH 2+6" and "2447 - 2407 = 40 MHz".

Is there a setting that can be changed to set this to 20 MHz, so it only uses one channel?

CANDIDATE · PULL REQUEST

ports/esp32: WLAN use esp_wifi_s/get_channel() to set and get wifi channel.

closedby glenn20opened 2022-07-31updated 2024-05-27
port-esp32

This PR updates ports/esp32/network_wlan.c to use the esp_wifi_set_channel() and esp_wifi_get_channel() to set and get the current wifi channel.

Fixes: WLAN.config('channel') returns incorrect channel for AP_IF if STA has connected to an external AP running on a different channel.

Currently, WLAN.config('channel') returns the configured channel for the AP_IF rather than the current channel. The current channel of the AP_IF will change if the STA_IF has connected to an Access Point on a different channel. eg:

import network, time
sta, ap = [network.WLAN(i) for i in (0, 1)]; sta.active(True); ap.active(True)
ap.config(channel=6)
sta.connect(ssid, password)    # Connect to AP running on channel 11
while not sta.isconnected(): time.sleep(0.1)
print("AP channel =", ap.config('channel'))

will print "AP channel = 6" but the AP is actualy running on channel 11. With this PR, the correct "AP channel = 11" will be printed.

Extends: Provides support for setting the secondary wifi channel as well as the primary channel.

WLAN.config(channel=x) will also accept x as a tuple: (p, s) where p is the primary channel and s signifies the secondary channel by:

  • s < 0: WIFI_SECOND_CHAN_BELOW, s == 0: WIFI_SECOND_CHAN_NONE; s > 0: WIFI_SECOND_CHAN_ABOVE

Example:

import network
ap = network.WLAN(1); ap.active(True)
ap.config(channel=6)
ap.config(channel=(6,-1))
ap.config(channel=(6,0))
ap.config(channel=(6,+1))

Also provides support for setting the channel of the STA_IF interface (supported in the IDF since IDFv4+).

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied