From dd774297839445d148bcbedafde677e5f9ed3e1d Mon Sep 17 00:00:00 2001 From: Dong-Jae Park Date: Wed, 17 Feb 2021 20:59:47 -0500 Subject: [PATCH 1/2] + app_wifi --- .../ESP32/Hardware_bringup/src/APP/app_wifi.c | 176 ++++++++++++++++++ .../ESP32/Hardware_bringup/src/APP/app_wifi.h | 15 ++ 2 files changed, 191 insertions(+) create mode 100644 Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.c create mode 100644 Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.h diff --git a/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.c b/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.c new file mode 100644 index 0000000..9c69697 --- /dev/null +++ b/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.c @@ -0,0 +1,176 @@ +/** + * @file app_wifi.c + * @author Dong-Jae (Alex) Park + * @date 17 Feb 2021 + * @brief Wifi initialization with router + * + * This document contains wifi initialization with router + */ + +#include "app_wifi.h" + +// Std. Lib +#include + +// TableUV Lib + +// External Library +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "lwip/err.h" +#include "lwip/sys.h" + +///////////////////////////////// +/////// DEFINITION //////// +///////////////////////////////// +#define EXAMPLE_ESP_WIFI_SSID "TP-LINK_B2C7EE" +#define EXAMPLE_ESP_WIFI_PASS "EDB2C7EE" +#define EXAMPLE_ESP_MAXIMUM_RETRY 10 + +/* The event group allows multiple bits for each event, but we only care about two events: + * - we are connected to the AP with an IP + * - we failed to connect after the maximum amount of retries */ +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +typedef struct { + EventGroupHandle_t wifi_event_group; + const char *TAG; + int wifi_reconnect_num ; +} app_wifi_data_S; + +///////////////////////////////////////// +/////// PRIVATE PROTOTYPE ///////// +///////////////////////////////////////// +static void app_wifi_event_handler(void*, esp_event_base_t, int32_t, void*); +static void app_wifi_init_sta(void); + +/////////////////////////// +/////// DATA //////// +/////////////////////////// +static app_wifi_data_S wifi_data; + +//////////////////////////////////////// +/////// PRIVATE FUNCTION ///////// +//////////////////////////////////////// +static void app_wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (wifi_data.wifi_reconnect_num < EXAMPLE_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + wifi_data.wifi_reconnect_num++; + ESP_LOGI(wifi_data.TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(wifi_data.wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(wifi_data.TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(wifi_data.TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + wifi_data.wifi_reconnect_num = 0; + xEventGroupSetBits(wifi_data.wifi_event_group, WIFI_CONNECTED_BIT); + } +} + +static void app_wifi_init_sta(void) +{ + wifi_data.wifi_event_group = xEventGroupCreate(); + + 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(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + esp_event_handler_instance_t instance_any_id; + esp_event_handler_instance_t instance_got_ip; + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &app_wifi_event_handler, + NULL, + &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, + IP_EVENT_STA_GOT_IP, + &app_wifi_event_handler, + NULL, + &instance_got_ip)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = EXAMPLE_ESP_WIFI_SSID, + .password = EXAMPLE_ESP_WIFI_PASS, + /* Setting a password implies station will connect to all security modes including WEP/WPA. + * However these modes are deprecated and not advisable to be used. Incase your Access point + * doesn't support WPA2, these mode can be enabled by commenting below line */ + .threshold.authmode = WIFI_AUTH_WPA2_PSK, + + .pmf_cfg = { + .capable = true, + .required = false + }, + }, + }; + 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() ); + + ESP_LOGI(wifi_data.TAG, "wifi_init_sta finished."); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(wifi_data.wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + * happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(wifi_data.TAG, "connected to ap SSID:%s password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(wifi_data.TAG, "Failed to connect to SSID:%s, password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else { + ESP_LOGE(wifi_data.TAG, "UNEXPECTED EVENT"); + } + + /* The event will not be processed after unregister */ + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); + vEventGroupDelete(wifi_data.wifi_event_group); +} + + +/////////////////////////////////////// +/////// PUBLIC FUNCTION ///////// +/////////////////////////////////////// +void app_wifi_init(void) +{ + memset(&wifi_data, 0x00, sizeof(app_wifi_data_S)); + + wifi_data.TAG = "WIFI"; + wifi_data.wifi_reconnect_num = 0; + + //Initialize NVS + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + + ESP_LOGI(wifi_data.TAG, "ESP_WIFI_MODE_STA"); + app_wifi_init_sta(); +} diff --git a/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.h b/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.h new file mode 100644 index 0000000..5303646 --- /dev/null +++ b/Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.h @@ -0,0 +1,15 @@ +/** + * @file app_wifi.h + * @author Dong-Jae (Alex) Park + * @date 17 Feb 2021 + * @brief Wifi initialization with router + * + * Wifi initialization with router + */ + +#ifndef APP_WIFI_H +#define APP_WIFI_H + +void app_wifi_init(void); + +#endif //APP_WIFI_H \ No newline at end of file From 5dcaba3c22244daae79d138c9f5a93cb5c9dde6d Mon Sep 17 00:00:00 2001 From: Dong-Jae Park Date: Wed, 17 Feb 2021 21:01:05 -0500 Subject: [PATCH 2/2] + wifi initialization in main.c --- Firmware/ESP32/Hardware_bringup/src/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Firmware/ESP32/Hardware_bringup/src/main.c b/Firmware/ESP32/Hardware_bringup/src/main.c index c909288..8ca5ef3 100644 --- a/Firmware/ESP32/Hardware_bringup/src/main.c +++ b/Firmware/ESP32/Hardware_bringup/src/main.c @@ -15,6 +15,7 @@ #include "io_ping_map.h" #include "APP/app_slam.h" #include "APP/app_supervisor.h" +#include "APP/app_wifi.h" // SDK config #include "sdkconfig.h" @@ -189,6 +190,7 @@ void app_main() esp32_task_init(); // app level init + app_wifi_init(); app_slam_init(); app_supervisor_init();