Overview
ESP32-C5 Wi-Fi Feature List
The following features are supported:
4 virtual Wi-Fi interfaces, which are STA, AP, Sniffer and reserved
Station-only mode, AP-only mode, station/AP-coexistence mode
IEEE 802.11b, IEEE 802.11g, IEEE 802.11n, IEEE 802.11a, IEEE 802.11ac, IEEE 802.11ax, and APIs to configure the protocol mode
WPA/WPA2/WPA3/WPA2-Enterprise/WPA3-Enterprise/WAPI/WPS and DPP
AMSDU, AMPDU, HT40, QoS, and other key features
Modem-sleep
The Espressif-specific ESP-NOW protocol and Long Range mode (only supported on 2.4 GHz band), which supports up to 1 km of data traffic
Up to 20 MBit/s TCP throughput and 30 MBit/s UDP throughput over the air
Sniffer
Both fast scan and all-channel scan
Multiple antennas
Channel state information
Individual TWT and Broadcast TWT
Downlink MU-MIMO
OFDMA
BSS Color
Wi-Fi Aware (NAN)
How To Write a Wi-Fi Application
Preparation
Generally, the most effective way to begin your own Wi-Fi application is to select an example which is similar to your own application, and port the useful part into your project. It is not a MUST, but it is strongly recommended that you take some time to read this article first, especially if you want to program a robust Wi-Fi application.
This article is supplementary to the Wi-Fi APIs/Examples. It describes the principles of using the Wi-Fi APIs, the limitations of the current Wi-Fi API implementation, and the most common pitfalls in using Wi-Fi. This article also reveals some design details of the Wi-Fi driver. We recommend you to select an example .
wifi/getting_started/station demonstrates how to use the station functionality to connect to an AP.
wifi/getting_started/softAP demonstrates how to use the SoftAP functionality to configure ESP32-C5 as an AP.
wifi/scan demonstrates how to scan for available APs, configure the scan settings, and display the scan results.
wifi/fast_scan demonstrates how to perform fast and all channel scans for nearby APs, set thresholds for signal strength and authentication modes, and connect to the best fitting AP based on signal strength and authentication mode.
wifi/wps demonstrates how to use the WPS enrollee feature to simplify the process of connecting to a Wi-Fi router, with options for PIN or PBC modes.
wifi/wps_softap_registrar demonstrates how to use the WPS registrar feature on SoftAP mode, simplifying the process of connecting to a Wi-Fi SoftAP from a station.
wifi/smart_config demonstrates how to use the smartconfig feature to connect to a target AP using the ESPTOUCH app.
wifi/power_save demonstrates how to use the power save mode in station mode.
wifi/softap_sta demonstrates how to configure ESP32-C5 to function as both an AP and a station simultaneously, effectively enabling it to act as a Wi-Fi NAT router.
wifi/iperf demonstrates how to implement the protocol used by the iPerf performance measurement tool, allowing for performance measurement between two chips or between a single chip and a computer running the iPerf tool, with specific instructions for testing station/soft-AP TCP/UDP RX/TX throughput.
wifi/roaming/roaming_app demonstrates how to use the Wi-Fi Roaming App functionality to efficiently roam between compatible APs.
wifi/roaming/roaming_11kvr demonstrates how to implement roaming using 11k and 11v APIs.
wifi/itwt demonstrates how to use the iTWT feature, which only works in station mode and under different power save modes, with commands for setup, teardown, and suspend, and also shows the difference in current consumption when iTWT is enabled or disabled.
Setting Wi-Fi Compile-time Options
Refer to Wi-Fi Menuconfig.
Init Wi-Fi
Refer to ESP32-C5 Wi-Fi Station General Scenario and ESP32-C5 Wi-Fi AP General Scenario.
Start/Connect Wi-Fi
Refer to ESP32-C5 Wi-Fi Station General Scenario and ESP32-C5 Wi-Fi AP General Scenario.
Event-Handling
Generally, it is easy to write code in "sunny-day" scenarios, such as WIFI_EVENT_STA_START and WIFI_EVENT_STA_CONNECTED. The hard part is to write routines in "rainy-day" scenarios, such as WIFI_EVENT_STA_DISCONNECTED. Good handling of "rainy-day" scenarios is fundamental to robust Wi-Fi applications. Refer to ESP32-C5 Wi-Fi Event Description, ESP32-C5 Wi-Fi Station General Scenario, and ESP32-C5 Wi-Fi AP General Scenario. See also the overview of the Event Loop Library in ESP-IDF.
Write Error-Recovery Routines Correctly at All Times
Just like the handling of "rainy-day" scenarios, a good error-recovery routine is also fundamental to robust Wi-Fi applications. Refer to ESP32-C5 Wi-Fi API Error Code.
ESP32-C5 Wi-Fi API Error Code
All of the ESP32-C5 Wi-Fi APIs have well-defined return values, namely, the error code. The error code can be categorized into:
No errors, e.g.,
ESP_OKmeans that the API returns successfully.Recoverable errors, such as
ESP_ERR_NO_MEM.Non-recoverable, non-critical errors.
Non-recoverable, critical errors.
Whether the error is critical or not depends on the API and the application scenario, and it is defined by the API user.
The primary principle to write a robust application with Wi-Fi API is to always check the error code and write the error-handling code. Generally, the error-handling code can be used:
For recoverable errors, in which case you can write a recoverable-error code. For example, when
esp_wifi_start()returnsESP_ERR_NO_MEM, the recoverable-error code vTaskDelay can be called in order to get a microseconds' delay for another try.For non-recoverable, yet non-critical errors, in which case printing the error code is a good method for error handling.
For non-recoverable and also critical errors, in which case "assert" may be a good method for error handling. For example, if
esp_wifi_set_mode()returnsESP_ERR_WIFI_NOT_INIT, it means that the Wi-Fi driver is not initialized byesp_wifi_init()successfully. You can detect this kind of error very quickly in the application development phase.
In esp_common/include/esp_err.h, ESP_ERROR_CHECK checks the return values. It is a rather commonplace error-handling code and can be used as the default error-handling code in the application development phase. However, it is strongly recommended that API users write their own error-handling code.
ESP32-C5 Wi-Fi API Parameter Initialization
When initializing struct parameters for the API, one of two approaches should be followed:
Explicitly set all fields of the parameter.
Use get API to get current configuration first, then set application specific fields.
Initializing or getting the entire structure is very important, because most of the time the value 0 indicates that the default value is used. More fields may be added to the struct in the future and initializing these to zero ensures the application will still work correctly after ESP-IDF is updated to a new release.
ESP32-C5 Wi-Fi Programming Model
The ESP32-C5 Wi-Fi programming model is depicted as follows:

