FluxDC/components/FluxWifi/FluxWifi.c

260 lines
8.4 KiB
C
Raw Normal View History

2025-03-10 18:07:34 +08:00
#include <stdio.h>
#include "FluxWifi.h"
#include "ui.h"
2025-03-10 18:07:34 +08:00
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
2025-03-15 09:37:07 +08:00
int number = 0, ap_count = 0;
2025-03-10 18:07:34 +08:00
static const char* TAG = "wifi_scan";
/**
2025-03-15 09:37:07 +08:00
* @brief Initialize Wi-Fi as STA and set up the scanning method.
* @param None
* @retval None
2025-03-10 18:07:34 +08:00
*/
void wifi_scanInit(void)
2025-03-10 18:07:34 +08:00
{
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();
2025-03-15 09:37:07 +08:00
/* Wi-Fi configuration initialization */
2025-03-10 18:07:34 +08:00
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
memset(ap_info, 0, sizeof(ap_info));
2025-03-15 09:37:07 +08:00
/* Set Wi-Fi to STA mode */
2025-03-10 18:07:34 +08:00
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
2025-03-15 09:37:07 +08:00
/* Register Wi-Fi response events */
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;
2025-03-10 18:07:34 +08:00
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");
2025-03-15 09:37:07 +08:00
lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "%s", "WIFI-Disconnected");
2025-03-15 09:37:07 +08:00
lv_obj_clear_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED);
2025-03-12 18:44:46 +08:00
} 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));
2025-03-15 09:37:07 +08:00
lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "IP:"IPSTR, IP2STR(&event->ip_info.ip));
2025-03-15 09:37:07 +08:00
lv_obj_add_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED);
}
}
void wifi_scan(void)
{
2025-03-15 09:37:07 +08:00
/* Start Wi-Fi */
ESP_ERROR_CHECK(esp_wifi_start());
vTaskDelay(50 / portTICK_PERIOD_MS);
2025-03-15 09:37:07 +08:00
/* Start scanning nearby Wi-Fi networks and wait for the scan to finish */
2025-03-10 18:07:34 +08:00
esp_wifi_scan_start(NULL, true);
2025-03-15 09:37:07 +08:00
/* Get the scan results */
2025-03-10 18:07:34 +08:00
get_wifi_scan_result();
2025-03-15 09:37:07 +08:00
/* Stop scanning */
esp_wifi_scan_stop();
esp_wifi_stop();
2025-03-10 18:07:34 +08:00
return;
}
/**
2025-03-15 09:37:07 +08:00
* @brief Get Wi-Fi scan results.
* @param None
* @retval None
2025-03-10 18:07:34 +08:00
*/
static void get_wifi_scan_result(void)
{
number = DEFAULT_SCAN_LIST_SIZE;
ap_count = 0;
2025-03-15 09:37:07 +08:00
/* Get the number of APs found in the last scan */
2025-03-10 18:07:34 +08:00
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
2025-03-15 09:37:07 +08:00
/* Get the list of APs found in the last scan */
2025-03-10 18:07:34 +08:00
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
2025-03-15 09:37:07 +08:00
/* Print Wi-Fi scan results */
2025-03-10 18:07:34 +08:00
print_wifi_scan_result(ap_count, number);
return;
}
/**
2025-03-15 09:37:07 +08:00
* @brief Authentication mode.
* @param authmode : Authentication mode
* @retval None
2025-03-10 18:07:34 +08:00
*/
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;
}
}
/**
2025-03-15 09:37:07 +08:00
* @brief Print Wi-Fi password type.
* @param pairwise_cipher : Password type
* @param group_cipher : Group password type
* @retval None
2025-03-10 18:07:34 +08:00
*/
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;
}
}
/**
2025-03-15 09:37:07 +08:00
* @brief Print Wi-Fi scan results.
* @param ap_count : Actual number of APs scanned
* @param number : Number of AP information that can be printed
* @retval None
2025-03-10 18:07:34 +08:00
*/
static void print_wifi_scan_result(uint16_t ap_count, uint16_t number)
{
2025-03-15 09:37:07 +08:00
/* Add scan results to the dropDown list */
lv_dropdown_clear_options(ui_pageWifiConnect_dropdownWifiName);
2025-03-15 09:37:07 +08:00
/* Print nearby Wi-Fi information */
2025-03-10 18:07:34 +08:00
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);
2025-03-10 18:07:34 +08:00
ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
lv_dropdown_add_option(ui_pageWifiConnect_dropdownWifiName, (char*)ap_info[i].ssid, i);
2025-03-15 09:37:07 +08:00
/* Encryption method */
2025-03-10 18:07:34 +08:00
print_auth_mode(ap_info[i].authmode);
2025-03-15 09:37:07 +08:00
/* Password type */
2025-03-10 18:07:34 +08:00
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;
2025-03-15 09:37:07 +08:00
}