#include #include "FluxWifi.h" #include "ui.h" wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE]; int number = 0,ap_count=0; static const char* TAG = "wifi_scan"; /** * @brief 将Wi-Fi初始化为sta并设置扫描方法 * @param 无 * @retval 无 */ void wifi_scanInit(void) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); vTaskDelay(50 / portTICK_PERIOD_MS); esp_netif_create_default_wifi_sta(); /* wifi配置初始化 */ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); memset(ap_info, 0, sizeof(ap_info)); /* 设置WIFI为STA模式 */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); /*注册WiFi响应事件*/ 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)); return; wifi_config_t wifi_config = { .sta = { .ssid = "yuwell", .password = "yuwell123456", /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8). * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. */ .threshold.authmode = WIFI_AUTH_WPA_PSK, .sae_pwe_h2e = WPA3_SAE_PWE_BOTH, .sae_h2e_identifier = "", }, }; } 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) { } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { ESP_LOGI(TAG, "Disconnected from Wi-Fi"); lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "%s","WIFI-Disconnected"); } 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)); lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "IP:"IPSTR,IP2STR(&event->ip_info.ip)); } } void wifi_scan(void) { /* 启动WIFI */ ESP_ERROR_CHECK(esp_wifi_start()); vTaskDelay(50 / portTICK_PERIOD_MS); /* 开始扫描附件的WIFI, 并等待扫描结束 */ esp_wifi_scan_start(NULL, true); /* 获取扫描结果 */ get_wifi_scan_result(); /* 停止扫描 */ esp_wifi_scan_stop(); esp_wifi_stop(); return; } /** * @brief 获取Wi-Fi扫描结果 * @param 无 * @retval 无 */ static void get_wifi_scan_result(void) { number = DEFAULT_SCAN_LIST_SIZE; ap_count = 0; /* 获取上次扫描中找到的AP数量 */ ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); /* 获取上次扫描中找到的AP列表 */ ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info)); /* 打印WIFI扫描结果 */ print_wifi_scan_result(ap_count, number); return; } /** * @brief 身份认证模式 * @param authmode :身份验证模式 * @retval 无 */ static void print_auth_mode(int authmode) { switch (authmode) { case WIFI_AUTH_OPEN: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN"); break; case WIFI_AUTH_OWE: ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OWE"); 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; } } /** * @brief 打印WIFI密码类型 * @param pairwise_cipher :密码类型 * @param group_cipher :群密码类型 * @retval 无 */ 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; } } /** * @brief 打印Wi-Fi扫描结果 * @param ap_count :实际扫描出的AP数量 * @param number :可打印输出的AP信息数量 * @retval 无 */ static void print_wifi_scan_result(uint16_t ap_count, uint16_t number) { /* 将扫描结果添加到 dropDown 列表 */ lv_dropdown_clear_options(ui_pageWifiConnect_dropdownWifiName); /* 打印附件的WIFI信息 */ ESP_LOGI(TAG, "wifi scan result, ap_count = %u, number = %u", ap_count, number); 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); lv_dropdown_add_option(ui_pageWifiConnect_dropdownWifiName, (char*)ap_info[i].ssid, i); /* 加密方式 */ 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); } return; }