Nimbin[12]?Embedded / esp32-wifi-enterprise / main/wifi_enterprise.c

esp32-wifi-enterprise git · main

WPA2 enterprise wifi setup on an ESP32

esp32 esp-idf wifi enterprise c · first commit 2025-11-07 · last commit 2025-11-07 (11 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/wifi_enterprise

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.gz
main/wifi_enterprise.c 3.5 KB · 121 lines raw
#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);
}