Chapter 4. API Guides
Setting Wi-Fi Compile-time Options
Refer to Wi-Fi Menuconfig.
Init Wi-Fi
Refer to ESP32-S2 Wi-Fi Station General Scenario, ESP32-S2 Wi-Fi AP General Scenario.
Start/Connect Wi-Fi
Refer to ESP32-S2 Wi-Fi Station General Scenario, ESP32-S2 Wi-Fi AP General Scenario.
Event-Handling
Generally, it is easy to write code in “sunny-day”scenarios, such as WIFI_EVENT_STA_START,
WIFI_EVENT_STA_CONNECTED etc. The hard part is to write routines in “rainy-day”scenarios, such as
WIFI_EVENT_STA_DISCONNECTED etc. Good handling of“rainy-day”scenarios is fundamental to robust Wi-Fi
applications. Refer to ESP32-S2 Wi-Fi Event Description, ESP32-S2 Wi-Fi Station General Scenario, ESP32-S2 Wi-Fi
AP General Scenario. See also an overview of event handling 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-S2 Wi-Fi API Error Code.
4.33.3 ESP32-S2 Wi-Fi API Error Code
All of the ESP32-S2 Wi-Fi APIs have well-defined return values, namely, the error code. The error code can be
categorized into:
• No errors, e.g. ESP_OK means that the API returns successfully.
• Recoverable errors, such as ESP_ERR_NO_MEM, etc.
• 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() returns ESP_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, critical errors, in which case “assert”may be a good method for error handling. For
example, if esp_wifi_set_mode() returns ESP_ERR_WIFI_NOT_INIT, it means that the Wi-Fi driver
is not initialized by esp_wifi_init() successfully. You can detect this kind of error very quickly in the
application development phase.
In 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, we strongly
recommend that API users write their own error-handling code.
Espressif Systems 1514
Submit Document Feedback
Release v4.4