ESP32 Application Note #02 - How to connect to WiFi AP

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.

Buy ESP32 Modules & Boards on AnalogLamb

ALB32-WROVER - 64Mb Flash, 32Mb PSRAM ESP-WROOM-32 Module

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.
ESP32 wifi programming model
esp32 wifi programming model
ESP32 wifi programming model

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
wifi_config_t

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.

Function esp_wifi_set_config()

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


Function esp_wifi_start()

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.

Function esp_wifi_connection()

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;

Function tcpip_adapter_get_ip_info()

It gets the ip information.

The Example Code

Github
esp32 wifi connection diagram
esp32 wifi connection diagram

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 ).

esp32 wifi connection
esp32 wifi connection example

Buy ESP32 Modules & Boards on AnalogLamb

ALB32-WROVER - 64Mb Flash, 32Mb PSRAM ESP-WROOM-32 Module

Comments

Popular posts from this blog

Polos CH32VXX RISC-V Breakout Boards

Amnos CH347T Breakout

ALB32-WROVER - 64Mb Flash , 32Mb PSRAM ESP32-WROOM Module