mirror of
				https://github.com/0xFEEDC0DE64/arduino-esp32.git
				synced 2025-10-31 06:01:39 +01:00 
			
		
		
		
	
		
			
	
	
		
			107 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
		
		
			
		
	
	
			107 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
|   | /*|----------------------------------------------------------|*/ | ||
|  | /*|WORKING EXAMPLE FOR HTTPS CONNECTION                      |*/ | ||
|  | /*|TESTED BOARDS: Devkit v1 DOIT, Devkitc v4                 |*/ | ||
|  | /*|CORE: June 2018                                           |*/ | ||
|  | /*|----------------------------------------------------------|*/ | ||
|  | #include <WiFi.h>
 | ||
|  | #include <WiFiClientSecure.h>
 | ||
|  | #include "esp_wpa2.h"
 | ||
|  | #include <Wire.h>
 | ||
|  | #define EAP_IDENTITY "identity" //if connecting from another corporation, use identity@organisation.domain in Eduroam 
 | ||
|  | #define EAP_PASSWORD "password" //your Eduroam password
 | ||
|  | const char* ssid = "eduroam"; // Eduroam SSID
 | ||
|  | const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
 | ||
|  | int counter = 0; | ||
|  | const char* test_root_ca= \ | ||
|  | "-----BEGIN CERTIFICATE-----\n" \ | ||
|  | "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ | ||
|  | "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ | ||
|  | "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \ | ||
|  | "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \ | ||
|  | "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \ |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bQk\n" \ | ||
|  | "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ | ||
|  | "-----END CERTIFICATE-----\n"; | ||
|  | // You can use x.509 client certificates if you want
 | ||
|  | //const char* test_client_key = "";   //to verify the client
 | ||
|  | //const char* test_client_cert = "";  //to verify the client
 | ||
|  | WiFiClientSecure client; | ||
|  | 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
 | ||
|  |   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(); | ||
|  |     } | ||
|  |   } | ||
|  |   client.setCACert(test_root_ca); | ||
|  |   //client.setCertificate(test_client_key); // for client verification
 | ||
|  |   //client.setPrivateKey(test_client_cert);  // for client verification
 | ||
|  |   Serial.println(""); | ||
|  |   Serial.println("WiFi connected"); | ||
|  |   Serial.println("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.println("Wifi is still connected with IP: ");  | ||
|  |     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.println(host); | ||
|  |   if (client.connect(host, 443)) { | ||
|  |     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"); | ||
|  |     while (client.connected()) { | ||
|  |       String header = client.readStringUntil('\n'); | ||
|  |       Serial.println(header); | ||
|  |       if (header == "\r") { | ||
|  |         break; | ||
|  |       } | ||
|  |     } | ||
|  |     String line = client.readStringUntil('\n'); | ||
|  |    Serial.println(line); | ||
|  |   }else{ | ||
|  |       Serial.println("Connection unsucessful"); | ||
|  |     } | ||
|  |   delay(5000); | ||
|  | } |