ESP32 WIFI功能支持STA、AP、APSTA三种模式,本文主要讲讲在STA模式下,连接到电脑或手机的移动热点(详细代码请点击下载,代码在ESP32S3上测试通过):
1、创建wifi事件组
s_wifi_event_group = xEventGroupCreate();
2、Wi-Fi/LwIP 初始化,创建LWIP核心任务
ESP_ERROR_CHECK(esp_netif_init());
3、创建系统事件任务,并初始化应用程序事件的回调函数
ESP_ERROR_CHECK(esp_event_loop_create_default());
4、创建有 TCP/IP 堆栈的默认网络接口实例绑定 station
esp_netif_create_default_wifi_sta();
5、由于wifi连接选项设置了使用nvs,会把每次配置的参数存储在nvs中。因此需要设置nvs分区
nvs_flash_init();
6、创建wifi驱动程序任务,并初始化wifi驱动程序
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
7、注册用于处理wifi连接的过程中的事件
esp_event_handler_instance_t instance_any_id; // 用于处理wifi连接时候的事件的句柄
esp_event_handler_instance_t instance_got_ip; // 用于处理ip分配时候产生的事件的句柄
// 该句柄对wifi连接所有事件都产生响应,连接到event_handler回调函数
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
// 该句柄仅仅处理IP_EVENT事件组中的从AP中获取ip地址事件,连接到event_handler回调函数
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
8、WIFI配置
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK, // 设置快速扫描模式下能接受的最弱的验证模式
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH, // 设置SAE和PWE(wifi协议)的配置
},
};
// 2 配置station工作模式
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
// 3 配置
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
9、启动WIFI
ESP_ERROR_CHECK(esp_wifi_start() ); // 会触发回调函数
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
/******************** 输出wifi连接结果 ********************/
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
10、在app_main函数中调用wifi初始化函数
wifi_init();
11、将程序烧录至ESP32芯片
12、启动芯片,热点显示连接结果
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3820,len:0x10a0
load:0x403c9700,len:0x4
load:0x403c9704,len:0xc80
load:0x403cc700,len:0x31e4
entry 0x403c9920
Hello world!
This is esp32s3 chip with 2 CPU core(s), WiFi/BLE, silicon revision v0.2, 16MB external flash
W (439) image wifi station: wifi init...
W (481) image wifi station: wifi_init_sta finished.
W (1502) image wifi station: got ip:192.168.137.124
free heap size: 7806020 bytes, minimum free heap size: 7803416 bytes
internal free heap size: 161135 bytes
完整代码请点击下载