|
1 |
| -//Sketch edited by: martinius96 (Martin Chlebovec) |
2 |
| -//Personal website: https://arduino.php5.sk |
3 |
| -#include "esp_wpa2.h" |
4 |
| -#include <WiFi.h> |
5 |
| -# define EAP_IDENTITY "[email protected]" //eduroam login --> [email protected] |
| 1 | +#include <WiFi.h> //Wifi library |
| 2 | +#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks |
| 3 | +# define EAP_IDENTITY "login" //if connecting from another corporation, use [email protected] in Eduroam |
6 | 4 | #define EAP_PASSWORD "password" //your Eduroam password
|
7 |
| -String line; //variable for response |
8 | 5 | const char* ssid = "eduroam"; // Eduroam SSID
|
9 | 6 | const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
|
| 7 | +int counter = 0; |
10 | 8 | void setup() {
|
11 | 9 | Serial.begin(115200);
|
12 | 10 | delay(10);
|
13 | 11 | Serial.println();
|
14 | 12 | Serial.print("Connecting to network: ");
|
15 | 13 | Serial.println(ssid);
|
16 | 14 | WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
|
17 |
| - WiFi.mode(WIFI_STA); |
| 15 | + WiFi.mode(WIFI_STA); //init wifi mode |
18 | 16 | esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
|
19 |
| - esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username |
| 17 | + esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same |
20 | 18 | esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
|
21 |
| - esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config to default (fixed for 2018 and Arduino 1.8.5+) |
22 |
| - esp_wifi_sta_wpa2_ent_enable(&config); //set config to enable function (fixed for 2018 and Arduino 1.8.5+) |
23 |
| - WiFi.begin(ssid); //connect to Eduroam function |
24 |
| - WiFi.setHostname("ESP32Name"); //set Hostname for your device - not neccesary |
| 19 | + esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default |
| 20 | + esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function |
| 21 | + WiFi.begin(ssid); //connect to wifi |
25 | 22 | while (WiFi.status() != WL_CONNECTED) {
|
26 | 23 | delay(500);
|
27 | 24 | Serial.print(".");
|
| 25 | + counter++; |
| 26 | + if(counter>=60){ //after 30 seconds timeout - reset board |
| 27 | + ESP.restart(); |
| 28 | + } |
28 | 29 | }
|
29 | 30 | Serial.println("");
|
30 | 31 | Serial.println("WiFi connected");
|
31 | 32 | Serial.println("IP address set: ");
|
32 | 33 | Serial.println(WiFi.localIP()); //print LAN IP
|
33 |
| - |
34 | 34 | }
|
35 | 35 | void loop() {
|
36 |
| - delay(5000); |
37 |
| - if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry |
38 |
| - WiFi.begin(ssid); |
39 |
| - delay(500); |
40 |
| -} |
| 36 | + if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network |
| 37 | + counter = 0; //reset counter |
| 38 | + Serial.println("Wifi is still connected with IP: "); |
| 39 | + Serial.println(WiFi.localIP()); //inform user about his IP address |
| 40 | + }else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry |
| 41 | + WiFi.begin(ssid); |
| 42 | + } |
| 43 | + while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots |
| 44 | + delay(500); |
| 45 | + Serial.print("."); |
| 46 | + counter++; |
| 47 | + if(counter>=60){ //30 seconds timeout - reset board |
| 48 | + ESP.restart(); |
| 49 | + } |
| 50 | + } |
41 | 51 | Serial.print("Connecting to website: ");
|
42 | 52 | Serial.println(host);
|
43 | 53 | WiFiClient client;
|
44 |
| - if (!client.connect(host, 80)) { // HTTP connection on port 80 |
45 |
| - Serial.println("Connection lost! - Failed response"); |
46 |
| - } |
47 |
| - String url = "/rele/rele1.txt"; //read .txt file |
48 |
| - Serial.print("Requesting URL: "); |
49 |
| - Serial.println(url); |
50 |
| - client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); |
51 |
| - unsigned long timeout = millis(); |
52 |
| - while (client.available() == 0) { |
53 |
| - if (millis() - timeout > 5000) { |
54 |
| - Serial.println("Client timed out! - retry"); |
| 54 | + if (client.connect(host, 80)) { |
| 55 | + String url = "/rele/rele1.txt"; |
| 56 | + 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"); |
| 57 | + |
| 58 | + while (client.connected()) { |
| 59 | + String line = client.readStringUntil('\n'); |
| 60 | + if (line == "\r") { |
| 61 | + break; |
| 62 | + } |
55 | 63 | }
|
56 |
| - } |
57 |
| - while(client.available()) { |
58 |
| - line = client.readStringUntil('\n'); |
59 |
| - Serial.println(line); |
60 |
| - } |
61 |
| - Serial.println(); |
62 |
| - Serial.println("End connection"); |
63 |
| - client.stop(); |
| 64 | + String line = client.readStringUntil('\n'); |
| 65 | + Serial.println(line); |
| 66 | + }else{ |
| 67 | + Serial.println("Connection unsucessful"); |
| 68 | + } |
64 | 69 | }
|
0 commit comments