From 8d61e4be6b5e277b9b73a0f5d9dc40cd8f14a701 Mon Sep 17 00:00:00 2001 From: Renze Nicolai Date: Fri, 13 May 2022 22:56:20 +0200 Subject: [PATCH] Split wifi_init into wifi_init and wifi_connect --- main/include/wifi_connection.h | 3 ++- main/main.c | 5 ++++- main/wifi_connection.c | 10 +++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/main/include/wifi_connection.h b/main/include/wifi_connection.h index 7c011ab..a46eb13 100644 --- a/main/include/wifi_connection.h +++ b/main/include/wifi_connection.h @@ -5,4 +5,5 @@ #include "esp_wifi.h" -bool wifi_init(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax); +void wifi_init(); +bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax); diff --git a/main/main.c b/main/main.c index 9d8b981..24d2e3b 100644 --- a/main/main.c +++ b/main/main.c @@ -379,6 +379,9 @@ void app_main(void) { ws2812_init(GPIO_LED_DATA); uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0}; ws2812_send_data(ledBuffer, sizeof(ledBuffer)); + + /* Start WiFi */ + wifi_init(); /* Launcher menu */ while (true) { @@ -419,7 +422,7 @@ void app_main(void) { } } nvs_close(&handle); - wifi_init(ssid, password, WIFI_AUTH_WPA2_PSK, 3); + wifi_connect(ssid, password, WIFI_AUTH_WPA2_PSK, 3); } else if (menu_action == ACTION_OTA) { graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Firmware update..."); } else if (menu_action == ACTION_SETTINGS) { diff --git a/main/wifi_connection.c b/main/wifi_connection.c index 7883289..931a4c9 100644 --- a/main/wifi_connection.c +++ b/main/wifi_connection.c @@ -41,9 +41,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_ } } -bool wifi_init(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax) { - gRetryCounter = 0; - gRetryMax = aRetryMax; +void wifi_init() { s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); @@ -58,7 +56,13 @@ bool wifi_init(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthm esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); + + ESP_ERROR_CHECK(esp_wifi_stop()); +} +bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax) { + gRetryCounter = 0; + gRetryMax = aRetryMax; wifi_config_t wifi_config = {0}; strcpy((char*) wifi_config.sta.ssid, aSsid); strcpy((char*) wifi_config.sta.password, aPassword);