Wi-Fi Programming Model
The Wi-Fi driver can be considered a black box that knows nothing about high-layer code, such as the TCP/IP stack, application task, and event task. The application task (code) generally calls Wi-Fi driver APIs to initialize Wi-Fi and handles Wi-Fi events when necessary. Wi-Fi driver receives API calls, handles them, and posts events to the application.
Wi-Fi event handling is based on the esp_event library. Events are sent by the Wi-Fi driver to the default event loop. Application may handle these events in callbacks registered using esp_event_handler_register(). Wi-Fi events are also handled by esp_netif component to provide a set of default behaviors. For example, when Wi-Fi station connects to an AP, esp_netif will automatically start the DHCP client by default.
ESP32-C5 Wi-Fi Event Description
WIFI_EVENT_WIFI_READY
The Wi-Fi driver will never generate this event, which, as a result, can be ignored by the application event callback. This event may be removed in future releases.
WIFI_EVENT_SCAN_DONE
The scan-done event is triggered by esp_wifi_scan_start() and will arise in the following scenarios:
The scan is completed, e.g., the target AP is found successfully, or all channels have been scanned.
The scan is stopped by
esp_wifi_scan_stop().The
esp_wifi_scan_start()is called before the scan is completed. A new scan will override the current scan and a scan-done event will be generated.
The scan-done event will not arise in the following scenarios:
It is a blocked scan.
The scan is caused by
esp_wifi_connect().
Upon receiving this event, the event task does nothing. The application event callback needs to call esp_wifi_scan_get_ap_num() and esp_wifi_scan_get_ap_records() to fetch the scanned AP list and trigger the Wi-Fi driver to free the internal memory which is allocated during the scan (do not forget to do this!).
Refer to ESP32-C5 Wi-Fi Scan for a more detailed description.
WIFI_EVENT_STA_START
If esp_wifi_start() returns ESP_OK and the current Wi-Fi mode is station or station/AP, then this event will arise. Upon receiving this event, the event task will initialize the LwIP network interface (netif). Generally, the application event callback needs to call esp_wifi_connect() to connect to the configured AP.
WIFI_EVENT_STA_STOP
If esp_wifi_stop() returns ESP_OK and the current Wi-Fi mode is station or station/AP, then this event will arise. Upon receiving this event, the event task will release the station's IP address, stop the DHCP client, remove TCP/UDP-related connections, and clear the LwIP station netif, etc. The application event callback generally does not need to do anything.
WIFI_EVENT_STA_CONNECTED
If esp_wifi_connect() returns ESP_OK and the station successfully connects to the target AP, the connection event will arise. Upon receiving this event, the event task starts the DHCP client and begins the DHCP process of getting the IP address. Then, the Wi-Fi driver is ready for sending and receiving data. This moment is good for beginning the application work, provided that the application does not depend on LwIP, namely the IP address. However, if the application is LwIP-based, then you need to wait until the got ip event comes in.
WIFI_EVENT_STA_DISCONNECTED
This event can be generated in the following scenarios:
When
esp_wifi_disconnect()oresp_wifi_stop()is called and the station is already connected to the AP.When
esp_wifi_connect()is called, but the Wi-Fi driver fails to set up a connection with the AP due to certain reasons, e.g., the scan fails to find the target AP or the authentication times out. If there are more than one AP with the same SSID, the disconnected event will be raised after the station fails to connect all of the found APs.When the Wi-Fi connection is disrupted because of specific reasons, e.g., the station continuously loses N beacons, the AP kicks off the station, or the AP's authentication mode is changed.
Upon receiving this event, the default behaviors of the event task are:
Shutting down the station's LwIP netif.
Notifying the LwIP task to clear the UDP/TCP connections which cause the wrong status to all sockets. For socket-based applications, the application callback can choose to close all sockets and re-create them, if necessary, upon receiving this event.
The most common event handle code for this event in application is to call esp_wifi_connect() to reconnect the Wi-Fi. However, if the event is raised because esp_wifi_disconnect() is called, the application should not call esp_wifi_connect() to reconnect. It is the application's responsibility to distinguish whether the event is caused by esp_wifi_disconnect() or other reasons. Sometimes a better reconnection strategy is required. Refer to Wi-Fi Reconnect and Scan When Wi-Fi Is Connecting.
Another thing that deserves attention is that the default behavior of LwIP is to abort all TCP socket connections on receiving the disconnect. In most cases, it is not a problem. However, for some special applications, this may not be what they want. Consider the following scenarios:
The application creates a TCP connection to maintain the application-level keep-alive data that is sent out every 60 seconds.
Due to certain reasons, the Wi-Fi connection is cut off, and the WIFI_EVENT_STA_DISCONNECTED is raised. According to the current implementation, all TCP connections will be removed and the keep-alive socket will be in a wrong status. However, since the application designer believes that the network layer should ignore this error at the Wi-Fi layer, the application does not close the socket.
Five seconds later, the Wi-Fi connection is restored because
esp_wifi_connect()is called in the application event callback function. Moreover, the station connects to the same AP and gets the same IPV4 address as before.Sixty seconds later, when the application sends out data with the keep-alive socket, the socket returns an error and the application closes the socket and re-creates it when necessary.
In above scenarios, ideally, the application sockets and the network layer should not be affected, since the Wi-Fi connection only fails temporarily and recovers very quickly.
IP_EVENT_STA_GOT_IP
This event arises when the DHCP client successfully gets the IPV4 address from the DHCP server, or when the IPV4 address is changed. The event means that everything is ready and the application can begin its tasks (e.g., creating sockets).
The IPV4 may be changed because of the following reasons:
The DHCP client fails to renew/rebind the IPV4 address, and the station's IPV4 is reset to 0.
The DHCP client rebinds to a different address.
The static-configured IPV4 address is changed.
Whether the IPV4 address is changed or not is indicated by the field ip_change of ip_event_got_ip_t.
The socket is based on the IPV4 address, which means that, if the IPV4 changes, all sockets relating to this IPV4 will become abnormal. Upon receiving this event, the application needs to close all sockets and recreate the application when the IPV4 changes to a valid one.
IP_EVENT_GOT_IP6
This event arises when the IPV6 SLAAC support auto-configures an address for the ESP32-C5, or when this address changes. The event means that everything is ready and the application can begin its tasks, e.g., creating sockets.
IP_EVENT_STA_LOST_IP
This event arises when the IPV4 address becomes invalid.
IP_EVENT_STA_LOST_IP does not arise immediately after the Wi-Fi disconnects. Instead, it starts an IPV4 address lost timer (configurable via CONFIG_ESP_NETIF_LOST_IP_TIMER_ENABLE and CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL). If the IPV4 address is got before the timer expires, IP_EVENT_STA_LOST_IP does not happen. Otherwise, the event arises when the IPV4 address lost timer expires.
Generally, the application can ignore this event, because it is just a debug event to inform that the IPV4 address is lost.
WIFI_EVENT_AP_START
Similar to WIFI_EVENT_STA_START.
WIFI_EVENT_AP_STOP
Similar to WIFI_EVENT_STA_STOP.
WIFI_EVENT_AP_STACONNECTED
Every time a station is connected to ESP32-C5 AP, the WIFI_EVENT_AP_STACONNECTED will arise. Upon receiving this event, the event task will do nothing, and the application callback can also ignore it. However, you may want to do something, for example, to get the info of the connected STA.
WIFI_EVENT_AP_STADISCONNECTED
This event can happen in the following scenarios:
The application calls
esp_wifi_disconnect(), oresp_wifi_deauth_sta(), to manually disconnect the station.The Wi-Fi driver kicks off the station, e.g., because the AP has not received any packets in the past five minutes. The time can be modified by
esp_wifi_set_inactive_time().The station kicks off the AP.
When this event happens, the event task will do nothing, but the application event callback needs to do something, e.g., close the socket which is related to this station.
WIFI_EVENT_AP_PROBEREQRECVED
This event is disabled by default. The application can enable it via API esp_wifi_set_event_mask().
When this event is enabled, it will be raised each time the AP receives a probe request.
WIFI_EVENT_STA_BEACON_TIMEOUT
If the station does not receive the beacon of the connected AP within the inactive time, the beacon timeout happens, the WIFI_EVENT_STA_BEACON_TIMEOUT will arise. The application can set inactive time via API esp_wifi_set_inactive_time().
WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START
The WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START will arise at the start of connectionless module Interval. See connectionless module power save.
ESP32-C5 Wi-Fi Configuration
All configurations will be stored into flash when the Wi-Fi NVS is enabled; otherwise, refer to Wi-Fi NVS Flash.
Wi-Fi Mode
Call esp_wifi_set_mode() to set the Wi-Fi mode.
Mode |
Description |
|---|---|
|
NULL mode: in this mode, the internal data struct is not allocated to the station and the AP, while both the station and AP interfaces are not initialized for RX/TX Wi-Fi data. Generally, this mode is used for Sniffer, or when you only want to stop both the station and the AP without calling |
|
Station mode: in this mode, |
|
AP mode: in this mode, |
|
Station/AP coexistence mode: in this mode, |
Wi-Fi Band Mode Configuration
The Wi-Fi band mode used by ESP32-C5 can be set via the function esp_wifi_set_band_mode().
Mode |
Description |
|---|---|
|
2.4 GHz band mode: The device operates only on 2.4 GHz band channels. |
|
5 GHz band mode: The device operates only on 5 GHz band channels. |
|
2.4 GHz + 5 GHz auto mode: The device automatically selects either the 2.4 GHz or 5 GHz band based on the connected AP or SoftAP configuration. |
Note
WIFI_BAND_MODE_AUTOdoes not mean simultaneous dual-band support; it only allows automatic band selection.
When operating in WIFI_BAND_MODE_AUTO mode, protocols and bandwidth can be configured separately for the 2.4 GHz and 5 GHz bands:
Use the function
esp_wifi_set_protocols()to set the supported protocol types for each band (e.g., 802.11b/g/n/ac/ax);Use the function
esp_wifi_set_bandwidths()to set the bandwidth for each band (e.g., 20 MHz, 40 MHz).
AP Choose
When the device scans multiple APs with the same SSID, ESP32-C5 selects the most suitable AP to connect to based on signal strength (RSSI) and band information. The default policy usually prefers the AP with higher RSSI; however, in environments where 2.4 GHz and 5 GHz coexist, this can cause the device to favor the 2.4 GHz band, ignoring the performance benefits of the 5 GHz band.
To address this, ESP-IDF provides the field rssi_5g_adjustment in the wifi_scan_threshold_t structure to optimize the priority of selecting 5 GHz APs.
Field |
Description |
|---|---|
|
Used to adjust priority between 2.4 GHz and 5 GHz APs with the same SSID. The default value is |
Example:
Suppose the device scans the following two APs with the SSID "MyWiFi":
2.4 GHz AP: RSSI = -60 dBm
5 GHz AP: RSSI = -68 dBm
Since rssi_5g_adjustment = 10 (default) and -68 > -60 - 10 holds true, the device will prioritize connecting to the 5 GHz AP.
Note
This parameter only takes effect when scanning results contain APs with the same SSID on both 2.4 GHz and 5 GHz bands. Its purpose is to avoid always connecting to a 2.4 GHz network with slightly stronger signal but poorer performance.
Station Basic Configuration
API esp_wifi_set_config() can be used to configure the station. And the configuration will be stored in NVS. The table below describes the fields in detail.
Field |
Description |
|---|---|
ssid |
This is the SSID of the target AP, to which the station wants to connect. |
password |
Password of the target AP. |
scan_method |
For |
bssid_set |
If bssid_set is 0, the station connects to the AP whose SSID is the same as the field “ssid”, while the field “bssid” is ignored. In all other cases, the station connects to the AP whose SSID is the same as the “ssid” field, while its BSSID is the same the “bssid” field . |
bssid |
This is valid only when bssid_set is 1; see field “bssid_set”. |
channel |
If the channel is 0, the station scans the channel 1 ~ N to search for the target AP; otherwise, the station starts by scanning the channel whose value is the same as that of the “channel” field, and then scans the channel 1 ~ N but skip the specific channel to find the target AP. For example, if the channel is 3, the scan order will be 3, 1, 2, 4,..., N. If you do not know which channel the target AP is running on, set it to 0. |
sort_method |
This field is only for If the sort_method is If the sort_method is |
threshold |
The threshold is used to filter the found AP. If the RSSI or security mode is less than the configured threshold, the AP will be discarded. If the RSSI is set to 0, it means the default threshold and the default RSSI threshold are -127 dBm. If the authmode threshold is set to 0, it means the default threshold and the default authmode threshold are open. |
Attention
WEP/WPA security modes are deprecated in IEEE 802.11-2016 specifications and are recommended not to be used. These modes can be rejected using authmode threshold by setting threshold as WPA2 by threshold.authmode as WIFI_AUTH_WPA2_PSK.
AP Basic Configuration
API esp_wifi_set_config() can be used to configure the AP. And the configuration will be stored in NVS. The table below describes the fields in detail.
Field |
Description |
|---|---|
ssid |
SSID of AP; if the ssid[0] is 0xFF and ssid[1] is 0xFF, the AP defaults the SSID to |
password |
Password of AP; if the auth mode is |
ssid_len |
Length of SSID; if ssid_len is 0, check the SSID until there is a termination character. If ssid_len > 32, change it to 32; otherwise, set the SSID length according to ssid_len. |
channel |
Channel of AP; if the channel is out of range, the Wi-Fi driver will return error. So, please make sure the channel is within the required range. For more details, refer to Wi-Fi Country Code. |
authmode |
Auth mode of ESP AP; currently, ESP AP does not support AUTH_WEP. If the authmode is an invalid value, AP defaults the value to |
ssid_hidden |
If ssid_hidden is 1, AP does not broadcast the SSID; otherwise, it does broadcast the SSID. |
max_connection |
The max number of stations allowed to connect in, the default value is 10. ESP Wi-Fi supports up to 15 ( |
beacon_interval |
Beacon interval; the value is 100 ~ 60000 ms, with default value being 100 ms. If the value is out of range, AP defaults it to 100 ms. |
Wi-Fi Protocol Mode
Currently, the ESP-IDF supports the following protocol modes:
2.4 GHz band: Supports 802.11b, 802.11bg, 802.11bgn, 802.11bgnax, and Espressif's proprietary LR mode.
5 GHz band: Supports 802.11a, 802.11an, 802.11anac, and 802.11anacax.
ESP32-C5 supports configuring Wi-Fi protocol modes for the 2.4 GHz and 5 GHz bands separately. It is recommended to use esp_wifi_set_protocols() for this purpose. The legacy API esp_wifi_set_protocol() is also supported.
Recommended Usage
Use the new API esp_wifi_set_protocols() to configure each band independently:
// Set 2.4 GHz to use 802.11bgnax, and 5 GHz to use 802.11anacax
wifi_protocols_t protocols = {
.ghz_2g = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX,
.ghz_5g = WIFI_PROTOCOL_11A | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AC | WIFI_PROTOCOL_11AX,
};
esp_wifi_set_protocols(WIFI_IF_STA, &protocols);
Legacy Usage
Use the legacy API esp_wifi_set_protocol() to configure the protocol mode for 2.4 GHz band or 5 GHz band:
// Set band mode to 2.4 GHz band
esp_wifi_set_band_mode(WIFI_BAND_MODE_2G_ONLY);
// Set protocol of station to 802.11bgnax
uint8_t protocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX;
esp_wifi_set_protocol(WIFI_IF_STA, protocol);
Note
The new API esp_wifi_set_protocols() allows configuring both bands simultaneously and is recommended for use on ESP32-C5.
The function
esp_wifi_set_protocol()is suitable for single-band scenarios, such as whenWIFI_BAND_MODE_2G_ONLYorWIFI_BAND_MODE_5G_ONLYis used. It only takes effect on the currently connected band. For example, if the interface is operating on the 5 GHz band, any configuration for the 2.4 GHz band will be ignored.If the configuration includes unsupported protocol combinations, the function will return an error.
To enable Espressif's proprietary LR mode, make sure to include WIFI_PROTOCOL_LR in the 2.4 GHz protocol configuration.
Wi-Fi Bandwidth Mode
ESP32-C5 currently supports two bandwidth modes, 20 MHz and 40 MHz, which are used in combination with protocol modes. Common combinations include:
HT20: 802.11n/11an, 20 MHz bandwidth
HT40: 802.11n/11an, 40 MHz bandwidth
HE20: 802.11ax, 20 MHz bandwidth
Note
The 40 MHz bandwidth mode is only supported under 802.11n (2.4 GHz) or 802.11an (5 GHz) modes.
Applications can use the esp_wifi_set_bandwidths() API to set independent bandwidths for 2.4 GHz and 5 GHz bands.
Example:
// Set 2.4 GHz to use 802.11bgnax, and 5 GHz to use 802.11an
wifi_protocols_t protocols = {
.ghz_2g = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX,
.ghz_5g = WIFI_PROTOCOL_11A | WIFI_PROTOCOL_11N,
};
esp_wifi_set_protocols(WIFI_IF_STA, &protocols);
// Set bandwidth to 20 MHz for 2.4 GHz band and 40 MHz for 5 GHz band
wifi_bandwidths_t bw = {
.ghz_2g = WIFI_BW_HT20,
.ghz_5g = WIFI_BW_HT40
};
esp_wifi_set_bandwidths(WIFI_IF_STA, &bw);
Note
When .ghz_2g is set to 0, only the 5 GHz bandwidth is updated, and the 2.4 GHz bandwidth remains unchanged.
When .ghz_5g is set to 0, only the 2.4 GHz bandwidth is updated, and the 5 GHz bandwidth remains unchanged.
Legacy Usage
Use the legacy API esp_wifi_set_bandwidth() to configure the bandwidth of the 2.4 GHz or 5 GHz band:
// Set band mode to 5 GHz band
esp_wifi_set_band_mode(WIFI_BAND_MODE_5G_ONLY);
// Set protocol of the station interface to 802.11an
uint8_t protocol = WIFI_PROTOCOL_11A | WIFI_PROTOCOL_11N;
esp_wifi_set_protocol(WIFI_IF_STA, protocol);
// Set bandwidth of the station interface to 40 MHz
esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT40);
Note
The new API
esp_wifi_set_bandwidths()can configure both 2.4 GHz and 5 GHz bandwidths simultaneously, and is recommended for use on ESP32-C5.esp_wifi_set_bandwidth()is suitable for single-band scenarios, such asWIFI_BAND_MODE_2G_ONLYorWIFI_BAND_MODE_5G_ONLYmodes. It only affects the currently connected band. For example, if the interface is on the 5 GHz band, any 2.4 GHz bandwidth settings will be ignored.If the configured bandwidth is not supported on the current band, the function will return an error.
Long Range (LR)
Long Range (LR) mode is an Espressif-patented Wi-Fi mode which can achieve a one-kilometer line of sight range. Compared to the traditional 802.11b mode, it has better reception sensitivity, stronger anti-interference ability, and longer transmission distance.
LR Compatibility
Since LR is an Espressif-unique Wi-Fi mode operating on the 2.4 GHz band, only ESP32-series chips (excluding the ESP32-C2) support LR data transmission and reception. To ensure compatibility, the ESP32 devices should NOT use LR data rates when connected to non-LR-capable devices. This can be enforced by configuring the appropriate Wi-Fi mode:
If the negotiated mode supports LR, ESP32 devices may transmit data at LR rates.
Otherwise, they must default to traditional Wi-Fi data rates.
The following table depicts the Wi-Fi mode negotiation on 2.4 GHz band:
APSTA |
BGNAX |
BGN |
BG |
B |
BGNAXLR |
BGNLR |
BGLR |
BLR |
LR |
|---|---|---|---|---|---|---|---|---|---|
BGNAX |
BGAX |
BGN |
BG |
B |
BGAX |
BGN |
BG |
B |
|
BGN |
BGN |
BGN |
BG |
B |
BGN |
BGN |
BG |
B |
|
BG |
BG |
BG |
BG |
B |
BG |
BG |
BG |
B |
|
B |
B |
B |
B |
B |
B |
B |
B |
B |
|
BGNAXLR |
BGAXLR |
BGNLR |
BGLR |
BLR |
LR |
||||
BGNLR |
BGNLR |
BGNLR |
BGLR |
BLR |
LR |
||||
BGLR |
BGLR |
BGLR |
BGLR |
BLR |
LR |
||||
BLR |
BLR |
BLR |
BLR |
BLR |
LR |
||||
LR |
LR |
LR |
LR |
LR |
LR |
In the above table, the row is the Wi-Fi mode of AP and the column is the Wi-Fi mode of station. The "-" indicates Wi-Fi mode of the AP and station are not compatible.
According to the table, the following conclusions can be drawn:
For LR-enabled AP of ESP32-C5, it is incompatible with traditional 802.11 mode, because the beacon is sent in LR mode.
For LR-enabled station of ESP32-C5 whose mode is NOT LR-only mode, it is compatible with traditional 802.11 mode.
If both station and AP are ESP32 series chips devices (except ESP32-C2) and both of them have enabled LR mode, the negotiated mode supports LR.
If the negotiated Wi-Fi mode supports both traditional 802.11 mode and LR mode, it is the Wi-Fi driver's responsibility to automatically select the best data rate in different Wi-Fi modes and the application can ignore it.
LR Impacts to Traditional Wi-Fi Device
The data transmission in LR rate has no impacts on the traditional Wi-Fi device because:
The CCA and backoff process in LR mode are consistent with 802.11 specification.
The traditional Wi-Fi device can detect the LR signal via CCA and do backoff.
In other words, the transmission impact in LR mode is similar to that in 802.11b mode.
LR Transmission Distance
The reception sensitivity gain of LR is about 4 dB larger than that of the traditional 802.11b mode. Theoretically, the transmission distance is about 2 to 2.5 times the distance of 11B.
LR Throughput
The LR rate has very limited throughput, because the raw PHY data rate LR is 1/2 Mbps and 1/4 Mbps.
When to Use LR
The general conditions for using LR are:
Both the AP and station are Espressif devices.
Long distance Wi-Fi connection and data transmission is required.
Data throughput requirements are very small, such as remote device control.
Dynamic Frequency Selection (DFS)
In the 5 GHz Wi-Fi band, certain channels (e.g., channels 52–144) share spectrum with critical systems such as weather radars. To avoid interference with these systems, Wi-Fi devices must perform specific detection and channel switching mechanisms before operating on these channels—this is known as Dynamic Frequency Selection (DFS).
Enabling DFS allows devices to access more 5 GHz channels, thereby increasing network capacity and reducing interference. This is especially beneficial in high-density deployments or applications that require high bandwidth. The range of DFS channels may vary by country or region; see esp_wifi/regulatory/esp_wifi_regulatory.txt for details.
ESP32-C5 supports DFS channels in the 5 GHz band, but only supports passive radar detection.
Type |
Description |
|---|---|
Passive Radar Detection Supported |
During scanning, the device can listen to DFS channels, detect access points (APs) operating on DFS channels, and connect to them. If the AP detects radar signals and switches to another channel using Channel Switch Announcement (CSA), ESP32-C5 will follow the AP to the new channel. |
Active Radar Detection Not Supported |
ESP32-C5 does not support active radar detection and therefore cannot operate as a DFS AP in SoftAP mode. |
Note
In STA mode, ESP32-C5 can connect to APs operating on DFS channels, provided those channels are detected during scanning.
In SoftAP mode, ESP32-C5 is not allowed to operate on DFS channels to comply with regulatory requirements.
In STA+SoftAP coexistence mode:
If the STA connects to an AP on a DFS channel, the SoftAP is allowed to switch to the same DFS channel using CSA (Channel Switch Announcement).
When the STA disconnects, the SoftAP will switch back to a non-DFS channel via CSA to remain compliant with regulations.
Wi-Fi Country Code
The esp_wifi_set_country() function is used to set the country/region information. The table below details the meaning of each field. Before configuring these fields, please refer to local 2.4 GHz and 5 GHz RF regulations.
Field |
Description |
|---|---|
cc[3] |
Country/region code string identifying the country or region of the station/AP, or a non-country entity. If it is a country/region, the first two bytes comply with the ISO/IEC 3166-1 two-letter country code standard. The third byte has the following meanings:
|
schan |
Start channel number, indicating the minimum channel allowed in the 2.4 GHz band for the configured country/region. |
nchan |
Number of channels. Defines the total number of allowed channels in the 2.4 GHz band. For example, if schan = 1 and nchan = 13, then channels 1 through 13 are allowed. |
policy |
Country/region policy. When the configured country/region conflicts with that of the connected AP, this field determines which information to use. Details are explained below. |
wifi_5g_channel_mask |
Bitmask indicating allowed 5 GHz channels for the station/AP. The mapping between channel numbers and bits can be found in |
A default configuration example:
wifi_country_t config = {
.cc = "01",
.schan = 1,
.nchan = 11,
.wifi_5g_channel_mask = 0xfe,
.policy = WIFI_COUNTRY_POLICY_AUTO,
};
When Wi-Fi is in station/AP coexistence mode, both use the same country/region information. Sometimes, the connected AP's country/region information may differ from the station's preset. For example:
Station configuration:
wifi_country_t config = {
.cc = "JP",
.schan = 1,
.nchan = 14,
.wifi_5g_channel_mask = 0xfe,
.policy = WIFI_COUNTRY_POLICY_AUTO,
};
Connected AP configuration:
wifi_country_t config = {
.cc = "CN",
.schan = 1,
.nchan = 13,
};
In this case, the connected AP's country/region information will be used.
The following table explains which country/region information is used under different Wi-Fi modes and policies, as well as differences in scanning behavior:
Wi-Fi Mode |
Policy |
Description |
|---|---|---|
Station mode |
WIFI_COUNTRY_POLICY_AUTO |
If the connected AP's beacon contains country/region information IE, that information is used; otherwise, the default configuration is used. Scanning behavior:
Note: If an AP with a hidden SSID is on a passive scan channel, scanning may not discover that AP. To discover hidden SSIDs on all channels, use WIFI_COUNTRY_POLICY_MANUAL. |
Station mode |
WIFI_COUNTRY_POLICY_MANUAL |
Always use the configured country/region information. Scanning behavior:
|
AP mode |
WIFI_COUNTRY_POLICY_AUTO |
Always use the configured country/region information. |
AP mode |
WIFI_COUNTRY_POLICY_MANUAL |
Always use the configured country/region information. |
Station/AP coexistence mode |
WIFI_COUNTRY_POLICY_AUTO |
Same behavior as station mode with WIFI_COUNTRY_POLICY_AUTO. If the station is not connected to any AP, the AP uses the configured country/region information; if the station is connected to an external AP, the AP uses the country/region information obtained by the station. |
Station/AP coexistence mode |
WIFI_COUNTRY_POLICY_MANUAL |
Same behavior as station mode with WIFI_COUNTRY_POLICY_MANUAL. The AP always uses the configured country/region information. |
Home Channel
In AP mode, the home channel is defined as the AP channel. In station mode, home channel is defined as the channel of AP which the station is connected to. In station/AP-coexistence mode, the home channel of AP and station must be the same, and if they are different, the station's home channel is always in priority. For example, assume that the AP is on channel 6, and the station connects to an AP whose channel is 9. Since the station's home channel has higher priority, the AP needs to switch its channel from 6 to 9 to make sure that it has the same home channel as the station. While switching channel, the ESP32-C5 in AP mode will notify the connected stations about the channel migration using a Channel Switch Announcement (CSA). Station that supports channel switching will transit without disconnecting and reconnecting to the AP.
Troubleshooting
Please refer to a separate document with Espressif Wireshark User Guide.

