static void wifi_scan(void)
{
wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_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_wifi_scan_start(NULL, true);
ESP_LOGI(TAG, "Max AP number ap_info can hold = %u", number);
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, actual AP number ap_info holds = %u", ap_count, number);
// 创建一个包含所有 SSID 并以换行符分隔的字符串
for (int i = 0; i < number; i++)
{
ESP_LOGI(TAG, "SSID \t\t%s", ap_info.ssid);
ssid_list = (char *)ap_info.ssid;
lv_dropdown_add_option(ui_wifiDropdown, ssid_list, i);
}
}
static void nvs_init(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);
}
static void switch_cb(lv_event_t *e)
{
lv_obj_t *sw = lv_event_get_target(e);
if (lv_obj_has_state(sw, LV_STATE_CHECKED))
{
ESP_LOGI(TAG, "Wifi scan started");
wifiswitch_flag = true;
nvs_init();
wifi_scan();
// my_lv_timer = lv_timer_create(wifi_scan, 500, NULL);
}
else
{
ESP_LOGI(TAG, "Wifi scan stopped");
wifiswitch_flag = false;
lv_led_off(ui_wifiled);
esp_wifi_stop();
esp_wifi_deinit();
lv_dropdown_clear_options(ui_wifiDropdown);
lv_textarea_clear_selection(ui_passwordTextArea);
}
}
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
ESP_LOGI(TAG, "Entry WiFi connect event_handler");
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 < ESP_WIFI_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);
}
}
void wifi_connect_sta(const char *name, const char *password)
{
s_wifi_event_group = xEventGroupCreate();
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_LOGI(TAG, "Registering WiFi event handler...");
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_LOGI(TAG, "Registering IP event handler...");
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
// .ssid = EXAMPLE_ESP_WIFI_SSID,
// .password = EXAMPLE_ESP_WIFI_PASS,
.ssid = "",
.password = "",
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword 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 = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
.sae_pwe_h2e = ESP_WIFI_SAE_MODE,
.sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
},
};
strncpy((char *)wifi_config.sta.ssid, name, sizeof(wifi_config.sta.ssid));
strncpy((char *)wifi_config.sta.password, password, sizeof(wifi_config.sta.password));
ESP_LOGI(TAG, "ssid: %s", wifi_config.sta.ssid);
ESP_LOGI(TAG, "password: %s", wifi_config.sta.password);
// ESP_LOGI(TAG, "Setting WiFi mode...");
// ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_LOGI(TAG, "Set config ago ssid: %s", wifi_config.sta.ssid);
ESP_LOGI(TAG, "Set config ago password: %s", wifi_config.sta.password);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_LOGI(TAG, "Set config after ssid: %s", wifi_config.sta.ssid);
ESP_LOGI(TAG, "Set config after password: %s", wifi_config.sta.password);
// ESP_LOGI(TAG,"Starting 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) */
ESP_LOGI(TAG, "Waiting for WiFi connection...");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
pdMS_TO_TICKS(10000));
/* 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",
wifi_config.sta.ssid, wifi_config.sta.password);
lv_led_on(ui_wifiled);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
wifi_config.sta.ssid, wifi_config.sta.password);
}
else
{
ESP_LOGE(TAG, "Timeout waiting for connection");
}
}
static void wifi_linkbtn(lv_event_t *e)
{
char wifiname[32];
lv_dropdown_get_selected_str(ui_wifiDropdown, wifiname, sizeof(wifiname));
ESP_LOGI(TAG, "Wifi SSID is %s\n", wifiname);
const char *pwd = lv_textarea_get_text(ui_passwordTextArea);
ESP_LOGI(TAG, "Wifi password is %s \n", pwd);
ESP_ERROR_CHECK(esp_wifi_scan_stop());
ESP_LOGI(TAG,"Wifi stop scaned");
wifi_connect_sta(wifiname, pwd);
}
why does my program report a timeout when wifi is connected, I can get the correct ssid and password through log checkin
-
- Posts: 1
- Joined: Mon Jul 15, 2024 6:55 am
why does my program report a timeout when wifi is connected, I can get the correct ssid and password through log checkin
- Attachments
-
- ui_wifipage.c
- (13.52 KiB) Downloaded 26 times
Who is online
Users browsing this forum: Bing [Bot] and 72 guests