Skip to content

Commit

Permalink
esp32/network_wlan: Fix network.WLAN.status().
Browse files Browse the repository at this point in the history
It can return STAT_NO_AP_FOUND and STAT_WRONG_PASSWORD.
This allows users to set the correct credentials.

Signed-off-by: IhorNehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa committed Nov 14, 2023
1 parent a00c9d5 commit 984e824
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ports/esp32/network_wlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_b
switch (disconn->reason) {
case WIFI_REASON_BEACON_TIMEOUT:
// AP has dropped out; try to reconnect.
message = "\nbeacon timeout";
message = "beacon timeout";
break;
case WIFI_REASON_NO_AP_FOUND:
// AP may not exist, or it may have momentarily dropped out; try to reconnect.
message = "\nno AP found";
message = "no AP found";
break;
case WIFI_REASON_AUTH_FAIL:
// Password may be wrong, or it just failed to connect; try to reconnect.
message = "\nauthentication failed";
message = "authentication failed";
break;
default:
// Let other errors through and try to reconnect.
break;
}
ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d%s", disconn->reason, message);
ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d:%s", disconn->reason, message);

wifi_sta_connected = false;
if (wifi_sta_connect_requested) {
Expand Down Expand Up @@ -333,6 +333,14 @@ STATIC mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) {
if (wifi_sta_connected) {
// Happy path, connected with IP
return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
} else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND) {
return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND);
} else if ((wifi_sta_disconn_reason == WIFI_REASON_AUTH_FAIL) || (wifi_sta_disconn_reason == WIFI_REASON_CONNECTION_FAIL)) {
// wrong password
return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_AUTH_FAIL);
} else if (wifi_sta_disconn_reason == WIFI_REASON_ASSOC_LEAVE) {
// After wlan.disconnect()
return MP_OBJ_NEW_SMALL_INT(STAT_IDLE);
} else if (wifi_sta_connect_requested
&& (conf_wifi_sta_reconnects == 0
|| wifi_sta_reconnects < conf_wifi_sta_reconnects)) {
Expand Down

0 comments on commit 984e824

Please sign in to comment.