Skip to content

Commit 273196d

Browse files
martinius96me-no-dev
authored andcommitted
HTTPClientEnterprise example (espressif#2023)
1 parent 5d2460c commit 273196d

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*|----------------------------------------------------------|*/
2+
/*|WORKING EXAMPLE FOR HTTP/HTTPS CONNECTION |*/
3+
/*|TESTED BOARDS: Devkit v1 DOIT, Devkitc v4 |*/
4+
/*|CORE: June 2018 |*/
5+
/*|----------------------------------------------------------|*/
6+
#include <WiFi.h>
7+
#include <HTTPClient.h>
8+
#include "esp_wpa2.h"
9+
#include <Wire.h>
10+
#define EAP_IDENTITY "identity" //if connecting from another corporation, use identity@organisation.domain in Eduroam
11+
#define EAP_PASSWORD "password" //your Eduroam password
12+
const char* ssid = "eduroam"; // Eduroam SSID
13+
int counter = 0;
14+
const char* test_root_ca= \
15+
"-----BEGIN CERTIFICATE-----\n" \
16+
"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \
17+
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \
18+
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \
19+
"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \
20+
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \
21+
"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \
22+
"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \
23+
"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \
24+
"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \
25+
"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \
26+
"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \
27+
"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \
28+
"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \
29+
"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \
30+
"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \
31+
"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \
32+
"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \
33+
"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \
34+
"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \
35+
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \
36+
"-----END CERTIFICATE-----\n";
37+
void setup() {
38+
Serial.begin(115200);
39+
delay(10);
40+
Serial.println();
41+
Serial.print("Connecting to network: ");
42+
Serial.println(ssid);
43+
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
44+
WiFi.mode(WIFI_STA); //init wifi mode
45+
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
46+
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
47+
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
48+
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
49+
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
50+
WiFi.begin(ssid); //connect to wifi
51+
while (WiFi.status() != WL_CONNECTED) {
52+
delay(500);
53+
Serial.print(".");
54+
counter++;
55+
if(counter>=60){ //after 30 seconds timeout - reset board
56+
ESP.restart();
57+
}
58+
}
59+
Serial.println("");
60+
Serial.println("WiFi connected");
61+
Serial.println("IP address set: ");
62+
Serial.println(WiFi.localIP()); //print LAN IP
63+
}
64+
void loop() {
65+
if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
66+
counter = 0; //reset counter
67+
Serial.println("Wifi is still connected with IP: ");
68+
Serial.println(WiFi.localIP()); //inform user about his IP address
69+
}else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
70+
WiFi.begin(ssid);
71+
}
72+
while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
73+
delay(500);
74+
Serial.print(".");
75+
counter++;
76+
if(counter>=60){ //30 seconds timeout - reset board
77+
ESP.restart();
78+
}
79+
}
80+
Serial.print("Connecting to website: ");
81+
HTTPClient http;
82+
http.begin("https://arduino.php5.sk/rele/rele1.txt", test_root_ca); //HTTPS example connection
83+
//http.begin("http://www.arduino.php5.sk/rele/rele1.txt"); //HTTP example connection
84+
//if uncomment HTTP example, you can comment root CA certificate too!
85+
int httpCode = http.GET();
86+
if(httpCode > 0) {
87+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
88+
//file found at server --> on unsucessful connection code will be -1
89+
if(httpCode == HTTP_CODE_OK) {
90+
String payload = http.getString();
91+
Serial.println(payload);
92+
}
93+
}else{
94+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
95+
}
96+
http.end();
97+
delay(2000);
98+
}

0 commit comments

Comments
 (0)