第五十章 WiFi热点实验
本章节实验作者把ESP32-S3配置为AP模式,即创建连接热点,读者可使用手机连接该热点。AP模式相关知识请读者查看第二章节的内容。 本章分为如下几个小节: 50.1 硬件设计 50.2 软件设计 50.3 下载验证
50.1 硬件设计 1.例程功能 本章实验功能简介:当手机连接这个热点时,LCD显示该连接设备的MAC地址,断开时,LCD显示断开设备的MAC地址。 2. 硬件资源 1)LED灯 LED-IO1 2)XL9555 IIC_INT-IO0(需在P5连接IO0) IIC_SDA-IO41 IIC_SCL-IO42 3)SPILCD CS-IO21 SCK-IO12 SDA-IO11 DC-IO40(在P5端口,使用跳线帽将IO_SET和LCD_DC相连) PWR- IO1_3(XL9555) RST- IO1_2(XL9555) 4)ESP32-S3内部WiFi 3. 原理图 本章实验使用的WiFi为ESP32-S3的片上资源,因此并没有相应的连接原理图。 50.2 软件设计 50.2.1 程序流程图 程序流程图能帮助我们更好的理解一个工程的功能和实现的过程,对学习和设计工程有很好的主导作用。下面看看本实验的程序流程图:
图50.2.1.1 程序流程图 50.2.2 程序解析 在本章节实验中,我们只关心main.c文件内容即可,该文件内容如下: i2c_obj_t i2c0_master; static const char *TAG = "AP"; #define EXAMPLE_ESP_WIFI_SSID "123" #define EXAMPLE_ESP_WIFI_PASS "123456789" #define EXAMPLE_MAX_STA_CONN 5 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" static char lcd_buff[100] = {0}; /** * @param event_base:WIFI事件 * @param event_id:事件ID * @param event_data:事件数据 * @retval 无 */ 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) { lcd_fill(0,90,320,240,WHITE); 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); sprintf(lcd_buff, "MACSTR:"MACSTR,MAC2STR(event->mac)); lcd_show_string(0, 90, 320, 16, 16, lcd_buff, BLUE); lcd_show_string(0, 110, 320, 16, 16, "With device connection", BLUE); } /* 设备断开 */ 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); lcd_fill(0,90,320,320,WHITE); sprintf(lcd_buff, "Device disconnected:"MACSTR,MAC2STR(event->mac)); lcd_show_string(0, 90, 320, 16, 16, lcd_buff, BLUE); } } /** * @brief WIFI初始化 * @param 无 * @retval 无 */ static void wifi_init_softap(void) { /* 初始化网卡 */ ESP_ERROR_CHECK(esp_netif_init()); /* 创建新的事件循环 */ ESP_ERROR_CHECK(esp_event_loop_create_default()); /* 使用默认配置初始化包括netif的Wi-Fi */ 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_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); /* 配置WIFI */ wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), .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_netif_ip_info_t ip_info; /* 获取当前设备的IP地址 */ esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ip_info); char ip_addr[16]; inet_ntoa_r(ip_info.ip.addr, ip_addr, 16); ESP_LOGI(TAG, "Set up softAP with IP: %s", ip_addr); ESP_LOGI(TAG, "wifi_init_softap finished. SSID:'%s' password:'%s'", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); lcd_show_string(0, 90, 240, 16, 16, "wifi connecting......", BLUE); } /** * @brief 程序入口 * @param 无 * @retval 无 */ void app_main(void) { /* 省略部分代码..... */ wifi_init_softap(); while (1) { LED_TOGGLE(); vTaskDelay(500); } } 上述源码相对简单,主要将ESP32-S3设备配置为AP模式,即作为热点设备。然后,设置热点设备的账号、密码、安全模式等参数。在WiFi事件回调函数中,当有外部设备请求连接时,程序会在LCD上显示连接设备的MAC地址等信息。而当外部设备从连接状态断开时,LCD会显示当前断开的外部设备MAC地址。 50.3 下载验证 程序下载成功后,我们利用手机连接ESP32-S3热点设备,当手机连接热点设备成功时,LCD显示手机的MAC地址等信息,当手机从已连接状态断开时,LCD显示断开的外部设备的MAC地址。下图为连接成功的LCD显示效果图。
图50.3.1 外部设备连接热点设备 下图为外部设备从已连接状态断开效果图,如下所示。
图50.3.2 外部设备断开热点设备
|