|
| 1 | +/*|----------------------------------------------------------|*/ |
| 2 | +/*|WORKING EXAMPLE FOR HTTPS CONNECTION |*/ |
| 3 | +/*|TESTED BOARDS: Devkit v1 DOIT, Devkitc v4 |*/ |
| 4 | +/*|CORE: June 2018 |*/ |
| 5 | +/*|----------------------------------------------------------|*/ |
| 6 | +#include <WiFi.h> |
| 7 | +#include <WiFiClientSecure.h> |
| 8 | +#include "esp_wpa2.h" |
| 9 | +#include <Wire.h> |
| 10 | +# define EAP_IDENTITY "identity" //if connecting from another corporation, use [email protected] in Eduroam |
| 11 | +#define EAP_PASSWORD "password" //your Eduroam password |
| 12 | +const char* ssid = "eduroam"; // Eduroam SSID |
| 13 | +const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification |
| 14 | +int counter = 0; |
| 15 | +const char* test_root_ca= \ |
| 16 | +"-----BEGIN CERTIFICATE-----\n" \ |
| 17 | +"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ |
| 18 | +"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ |
| 19 | +"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \ |
| 20 | +"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \ |
| 21 | +"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \ |
| 22 | +"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \ |
| 23 | +"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \ |
| 24 | +"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \ |
| 25 | +"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \ |
| 26 | +"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \ |
| 27 | +"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \ |
| 28 | +"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \ |
| 29 | +"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \ |
| 30 | +"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \ |
| 31 | +"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \ |
| 32 | +"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \ |
| 33 | +"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \ |
| 34 | +"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \ |
| 35 | +"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ |
| 36 | +"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ |
| 37 | +"-----END CERTIFICATE-----\n"; |
| 38 | +// You can use x.509 client certificates if you want |
| 39 | +//const char* test_client_key = ""; //to verify the client |
| 40 | +//const char* test_client_cert = ""; //to verify the client |
| 41 | +WiFiClientSecure client; |
| 42 | +void setup() { |
| 43 | + Serial.begin(115200); |
| 44 | + delay(10); |
| 45 | + Serial.println(); |
| 46 | + Serial.print("Connecting to network: "); |
| 47 | + Serial.println(ssid); |
| 48 | + WiFi.disconnect(true); //disconnect form wifi to set new wifi connection |
| 49 | + WiFi.mode(WIFI_STA); //init wifi mode |
| 50 | + esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity |
| 51 | + esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same |
| 52 | + esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password |
| 53 | + esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default |
| 54 | + esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function |
| 55 | + WiFi.begin(ssid); //connect to wifi |
| 56 | + while (WiFi.status() != WL_CONNECTED) { |
| 57 | + delay(500); |
| 58 | + Serial.print("."); |
| 59 | + counter++; |
| 60 | + if(counter>=60){ //after 30 seconds timeout - reset board |
| 61 | + ESP.restart(); |
| 62 | + } |
| 63 | + } |
| 64 | + client.setCACert(test_root_ca); |
| 65 | + //client.setCertificate(test_client_key); // for client verification |
| 66 | + //client.setPrivateKey(test_client_cert); // for client verification |
| 67 | + Serial.println(""); |
| 68 | + Serial.println("WiFi connected"); |
| 69 | + Serial.println("IP address set: "); |
| 70 | + Serial.println(WiFi.localIP()); //print LAN IP |
| 71 | +} |
| 72 | +void loop() { |
| 73 | + if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network |
| 74 | + counter = 0; //reset counter |
| 75 | + Serial.println("Wifi is still connected with IP: "); |
| 76 | + Serial.println(WiFi.localIP()); //inform user about his IP address |
| 77 | + }else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry |
| 78 | + WiFi.begin(ssid); |
| 79 | + } |
| 80 | + while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots |
| 81 | + delay(500); |
| 82 | + Serial.print("."); |
| 83 | + counter++; |
| 84 | + if(counter>=60){ //30 seconds timeout - reset board |
| 85 | + ESP.restart(); |
| 86 | + } |
| 87 | + } |
| 88 | + Serial.print("Connecting to website: "); |
| 89 | + Serial.println(host); |
| 90 | + if (client.connect(host, 443)) { |
| 91 | + String url = "/rele/rele1.txt"; |
| 92 | + 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"); |
| 93 | + while (client.connected()) { |
| 94 | + String header = client.readStringUntil('\n'); |
| 95 | + Serial.println(header); |
| 96 | + if (header == "\r") { |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + String line = client.readStringUntil('\n'); |
| 101 | + Serial.println(line); |
| 102 | + }else{ |
| 103 | + Serial.println("Connection unsucessful"); |
| 104 | + } |
| 105 | + delay(5000); |
| 106 | +} |
0 commit comments