我是根据\esp\esp-idf\examples\wifi\smart_config例程来编写的智能配网,如果输入密码是错的,程序也不会返回信息给手机APP,要怎么做才能判断智能配网连接是否失败?这是我的一部分代码
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
static int retry_num = 0; /* 记录wifi重连次数 */
esp_err_t err;
/* 系统事件为WiFi事件 */
if (event_base == WIFI_EVENT)
{
if(event_id == WIFI_EVENT_STA_START) /* 事件id为STA开始 */
{
err= esp_wifi_connect();
printf("err = %d\r\n",err);
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED) /* 事件id为失去STA连接 */
{
gpio_set_level(led, 1);
err=esp_wifi_connect();
printf("err111111 = %d\r\n",err);
retry_num++;
printf("retry to connect to the AP %d times. \n",retry_num);
if (retry_num > 5) /* WiFi重连次数大于10 */
{
/* 将WiFi连接事件标志组的WiFi连接失败事件位置1 */
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
/* 清除WiFi连接成功标志位 */
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
}
}
/* 系统事件为ip地址事件,且事件id为成功获取ip地址 */
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; /* 获取IP地址信息*/
printf("got ip:%d.%d.%d.%d \n" , IP2STR(&event->ip_info.ip)); /* 打印ip地址*/
retry_num = 0; /* WiFi重连次数清零 */
/* 将WiFi连接事件标志组的WiFi连接成功事件位置1 */
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
}
/* 系统事件为智能配网事件 */
else if (event_base == SC_EVENT)
{
led_mode = Connect_led;
if(event_id == SC_EVENT_SCAN_DONE ) /* 开始扫描智能配网设备端 */
printf("Scan done\n");
else if(event_id == SC_EVENT_FOUND_CHANNEL) /* 得到了智能配网通道 */
printf("Found channel \n");
else if(event_id == SC_EVENT_GOT_SSID_PSWD) /* 得到了智能配网设备提供的ssid和password */
{
printf("smartconfig got SSID and password\n");
/* 获取智能配网设备端提供的数据信息 */
smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
/* 定义WiFi配置结构体和用了结收ssid和password的数组 */
wifi_config_t wifi_config;
char ssid[32] = { 0 };
char password[64] = { 0 };
bzero(&wifi_config, sizeof(wifi_config_t)); /* 将结构体数据清零 */
/* 将智能配网设备发送来的WiFi的ssid、password及MAC地址复制到wifi_config */
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
wifi_config.sta.bssid_set = evt->bssid_set;
if (wifi_config.sta.bssid_set == true) {
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
}
/* 打印WiFi名称和密码 */
memcpy(ssid, evt->ssid, sizeof(evt->ssid));
memcpy(password, evt->password, sizeof(evt->password));
printf("SSID:%s \n", ssid);
printf("PASSWORD:%s \n", password);
/* 将得到的WiFi名称和密码存入NVS*/
nvs_handle wificfg_nvs_handler;
ESP_ERROR_CHECK( nvs_open("WiFi_cfg", NVS_READWRITE, &wificfg_nvs_handler) );
ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_ssid",ssid) );
ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_passwd",password) );
ESP_ERROR_CHECK( nvs_commit(wificfg_nvs_handler) ); /* 提交 */
nvs_close(wificfg_nvs_handler); /* 关闭 */
printf("smartconfig save wifi_cfg to NVS .\n");
/* 根据得到的WiFi名称和密码连接WiFi*/
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_connect() );
}
else if (event_id == SC_EVENT_SEND_ACK_DONE) /* 智能配网成功,已经给智能配网设备发送应答*/
{
xEventGroupSetBits(s_wifi_event_group, SMART_CONFIG_BIT);
}
}
}
在使用智能配网时,如果输入WiFi的密码是错的,为什么会反复尝试连接WiFi??
-
- Posts: 171
- Joined: Thu Sep 23, 2021 10:03 am
Re: 在使用智能配网时,如果输入WiFi的密码是错的,为什么会反复尝试连接WiFi??
可以通过 reason code 判断来不进行重连:
例如:
例如:
Code: Select all
else if (event_id == WIFI_EVENT_STA_DISCONNECTED) /* 事件id为失去STA连接 */
{
wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason);
/* Set code corresponding to the reason for disconnection */
switch (disconnected->reason) {
case WIFI_REASON_AUTH_EXPIRE:
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
case WIFI_REASON_AUTH_FAIL:
case WIFI_REASON_ASSOC_EXPIRE:
case WIFI_REASON_HANDSHAKE_TIMEOUT:
case WIFI_REASON_MIC_FAILURE:
ESP_LOGE(TAG, "STA Auth Error");
break;
case WIFI_REASON_NO_AP_FOUND:
ESP_LOGE(TAG, "STA AP Not found");
break;
default:
/* If none of the expected reasons,
* retry connecting to host SSID */
esp_wifi_connect();
}
Who is online
Users browsing this forum: No registered users and 55 guests