Welcome to AnalogLamb WordPress Blog. Today I am glad to share ESP32 Application Notes with you. ESP32 is Espressif new product, which has WiFi and Bluetooth. It is very suitable for IoT Applications. We hope you can learn about more details of ESP32 from the series – ESP32 Application Note.
Let us begin ESP32 Application Note #02 – How to connect to WiFi AP.
Overview of ESP32 WiFi Programming Model.
Event and Event Handler is a very import concept. Now I would like to explain the scheme of ESP32 WiFi Programming. So that we can understand it clearly. Let us view the Diagram as below.
1), The WiFi driver can be consider as black box, it knows nothing about the high layer code, such as TCPIP stack, application task, event task etc, all it can do is to receive API call from high layer or post event queue to a specified Queue, which is initialized by API esp_wifi_init().
2), The event task is a daemon task, which receives events from WiFi driver or from other subsystem, such as TCPIP stack, event task will call the default callback function on receiving the event. For example,on receiving event SYSTEM_EVENT_STA_CONNECTED, it will call tcpip_adapter_start() to start the DHCP client in it’s default handler.
3), Application can register it’s own event callback function by API esp_event_init, then the application callback function will be called after the default callback. Also, if application doesn’t want to execute the callback in the event task, what it needs to do is to post the related event to application task in the application callback function.
4), The application task (code) generally mixes all these thing together, it calls APIs to init the system/WiFi and handle the events when necessary.
Functions and Data Structure
Data Structure wifi_config_t
wifi_config_t is defined in esp_wifi.h . The details, please check here or as below pic
wifi_config_t is an union, it includes two struct: wifi_ap_config_t and wifi_sta_config_t. The two structs are used to config AP and STA.
The function set configuration of ESP32 STA or AP. It is defined in esp_wifi.c file. The details, please check here.
There are two parameters: wifi_interface_t ifx – WIFI_IF_STA( station interface) and WIFI_IF_AP( ap interface ). But in esp_wifi_types.h, They are also defined as:
#define WIFI_IF_STA ESP_IF_WIFI_STA
#define WIFI_IF_AP ESP_IF_WIFI_AP
Start WiFi according to current configuration. If mode is WIFI_MODE_STA, it create station control block and start station. If mode is WIFI_MODE_AP, it create soft-AP control block and start soft-AP. If mode is WIFI_MODE_APSTA, it create soft-AP and station control block and start soft-AP and station.
Connect the ESP32 WiFi station to the AP. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode. If the ESP32 is connected to an AP, call esp_wifi_disconnect to disconnect.
Data Structure tcpip_adatper_ip_info_t
It defines ip information, included ip, netmask, gateway;
It gets the ip information.
The Example Code
The app main function configure the wifi mode( esp_wifi_set_mode() ), wifi prarmeters storage( esp_wifi_set_storage() ), and event handler( esp_event_loop_init() ), start wifi( esp_wifi_start() ) and set wifi station interface: SSID and Password ( esp_wifi_set_config() ).
After start wifi, an event( SYSTEM_EVENT_STA_START) is occured, then connect to wifi ap( esp_wifi_connection() ). After connection, an event( SYSTEM_EVENT_GOT_IP ) occured, to get ip information( tcpip_adapter_get_ip_info() ). Use esp_wifi_disconnection() to disconnect to wifi ap( SYSTEM_EVENT_STA_DISCONNECTION ).