Using WPA2 Enterprise with Python?

heikki.hietala
Posts: 5
Joined: Tue Sep 04, 2018 6:47 am

Using WPA2 Enterprise with Python?

Postby heikki.hietala » Sun Sep 16, 2018 7:24 am

Hi,

I was able to connect to our Eduroam network with the ESP32 DevKitC using the Arduino IDE and the C code.

I am now working on a Python project for which I am using the uPyCraft IDE which works fine.

My question now is in two parts:

1) Does someone have an example of connecting to a WPA2 Enterprise using Python as such?

2) Does someone have the capability of creating a C module which can be called from Python to connect?

Many thanks for any pointers in this issue :D

parys15
Posts: 2
Joined: Mon Oct 22, 2018 2:30 pm

Re: Using WPA2 Enterprise with Python?

Postby parys15 » Mon Oct 22, 2018 2:51 pm

Hi,
According to Micropython documentation (https://docs.micropython.org/en/latest/ ... twork.html) it should look something like this:

Code: Select all

from network import WLAN
wlan = WLAN(mode=WLAN.STA)
wlan.connect(ssid='eduroam', auth=(WLAN.WPA2_ENT, 'username', 'password'), identity='identity')
but so far I couldn't connect with this code....
To your second question: I think there is a

Code: Select all

spam
module to connect C and Python codes.
And now my question, How did you connect to eduroam with C code? I think I have tried all the possible methods (for example with: https://github.com/martinius96/ESP32-Eduroam) but without success...
Could you "direct me to the right path"?
Regards.

heikki.hietala
Posts: 5
Joined: Tue Sep 04, 2018 6:47 am

Re: Using WPA2 Enterprise with Python?

Postby heikki.hietala » Tue Oct 23, 2018 4:02 am

Sure,

this is what I used:

//////////////

#include "esp_wpa2.h"
#include <WiFi.h>

const char* ssid = "MY_SSID"; // your ssid
#define EAP_ID "MY_EAP_ID"
#define EAP_USERNAME "MY_EAP_USERNAME"
#define EAP_PASSWORD "MY_EAP_PASSWD"

void setup() {
Serial.begin(115200);
delay(10);

Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

// WPA2 enterprise magic starts here
WiFi.disconnect(true);
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&config);

// WPA2 enterprise magic ends here


WiFi.begin(ssid);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

///////////////

parys15
Posts: 2
Joined: Mon Oct 22, 2018 2:30 pm

Re: Using WPA2 Enterprise with Python?

Postby parys15 » Tue Nov 06, 2018 1:19 pm

Greetings,

If someone needs it - I've got a successful connection with eduroam, but there are few limitations:
- This code runs with the newest development Framework code 3.1 / 3.2 (beta),
- Paste your Certificate in

Code: Select all

ca_pem
array,
- Fill code (or create a remote .h file) with your credentials,
- You have to wait a little bit to connect,

Code: Select all

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"

#define EXAMPLE_WIFI_SSID "eduroam"
#define EXAMPLE_EAP_METHOD EAP_PEAP

#define EXAMPLE_EAP_ID ""
#define EXAMPLE_EAP_USERNAME "login@university"
#define EXAMPLE_EAP_PASSWORD "PASSWORD"

static EventGroupHandle_t wifi_event_group;

const int CONNECTED_BIT = BIT0;

/* Constants that aren't configurable in menuconfig */
#define EAP_PEAP 1
#define EAP_TTLS 2

// You may change it
static const char *TAG = "test";

//HERE YOU MAKE Ctrl+C Ctrl+V OF YOUR UNIVERSITY CERTIFICATE 
const char *ca_pem = "Certificate:\n"\
"-----END CERTIFICATE-----\n";

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
        break;
    default:
        break;
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();

    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_WIFI_SSID,
        },
    };
    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    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_sta_wpa2_ent_set_ca_cert((uint8_t *)ca_pem, strlen(ca_pem)) );
    //DON'T USE - DON'T NEED
    // ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_cert_key(client_crt_start, client_crt_bytes,	client_key_start, client_key_bytes, NULL, 0) );
    // ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EXAMPLE_EAP_ID, strlen(EXAMPLE_EAP_ID)) );
    if (EXAMPLE_EAP_METHOD == EAP_PEAP || EXAMPLE_EAP_METHOD == EAP_TTLS) {
        ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EXAMPLE_EAP_USERNAME, strlen(EXAMPLE_EAP_USERNAME)) );
        ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EXAMPLE_EAP_PASSWORD, strlen(EXAMPLE_EAP_PASSWORD)) );
    }

    ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_enable(&config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
}

static void wpa2_enterprise_example_task(void *pvParameters)
{
    tcpip_adapter_ip_info_t ip;
    memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
    vTaskDelay(2000 / portTICK_PERIOD_MS);

    while (1) {
        vTaskDelay(2000 / portTICK_PERIOD_MS);

        if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &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()
{
    ESP_ERROR_CHECK( nvs_flash_init() );
    initialise_wifi();
    xTaskCreate(&wpa2_enterprise_example_task, "wpa2_enterprise_example_task", 4096, NULL, 5, NULL);
}
Regards,
and good luck ;)

heikki.hietala
Posts: 5
Joined: Tue Sep 04, 2018 6:47 am

Re: Using WPA2 Enterprise with Python?

Postby heikki.hietala » Wed Nov 07, 2018 6:18 am

Thank you extremely muc for this! I was just told by my University that they are wary of letting me enter Eduroam, so they gave me a 4G hotspot instead, but I will try this for sure ;P

Return to “ESP32 IDF”

Who is online

Users browsing this forum: No registered users and 12 guests