From 012fa54f4efed05725a0ff238f0a3a1af588d685 Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Thu, 14 Oct 2021 18:30:43 +1000 Subject: [PATCH] Wifi --- .gitignore | 3 +- main/CMakeLists.txt | 1 + main/main.c | 2 ++ main/nvs.c | 11 +++++++ main/nvs.h | 3 ++ main/usb-cdc.h | 2 +- main/wifi.c | 80 +++++++++++++++++++++++++++++++++++++++++++-- main/wifi.h | 2 +- 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 main/nvs.c create mode 100644 main/nvs.h diff --git a/.gitignore b/.gitignore index 43285c6..82f6ddc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode build -sdkconfig.old \ No newline at end of file +sdkconfig.old +main/wifi-credentials.h \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a5261c3..0cc0201 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES "main.c" "usb-cdc.c" + "nvs.c" "wifi.c" ) diff --git a/main/main.c b/main/main.c index 7565fe5..b619fe9 100644 --- a/main/main.c +++ b/main/main.c @@ -1,6 +1,7 @@ #include #include #include "usb-cdc.h" +#include "nvs.h" #include "wifi.h" static const char* TAG = "main"; @@ -9,6 +10,7 @@ void app_main(void) { ESP_LOGI(TAG, "start"); usb_cdc_init(); + nvs_init(); wifi_init(); ESP_LOGI(TAG, "end"); diff --git a/main/nvs.c b/main/nvs.c new file mode 100644 index 0000000..c8d0325 --- /dev/null +++ b/main/nvs.c @@ -0,0 +1,11 @@ +#include +#include + +void nvs_init(void) { + 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); +} diff --git a/main/nvs.h b/main/nvs.h new file mode 100644 index 0000000..caa8010 --- /dev/null +++ b/main/nvs.h @@ -0,0 +1,3 @@ +#pragma once + +void nvs_init(void); \ No newline at end of file diff --git a/main/usb-cdc.h b/main/usb-cdc.h index 4f78b2f..48745a4 100644 --- a/main/usb-cdc.h +++ b/main/usb-cdc.h @@ -1,3 +1,3 @@ #pragma once -void usb_cdc_init(void); \ No newline at end of file +void usb_cdc_init(void); diff --git a/main/wifi.c b/main/wifi.c index b664b4d..0da18b0 100644 --- a/main/wifi.c +++ b/main/wifi.c @@ -1,7 +1,83 @@ #include +#include +#include +#include "wifi-credentials.h" static const char* TAG = "wifi"; +#if !defined(WIFI_SSID) || !defined(WIFI_PASS) +#error Create wifi-credentials.h with defined WIFI_SSID and WIFI_PASS +#endif + +#define WIFI_MAXIMUM_RETRY 3 + +// TODO remove global vars +static EventGroupHandle_t wifi_event_group; +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 +static int s_retry_num = 0; + +static void + 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(s_retry_num < WIFI_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(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(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); + } +} + void wifi_init(void) { - ESP_LOGI(TAG, "Not implemented"); -} \ No newline at end of file + 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_ERROR_CHECK( + esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK( + esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + + wifi_config_t wifi_config = { + .sta = {.ssid = WIFI_SSID, .password = WIFI_PASS}, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(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_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(TAG, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS); + } else if(bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } + + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler)); +} diff --git a/main/wifi.h b/main/wifi.h index 79d9677..1f87ee7 100644 --- a/main/wifi.h +++ b/main/wifi.h @@ -1,3 +1,3 @@ #pragma once -void wifi_init(void); \ No newline at end of file +void wifi_init(void);