updated WifiClientEnterprise example (#1988)

* updated WifiClientEnterprise example

* Update WiFiClientEnterprise.ino
This commit is contained in:
Martin 2018-11-19 16:59:56 +01:00 committed by Me No Dev
parent 7b811f9b3a
commit c7fa251d78

View File

@ -1,12 +1,10 @@
//Sketch edited by: martinius96 (Martin Chlebovec) #include <WiFi.h> //Wifi library
//Personal website: https://arduino.php5.sk #include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
#include "esp_wpa2.h" #define EAP_IDENTITY "login" //if connecting from another corporation, use identity@organisation.domain in Eduroam
#include <WiFi.h>
#define EAP_IDENTITY "login@university.domain" //eduroam login --> identity@youruniversity.domain
#define EAP_PASSWORD "password" //your Eduroam password #define EAP_PASSWORD "password" //your Eduroam password
String line; //variable for response
const char* ssid = "eduroam"; // Eduroam SSID const char* ssid = "eduroam"; // Eduroam SSID
const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
int counter = 0;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(10); delay(10);
@ -14,51 +12,58 @@ void setup() {
Serial.print("Connecting to network: "); Serial.print("Connecting to network: ");
Serial.println(ssid); Serial.println(ssid);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); 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_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_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_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_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
esp_wifi_sta_wpa2_ent_enable(&config); //set config to enable function (fixed for 2018 and Arduino 1.8.5+) esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
WiFi.begin(ssid); //connect to Eduroam function WiFi.begin(ssid); //connect to wifi
WiFi.setHostname("ESP32Name"); //set Hostname for your device - not neccesary
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
counter++;
if(counter>=60){ //after 30 seconds timeout - reset board
ESP.restart();
}
} }
Serial.println(""); Serial.println("");
Serial.println("WiFi connected"); Serial.println("WiFi connected");
Serial.println("IP address set: "); Serial.println("IP address set: ");
Serial.println(WiFi.localIP()); //print LAN IP Serial.println(WiFi.localIP()); //print LAN IP
} }
void loop() { void loop() {
delay(5000); if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry counter = 0; //reset counter
WiFi.begin(ssid); Serial.println("Wifi is still connected with IP: ");
delay(500); Serial.println(WiFi.localIP()); //inform user about his IP address
} }else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //30 seconds timeout - reset board
ESP.restart();
}
}
Serial.print("Connecting to website: "); Serial.print("Connecting to website: ");
Serial.println(host); Serial.println(host);
WiFiClient client; WiFiClient client;
if (!client.connect(host, 80)) { // HTTP connection on port 80 if (client.connect(host, 80)) {
Serial.println("Connection lost! - Failed response"); String url = "/rele/rele1.txt";
} client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP32\r\n" + "Connection: close\r\n\r\n");
String url = "/rele/rele1.txt"; //read .txt file
Serial.print("Requesting URL: "); while (client.connected()) {
Serial.println(url); String line = client.readStringUntil('\n');
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); if (line == "\r") {
unsigned long timeout = millis(); break;
while (client.available() == 0) { }
if (millis() - timeout > 5000) {
Serial.println("Client timed out! - retry");
} }
} String line = client.readStringUntil('\n');
while(client.available()) { Serial.println(line);
line = client.readStringUntil('\n'); }else{
Serial.println(line); Serial.println("Connection unsucessful");
} }
Serial.println();
Serial.println("End connection");
client.stop();
} }