在 Wi-Fi 连接失败的部分添加以下代码:
ESP_LOGI(TAG,"reason: fail %d\n", event->event_info.disconnected.reason);
例如:
case SYSTEM_EVENT_STA_DISCONNECTED:
{
if (s_retry_num event_info.disconnected.reason);
break;
}
然后编译,烧录,打开串口监视器。如果 Wi-Fi此时会在终端查看到对应的错误原因,例如:
I (10654) wifi station: connect to the AP fail
I (10654) wifi station: reason: fail 201
附:Wi-Fi 连接失败的 reason 对照表:
typedef enum {
WIFI_REASON_UNSPECIFIED = 1,
WIFI_REASON_AUTH_EXPIRE = 2,
WIFI_REASON_AUTH_LEAVE = 3,
WIFI_REASON_ASSOC_EXPIRE = 4,
WIFI_REASON_ASSOC_TOOMANY = 5,
WIFI_REASON_NOT_AUTHED = 6,
WIFI_REASON_NOT_ASSOCED = 7,
WIFI_REASON_ASSOC_LEAVE = 8,
WIFI_REASON_ASSOC_NOT_AUTHED = 9,
WIFI_REASON_DISASSOC_PWRCAP_BAD = 10,
WIFI_REASON_DISASSOC_SUPCHAN_BAD = 11,
WIFI_REASON_IE_INVALID = 13,
WIFI_REASON_MIC_FAILURE = 14,
WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT = 15,
WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT = 16,
WIFI_REASON_IE_IN_4WAY_DIFFERS = 17,
WIFI_REASON_GROUP_CIPHER_INVALID = 18,
WIFI_REASON_PAIRWISE_CIPHER_INVALID = 19,
WIFI_REASON_AKMP_INVALID = 20,
WIFI_REASON_UNSUPP_RSN_IE_VERSION = 21,
WIFI_REASON_INVALID_RSN_IE_CAP = 22,
WIFI_REASON_802_1X_AUTH_FAILED = 23,
WIFI_REASON_CIPHER_SUITE_REJECTED = 24,
WIFI_REASON_BEACON_TIMEOUT = 200,
WIFI_REASON_NO_AP_FOUND = 201,
WIFI_REASON_AUTH_FAIL = 202,
WIFI_REASON_ASSOC_FAIL = 203,
WIFI_REASON_HANDSHAKE_TIMEOUT = 204,
WIFI_REASON_CONNECTION_FAIL = 205,
} wifi_err_reason_t;
注:如果出现使用信道 13 & 隐藏 SSID 的情况下出现 Wi-Fi 连接失败的问题,可参考以下代码修改 policy
:
wifi_country_t wifi_country = { 0 };
ESP_ERROR_CHECK(esp_wifi_get_country(&wifi_country) );
wifi_country.policy = WIFI_COUNTRY_POLICY_MANUAL;
ESP_ERROR_CHECK(esp_wifi_set_country(&wifi_country) );