I recently acquired an esp32, a pycom LoPy yay.
I am trying to connect to a wifi network with my esp32 but it always seems to disconnect.
I used the IDF example https_request.
The code catches the events: SYSTEM_EVENT_STA_START, CONNECTED, GOT_IP, and DISCONNECTED.
The reason for disconnecting is
201(WIFI_REASON_NO_AP_FOUND) or 2(WIFI_REASON_AUTH_EXPIRE) these occur very often,
3(WIFI_REASON_AUTH_LEAVE) or 4(WIFI_REASON_ASSOC_EXPIRE) occurs when it connects but then disconnect.
I also tried my phone as an accesspoint instead of my router, same results.
Any help would be appreciated!
The wifi state jumps from 0 to 2 and 2 to 0 alot and sometimes it does connect(jumps to 3 and somtimes 5) , but this doesn't last.
My serial output:
Code: Select all
D (101626) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:Gadfhfss, ssid_len:8, bssid:64:bc:0c:82:0f:91, reason:2[0m
V (101626) event: enter default callback[0m
V (101636) event: exit default callback[0m
[0;32mI (101636) example: disconnect reason: 2[0m
[0;32mI (101646) example: reconnected Ok or error: 0, authmode: 0[0m
I (102516) wifi: n:6 0, o:6 0, ap:255 255, sta:6 0, prof:1
I (102516) wifi: state: 0 -> 2 (b0)
I (103516) wifi: state: 2 -> 0 (2)
D (103516) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:Gadfhfss, ssid_len:8, bssid:64:bc:0c:82:0f:91, reason:2[0m
V (103516) event: enter default callback[0m
V (103516) event: exit default callback[0m
[0;32mI (103526) example: disconnect reason: 2[0m
[0;32mI (103526) example: reconnected Ok or error: 0, authmode: 0[0m
D (105636) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:Gadfhfss, ssid_len:8, bssid:00:00:00:00:00:00, reason:201[0m
V (105636) event: enter default callback[0m
V (105636) event: exit default callback[0m
[0;32mI (105646) example: disconnect reason: 201[0m
[0;32mI (105646) example: reconnected Ok or error: 0, authmode: 81[0m
I (106516) wifi: n:6 0, o:6 0, ap:255 255, sta:6 0, prof:1
I (106516) wifi: state: 0 -> 2 (b0)
I (106526) wifi: state: 2 -> 3 (0)
I (106586) wifi: state: 3 -> 5 (10)
I (109676) wifi: connected with Gadfhfss, channel 6
D (109676) event: SYSTEM_EVENT_STA_CONNECTED, ssid:Gadfhfss, ssid_len:8, bssid:64:bc:0c:82:0f:91, channel:6, authmode:3[0m
V (109686) event: enter default callback[0m
V (109686) event: exit default callback[0m
[0;32mI (109696) example: connected! ip = 1717854535[0m
I (113616) wifi: state: 5 -> 2 (3a0)
I (113616) wifi: n:6 0, o:6 0, ap:255 255, sta:6 0, prof:1
D (113616) event: SYSTEM_EVENT_STA_DISCONNECTED, ssid:Gadfhfss, ssid_len:8, bssid:64:bc:0c:82:0f:91, reason:3[0m
V (113626) event: enter default callback[0m
V (113626) event: exit default callback[0m
Code: Select all
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
Code: Select all
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
int32_t eet;
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
eet = esp_wifi_connect();
ESP_LOGI(TAG, "Ok or error: %d, authmode: %d", eet, event->event_info.connected.authmode);
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "connected! ip = %d", event->event_info.got_ip.ip_info.ip.addr);
break;
case SYSTEM_EVENT_STA_GOT_IP:
//not yet
ESP_LOGI(TAG, "ip = %d", event->event_info.got_ip.ip_info.ip.addr);
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
ESP_LOGI(TAG, "disconnect reason: %d", event->event_info.disconnected.reason);
eet = esp_wifi_connect();
ESP_LOGI(TAG, "reconnected Ok or error: %d, authmode: %d", eet, event->event_info.connected.authmode);
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
ESP_LOGI(TAG, "Other system event %d...", event->event_info.connected.authmode );
break;
}
return ESP_OK;
}