esp32-wifi-enterprise git · main
WPA2 enterprise wifi setup on an ESP32
C 95.5% Markdown 4.5%git clone https://git.christianimmanuel.de/embedded/esp32-wifi-enterprise.gitwget https://git.christianimmanuel.de/embedded/esp32-wifi-enterprise/archive/esp32-wifi-enterprise.tar.gzinit
.gitignore | 1 - CMakeLists.txt | 3 ++ README.md | 5 ++ main/CMakeLists.txt | 3 ++ main/secrets.example.h | 5 ++ main/wifi_enterprise.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 072a982..c0d3fbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ **/secrets.h -**/*.pem **/build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cc026f5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.16) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wifi_hsnr) diff --git a/README.md b/README.md new file mode 100644 index 0000000..208cf78 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Connect ESP32-C3 With HN1X WiFi + +1. create a `main/secrets.c` file with the required credentials. An example can be found in `main/secrets.example.h` + +> Tested with "ESP-IDF 5.5" diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..c2345ff --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "wifi_enterprise.c" + INCLUDE_DIRS "." + EMBED_TXTFILES "ca.pem") diff --git a/main/secrets.example.h b/main/secrets.example.h new file mode 100644 index 0000000..3087d57 --- /dev/null +++ b/main/secrets.example.h @@ -0,0 +1,5 @@ +#define WIFI_SSID "HN1X" +#define EAP_IDENTITY "anonymous42@hs-niederrhein.de" +#define EAP_USERNAME "myuser123@hs-niederrhein.de" +#define EAP_PASSWORD "mypassword" + diff --git a/main/wifi_enterprise.c b/main/wifi_enterprise.c new file mode 100644 index 0000000..47a8a03 --- /dev/null +++ b/main/wifi_enterprise.c @@ -0,0 +1,121 @@ +#include <string.h> +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_eap_client.h" +#include "nvs_flash.h" + +#include "secrets.h" + +#define WIFI_CONNECTED_BIT BIT0 + +static EventGroupHandle_t +wifi_event_group; + +static +esp_netif_t *sta_netif; + +static const char* +TAG = "wifi_peap"; + +extern const uint8_t +ca_pem_start[] asm("_binary_ca_pem_start"); + +extern const uint8_t +ca_pem_end[] asm("_binary_ca_pem_end"); + + + + +static void +wifi_EventHandler(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) { + ESP_LOGW(TAG, "Disconnected, retrying..."); + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); + } +} + + + + +static void +wifi_Init(void) { + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + sta_netif = esp_netif_create_default_wifi_sta(); + wifi_event_group = xEventGroupCreate(); + + 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, &wifi_EventHandler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_EventHandler, NULL)); + + wifi_config_t wifi_config = { 0 }; + strncpy((char *)wifi_config.sta.ssid, WIFI_SSID, sizeof(wifi_config.sta.ssid)); + + ESP_LOGI(TAG, "Connecting to 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)); + + // Configure EAP client + ESP_ERROR_CHECK(esp_eap_client_set_ca_cert(ca_pem_start, + ca_pem_end - ca_pem_start)); + ESP_ERROR_CHECK(esp_eap_client_set_identity((uint8_t *)EAP_IDENTITY, + strlen(EAP_IDENTITY))); + ESP_ERROR_CHECK(esp_eap_client_set_username((uint8_t *)EAP_USERNAME, + strlen(EAP_USERNAME))); + ESP_ERROR_CHECK(esp_eap_client_set_password((uint8_t *)EAP_PASSWORD, + strlen(EAP_PASSWORD))); + ESP_ERROR_CHECK(esp_eap_client_set_eap_methods(ESP_EAP_TYPE_PEAP)); + ESP_ERROR_CHECK(esp_wifi_sta_enterprise_enable()); + + + ESP_ERROR_CHECK(esp_wifi_start()); +} + + + + +static void +wifi_Task(void *pvParameters) { + esp_netif_ip_info_t ip; + memset(&ip, 0, sizeof(esp_netif_ip_info_t)); + vTaskDelay(2000 / portTICK_PERIOD_MS); + + while (1) { + vTaskDelay(2000 / portTICK_PERIOD_MS); + + if (esp_netif_get_ip_info(sta_netif, &ip) == 0) { + ESP_LOGI(TAG, "~~~~~~~~~~~"); + ESP_LOGI(TAG, "IP:"IPSTR, IP2STR(&ip.ip)); + ESP_LOGI(TAG, "MASK:"IPSTR, IP2STR(&ip.netmask)); + ESP_LOGI(TAG, "GW:"IPSTR, IP2STR(&ip.gw)); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + } + } +} + + + + +void +app_main(void) { + ESP_ERROR_CHECK(nvs_flash_init()); + + wifi_Init(); + + // wait for ip + xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + + xTaskCreate(&wifi_Task, "wifi_Task", 4096, NULL, 5, NULL); +}