Created
June 4, 2018 23:35
-
-
Save martinius96/6a5ce106ff2c43eb5a1d65e086d5914b to your computer and use it in GitHub Desktop.
ESP32 sketch for access Eduroam network under WPA2 Enterprise (802.1x), also usable for another WPA2 Enterprise networks!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sketch edited by: Martin Chlebovec | |
//My website: https://arduino.php5.sk | |
//Donate me: https://www.paypal.me/Chlebovec | |
//You can use it for eduroam or for any other RADIUS network | |
#include "esp_wpa2.h" | |
#include <WiFi.h> | |
String line; //variable for response | |
const char* ssid = "eduroam"; // Eduroam SSID | |
const char* host = "arduino.php5.sk"; //external server domain | |
#define EAP_IDENTITY "login@university.com" //identity@youruniversity.domain | |
#define EAP_PASSWORD "password" //your Eduroam password | |
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 | |
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 | |
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 to default (fixed for 2018 and Arduino 1.8.5+) | |
esp_wifi_sta_wpa2_ent_enable(&config); //set config to enable function (fixed for 2018 and Arduino 1.8.5+) | |
Serial.println("MAC address: "); | |
Serial.println(WiFi.macAddress()); //Print our MAC address of our device | |
WiFi.begin(ssid); //connect to Eduroam function | |
WiFi.setHostname("ESP32Name"); //set Hostname for your device - not neccesary | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); //print LAN IP | |
} | |
void loop() { | |
delay(5000); | |
if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry | |
WiFi.begin(ssid); | |
delay(500); | |
} | |
Serial.print("Connecting to website: "); | |
Serial.println(host); | |
WiFiClient client; | |
if (!client.connect(host, 80)) { // HTTP connection on port 80 | |
Serial.println("Connection lost! - Failed response"); | |
} | |
String url = "/rele/rele1.txt"; //read .txt file | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println("Client timed out! - retry"); | |
client.stop(); | |
} | |
} | |
while(client.available()) { | |
line = client.readStringUntil('\n'); | |
Serial.println(line); | |
} | |
Serial.println(); | |
Serial.println("End connection"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment