[16] ESP32 Wi-Fi usage memo (based on ESP-IDF v4.0+)

Article updated version time Release Notes Revised by
V0.1 Posted on 2020-04-25 initial version libo
V0.2 Posted on 2020-04-27 Add notes, add differences between different versions of Wi-Fi library libo

1. Wi-Fi performance

ESP32 Wi-Fi performance

project parameter
Mode Station, AP, Coexistence
Protocol IEEE-802.11B, IEEE-802.11G, IEEE802.11N, 802.11 LR (Espressif) support software switching
Safety WPA/WPA2/WPA2-Enterprise and WPS
Keyfeature AMPDU, HT40, QoS
Distance 1 km with Espressif-specific protocol
Speed 20 MBit/sec TCP throughput, 30 MBit/sec UDP

ESP32-S2 Wi-Fi Throughput:https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-guides/wifi.html#esp32-s2-wi-fi-throughput

Other parameters: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#esp32-wi-fi-feature-list

ESP32-S2 Wi-Fi performance (verification and supplement required)

project parameter
Mode Station, AP, Coexistence
Protocol IEEE-802.11B, IEEE-802.11G, IEEE802.11N support software switching
Safety WPA/WPA2/WPA2-Enterprise and WPS
Keyfeature AMPDU, HT40, QoS
Distance 1 km with Espressif-specific protocol
Speed 20 MBit/sec TCP throughput, 30 MBit/sec UDP

Other parameters: https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-guides/wifi.html#esp32-s2-wi-fi-feature-list

2. Wi-Fi programming framework

Wi-Fi Programming Model

  1. The application layer calls Wi-Fi driver APIs to initialize Wi-Fi.
  2. Wi-Fi can be seen as a black box that works independently. When an event occurs, it is released to the default event loop . Applications can write programs and register according to their needs .
    eventhandle
  3. The network interface component esp_netif provides a series of handleprograms, which are associated with the WiFi driver by eventdefault. For example, ESP32 is used as an AP. When a user accesses, esp_netif will automatically start the DHCP service.

Note: Before Wi-Fi initialization, you should use the WIFI_INIT_CONFIG_DEFAULTGet Initialization Configuration Structure to personalize the structure, and then perform the initialization work. Prevent problems caused by uninitialized structure members, especially when IDF updates add new structure members.

2.1 General use process

Sample Wi-Fi Event Scenarios in Station Mode

Sample Wi-Fi Event Scenarios in Station Mode

Sample Wi-Fi Event Scenarios in AP Mode

Sample Wi-Fi Event Scenarios in AP Mode

2.2 menuconfig configurable items

  1. Modify Wi-Fi Buffer to save space After
    Wi-Fi is initialized, a part of the memory space will be statically allocated. If the user is more sensitive to memory, you can Static RX Buffersave up to 6400 Bytes space by reducing it , and by Static TX Buffermodifying it to Dynamic TX Buffersave (16 * 1600-32) Bytes left and right space. For details, please refer to: Wi-Fi Buffer Configure

  2. Keep WiFi configuration information in NVS
    By enabling Wi-Fi NVS Flash, the Wi-Fi driver will save the configuration information in NVS, and the WiFi configuration phase can be skipped next time you start it. Users can also turn off this option and save the configuration information by themselves.

  3. Enable AMPDU to improve network throughput
    AMPDU is enabled by default. In the 802.11n standard, the A-MPDU aggregation frame format is adopted, that is, multiple MPDUs are aggregated into one A-MPDU, only one PHY header is retained, and the PHY headers of the remaining MPDUs are deleted, which reduces the additional information of the PHY header of each MPDU. It also reduces the number of ACK frames, thereby reducing the protocol load, and effectively improving network throughput.

The 802.11n protocol defines a new MAC feature A-MSDU. This feature enables multiple MSDUs to be combined into one MSDU for transmission. Similar to A-MPDU, through aggregation, A-MSDU reduces the transmission of the MAC header of each MSDU. Additional information improves the transmission efficiency of the MAC layer. 802.11n supports optimization at the physical layer and provides short interval functions. The original 11a/g GI duration is 800us, while the short interval Short GI duration is 400us. When Short GI is used, the rate can be increased by 10%.ESP32-S2 supports receiving AMSDU but doesn’t support transmitting AMSDU. The transmitting AMSDU is not necessary since ESP32-S2 has transmitting AMPDU.

3. Wi-Fi debugging

3.1 Improve signal quality

  1. Modifying the default HT40 to a fixed HT20 can reduce external interference, but the theoretical transmission rate drops from 150Mbps to 72Mbps (raw PHY data rate).

3.2 Wireshark network analysis tool

Espressif Wireshark User Guide

3.3 Error code monitoring

Error codes are defined in the esp_err.hfile

ESP-IDF provides a default error code monitoring macros ESP_ERROR_CHECKcan be used to monitor the location and the error message, but it is recommended to monitor user-defined macros to automate processing error messages.

Error type Approach
ESP_OK Next step

4. Differences in Wi-Fi Library of Different Versions

4.1 Event naming is different

All event names have changed, mainly because the SYSTEM_EVENT_XXX_YYYcorresponding changes have been changed to WIFI_EVENT_XXX_YYYorIP_EVENT_XXX_YYY

v3.3 v4.0 + Remarks
SYSTEM_EVENT_WIFI_READY WIFI_EVENT_WIFI_READY This event will not be triggered in v4.0
SYSTEM_EVENT_SCAN_DONE WIFI_EVENT_SCAN_DONE
SYSTEM_EVENT_XXX_YYY WIFI_EVENT_XXX_YYY
SYSTEM_EVENT_STA_GOT_IP IP_EVENT_STA_GOT_IP
SYSTEM_EVENT_AP_STA_GOT_IP6 IP_EVENT_GOT_IP6
SYSTEM_EVENT_STA_LOST_IP IP_EVENT_STA_LOST_IP Is the official document wrong?

4.2 Different function interface definitions

v3.3 v4.0 + Remarks
tcpip_adapter_init() esp_netif_init() Create and initialize LwIP thread
esp_event_loop_init() esp_event_loop_init() Create system event loop thread, register event callback function
no esp_netif_create_default_wifi_ap(), esp_netif_create_default_wifi_sta() Create a default network interface and bind it to the TCP/IP protocol stack
esp_wifi_init() esp_wifi_init() Create and initialize Wi-Fi driver thread

Five, matters needing attention

  1. The initialization process is not fixed, but the General Scenario should be followed as much as possible .
  2. The WiFi configuration work should be completed before the WiFi connection as much as possible. If the WiFi is configured in the middle, it will trigger the WiFi reconnection.
  3. The process of obtaining IP in WiFi station mode will take some time. Please do not IP_EVENT_STA_GOT_IPstart socket related work before triggering.
  4. If the AP is closed or the RSSI is weak, it WIFI_EVENT_STA_DISCONNECTEDwill be triggered, then the LwIP thread will disconnect and clear the UDP/TCP connection, and then the socket related functions will return an error code. Pay attention to the error monitoring .
  5. If the IP address changes, IP_EVENT_STA_GOT_IPwill be triggered at the same time ip_changeit is true, if you receive such an event, you need to close all sockets have been created, re-created .
  6. Long-distance mode requires both ends to support Espressif 802.11LR. Compared with 802.11 B, this mode has a 4 dB gain increase. In principle, the transmission distance is 2-2.5 times that of 802.11 B, but the throughput of this mode is very limited to 1/2 Mbits

Guess you like

Origin blog.csdn.net/qq_20515461/article/details/105711981