赞
踩
Wi-Fi 库支持配置及监控 ESP32 Wi-Fi 连网功能。
支持配置:
基站模式(即 STA 模式或 Wi-Fi 客户端模式),此时 ESP32 连接到接入点 (AP)。
AP 模式(即 Soft-AP 模式或接入点模式),此时基站连接到 ESP32。
AP-STA 共存模式(ESP32 既是接入点,同时又作为基站连接到另外一个接入点)。
上述模式的各种安全模式(WPA、WPA2 及 WEP 等)。
扫描接入点(包括主动扫描及被动扫描)。
使用混杂模式监控 IEEE802.11 Wi-Fi 数据包。
以下 WIFI 接口位于 esp_wifi/include/esp_wifi.h。
如上图中 1.1\1.2\1.3\1.4 所示,分别
ESP_ERROR_CHECK(esp_netif_init());
esp_event_handler_register()
。esp_netif组件还处理Wi-Fi事件,以提供一组默认行为。例如,当Wi-Fi站连接到AP时,esp_netif将自动启动DHCP客户端(默认情况下)。// 创建系统事件任务并初始化应用程序事件的回调函数。
ESP_ERROR_CHECK(esp_event_loop_create_default());
// 创建具有TCP / IP堆栈的默认网络接口实例绑定AP。
esp_netif_create_default_wifi_ap();
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), .channel = EXAMPLE_ESP_WIFI_CHANNEL, .password = EXAMPLE_ESP_WIFI_PASS, .max_connection = EXAMPLE_MAX_STA_CONN, .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
当有终端接入或断开连接时,产生 WIFI_EVENT_AP_STACONNECTED
或 WIFI_EVENT_AP_STADISCONNECTED
事件。
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}
/* WiFi softAP Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "lwip/err.h" #include "lwip/sys.h" /* The examples use WiFi configuration that you can set via project configuration menu. If you'd rather not, just change the below entries to strings with the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" */ #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD #define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL #define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN static const char *TAG = "wifi softAP"; static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_id == WIFI_EVENT_AP_STACONNECTED) { wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid); } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid); } } void wifi_init_softap(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL)); wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), .channel = EXAMPLE_ESP_WIFI_CHANNEL, .password = EXAMPLE_ESP_WIFI_PASS, .max_connection = EXAMPLE_MAX_STA_CONN, .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL); } void app_main(void) { //Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); wifi_init_softap(); }
如上图中 1.1\1.2\1.3\1.4 所示,分别
ESP_ERROR_CHECK(esp_netif_init());
esp_event_handler_register()
。esp_netif组件还处理Wi-Fi事件,以提供一组默认行为。例如,当Wi-Fi站连接到AP时,esp_netif将自动启动DHCP客户端(默认情况下)。// 创建系统事件任务并初始化应用程序事件的回调函数。 ESP_ERROR_CHECK(esp_event_loop_create_default()); // 创建具有TCP / IP堆栈的默认网络接口实例绑定基站。 esp_netif_create_default_wifi_sta(); esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, instance_got_ip));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
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, .pmf_cfg = { .capable = true, .required = false }, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
调用esp_wifi_start()
以启动Wi-Fi驱动程序。
Wi-Fi驱动程序将WIFI_EVENT_STA_START发布到事件任务;然后,事件任务将执行一些常规操作,并将调用应用程序事件回调函数。
应用程序事件回调函数将WIFI_EVENT_STA_START中继到应用程序任务。此时调用esp_wifi_connect()
。
ESP_ERROR_CHECK(esp_wifi_start());
一旦esp_wifi_connect()
被调用,Wi-Fi驱动程序将开始内部扫描/连接过程。
如果内部扫描/连接过程成功,将生成WIFI_EVENT_STA_CONNECTED。在事件任务中,它将启动DHCP客户端,该客户端最终将触发DHCP进程。
由于例如密码错误,找不到AP等原因,Wi-Fi连接可能会失败。在这种情况下,会出现WIFI_EVENT_STA_DISCONNECTED并提供这种失败的原因。
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); s_retry_num++; ESP_LOGI(TAG, "retry to connect to the AP"); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } }
初始化DHCP客户端后,将开始IP阶段。如果从DHCP服务器成功接收到IP地址,则将出现IP_EVENT_STA_GOT_IP,并且事件任务将执行常规处理。
在应用程序事件回调中,IP_EVENT_STA_GOT_IP被中继到应用程序任务。对于基于LwIP的应用程序,此事件非常特殊,这意味着该应用程序已准备就绪,可以开始其任务,例如创建TCP / UDP套接字等。一个非常常见的错误是在收到IP_EVENT_STA_GOT_IP之前初始化套接字。接收IP之前,请勿开始与套接字相关的工作。
当前,esp_wifi_scan_start()
仅在Station或Station + AP模式下支持该API。
扫描所有频道后,将出现WIFI_EVENT_SCAN_DONE。
应用程序的事件回调函数通知应用程序任务已接收到WIFI_EVENT_SCAN_DONE。esp_wifi_scan_get_ap_num()
调用以获取在此扫描中找到的AP的数量。然后,它分配足够的条目和调用esp_wifi_scan_get_ap_records()
以获取AP记录。请注意,一旦esp_wifi_scan_get_ap_records()
被调用,Wi-Fi驱动程序中的AP记录将被释放。esp_wifi_scan_get_ap_records()
一次扫描完成事件请勿调用两次。如果esp_wifi_scan_get_ap_records()
在发生扫描完成事件时未调用,则不会释放由Wi-Fi驱动程序分配的AP记录。因此,请确保调用esp_wifi_scan_get_ap_records()
,但只能调用一次。
/* Scan Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* This example shows how to scan for available set of APs. */ #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_log.h" #include "esp_event.h" #include "nvs_flash.h" #define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE static const char *TAG = "scan"; static void print_auth_mode(int authmode) { switch (authmode) { case WIFI_AUTH_OPEN: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN"); break; case WIFI_AUTH_WEP: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WEP"); break; case WIFI_AUTH_WPA_PSK: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_PSK"); break; case WIFI_AUTH_WPA2_PSK: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_PSK"); break; case WIFI_AUTH_WPA_WPA2_PSK: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_WPA2_PSK"); break; case WIFI_AUTH_WPA2_ENTERPRISE: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_ENTERPRISE"); break; case WIFI_AUTH_WPA3_PSK: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA3_PSK"); break; case WIFI_AUTH_WPA2_WPA3_PSK: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_WPA3_PSK"); break; default: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN"); break; } } static void print_cipher_type(int pairwise_cipher, int group_cipher) { switch (pairwise_cipher) { case WIFI_CIPHER_TYPE_NONE: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_NONE"); break; case WIFI_CIPHER_TYPE_WEP40: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP40"); break; case WIFI_CIPHER_TYPE_WEP104: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP104"); break; case WIFI_CIPHER_TYPE_TKIP: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP"); break; case WIFI_CIPHER_TYPE_CCMP: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_CCMP"); break; case WIFI_CIPHER_TYPE_TKIP_CCMP: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP"); break; default: ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_UNKNOWN"); break; } switch (group_cipher) { case WIFI_CIPHER_TYPE_NONE: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_NONE"); break; case WIFI_CIPHER_TYPE_WEP40: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP40"); break; case WIFI_CIPHER_TYPE_WEP104: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP104"); break; case WIFI_CIPHER_TYPE_TKIP: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP"); break; case WIFI_CIPHER_TYPE_CCMP: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_CCMP"); break; case WIFI_CIPHER_TYPE_TKIP_CCMP: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP"); break; default: ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN"); break; } } /* Initialize Wi-Fi as sta and set scan method */ static void wifi_scan(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); uint16_t number = DEFAULT_SCAN_LIST_SIZE; wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE]; uint16_t ap_count = 0; memset(ap_info, 0, sizeof(ap_info)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info)); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); ESP_LOGI(TAG, "Total APs scanned = %u", ap_count); for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) { ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid); ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi); print_auth_mode(ap_info[i].authmode); if (ap_info[i].authmode != WIFI_AUTH_WEP) { print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher); } ESP_LOGI(TAG, "Channel \t\t%d\n", ap_info[i].primary); } } void app_main(void) { // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK( ret ); wifi_scan(); }
• 由 Leung 写于 2021 年 4 月 19 日
• 参考:Wi-Fi驱动程序
ESP32 开发笔记(三)源码示例 19_WIFI_STA 创建STA站模式连接路由器
ESP32 开发笔记(三)源码示例 15_WIFI_AP 创建软AP示例
ESP32 开发笔记(三)源码示例 14_WIFI_Scan 附近WIFI信号扫描示例
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。