diff --git a/main/esp32-wifi-manager.c b/main/esp32-wifi-manager.c index 6b39373..6ac5db7 100644 --- a/main/esp32-wifi-manager.c +++ b/main/esp32-wifi-manager.c @@ -46,26 +46,29 @@ uint8_t wifi_manager_ap_count() { return ap_store.count; }; -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) { - ESP_LOGI(TAG, "wifi event: %d", event->event_id); +static esp_err_t wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + ESP_LOGI(TAG, "wifi event: %d", event_id); - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: + if(event_id == WIFI_EVENT_STA_START) { ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); //ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: + } else if(event_id == WIFI_EVENT_STA_CONNECTED) { + ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED"); + xEventGroupSetBits(wm_event_group, WIFI_CONNECTED); + } else if(event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); ESP_LOGI(TAG, "Got IP: %s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + ip4addr_ntoa(&event->ip_info.ip)); + } else if(event_id == SYSTEM_EVENT_STA_DISCONNECTED) { + wifi_event_sta_disconnected_t *event = ( wifi_event_sta_disconnected_t*)event_data; + + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED: reason: %d", event->reason); - xEventGroupSetBits(wm_event_group, WIFI_CONNECTED); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); xEventGroupClearBits(wm_event_group, WIFI_CONNECTED); - break; - case SYSTEM_EVENT_SCAN_DONE: + + } else if(event_id == SYSTEM_EVENT_SCAN_DONE) { ESP_LOGI(TAG, "SYSTEM_EVENT_SCAN_DONE"); uint8_t i = 0; @@ -115,10 +118,7 @@ found: } else { ESP_LOGI(TAG, "No APs found"); } - default: - break; } - return ESP_OK; return ESP_OK; } @@ -138,7 +138,11 @@ uint8_t wifi_manager_add_ap(char *ssid, char *password) { }; void wifi_manager_scan() { - wifi_scan_config_t scan_config = { 0 }; + wifi_scan_config_t scan_config = { + .ssid = &ap_store.aps[ap_store.last].ssid, + .show_hidden = true, + .scan_type = WIFI_SCAN_TYPE_ACTIVE, + }; ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); @@ -150,6 +154,7 @@ void wifi_manager_scan() { void wifi_manager_connect() { wifi_config_t wifi_config = { .sta = { + .channel = 11, } }; @@ -178,7 +183,8 @@ void wifi_manager_main_loop() { ) { ESP_LOGI(TAG, "AP Available, starting WiFi connect"); - wifi_manager_scan(); + wifi_manager_connect(); + //wifi_manager_scan(); vTaskDelay(10000 / portTICK_PERIOD_MS); } @@ -258,12 +264,30 @@ EventGroupHandle_t wifi_manager_start() { wifi_manager_load_config(); - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL) ); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_sta(); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + + //cfg.event_handler = wifi_event_handler; + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_start()); + esp_event_handler_instance_t instance_got_ip; + esp_event_handler_instance_t instance_any_id; + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &wifi_event_handler, + NULL, + &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, + ESP_EVENT_ANY_ID, + &wifi_event_handler, + NULL, + &instance_got_ip)); + xTaskCreate(&wifi_manager_main_loop, "wm_main_loop", 4096, NULL, 6, NULL); return wm_event_group;