Skip to content

Instantly share code, notes, and snippets.

@ouoam
Created October 5, 2020 02:53
Show Gist options
  • Select an option

  • Save ouoam/68b5e9fc31d92c73ae2b49a6d83a9d90 to your computer and use it in GitHub Desktop.

Select an option

Save ouoam/68b5e9fc31d92c73ae2b49a6d83a9d90 to your computer and use it in GitHub Desktop.
ESP32 802.1x Wi-Fi Login
#include <WiFi.h> //Wifi library
#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
#include <HTTPClient.h>
#define EAP_IDENTITY ""
#define EAP_PASSWORD ""
const char* ssid = "";
int counter = 0;
void connectWiFi() {
WiFi.begin(ssid); //connect to wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //after 30 seconds timeout - reset board
ESP.restart();
}
}
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to network: ");
Serial.println(ssid);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); //init wifi mode
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
connectWiFi();
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address set: ");
Serial.println(WiFi.localIP()); //print LAN IP
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
counter = 0; //reset counter
Serial.print("Wifi is still connected with IP: ");
Serial.println(WiFi.localIP()); //inform user about his IP address
HTTPClient http;
http.begin("http://ifconfig.me/ip"); //HTTP
int httpCode = http.GET();
if(httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(1000);
} else { //if we lost connection, retry
connectWiFi();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment