#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <HTTPClient.h>
#else
#error "Only ESP8266 or ESP32"
#endif
#include <WiFiClientSecure.h>
const char *ssid = "SSID";
// >= 8 oder <= 63 Zeichen oder NULL
const char *pass = "geheim";
const char *host = "www.wikinger-tommy.de";
const uint16_t port = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.println(host);
Serial.println("Ohne Sicherheitseinstellungen");
if (!client.connect(host, port)) {
Serial.println("Connection failed");
return;
}
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
if (http.begin(client, "https://www.wikinger-tommy.de/ardutest/ardutest.php")) {
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP Unable to connect\n");
}
}
void loop() {}
Ohne Sicherheitseinstellung bekommen wir keine Verbindung, wie wir an der Ausgabe sehen:
.......
WiFi connected
IP address:
192.168.178.28
Connecting to www.wikinger-tommy.de
Ohne Sicherheitseinstellungen
Connection failed
Was können wir also tun, um eine HTTPS-Verbindung zu nutzen.
Wir haben 4 Möglichkeiten:
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <HTTPClient.h>
#else
#error "Only ESP8266 or ESP32"
#endif
#include <WiFiClientSecure.h>
const char *ssid = "SSID";
// >= 8 oder <= 63 Zeichen oder NULL
const char *pass = "geheim";
const char *host = "www.wikinger-tommy.de";
const uint16_t port = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.println(host);
Serial.println("Mit setInsecure()");
client.setInsecure(); // <--------------------------
if (!client.connect(host, port)) {
Serial.println("Connection failed");
return;
}
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
if (http.begin(client, "https://www.wikinger-tommy.de/ardutest/ardutest.php")) {
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTPS Unable to connect\n");
}
}
void loop() {}
Wir sehen, die Verbindung ist erfolgreich.
.......
WiFi connected
IP address:
192.168.178.28
Connecting to www.wikinger-tommy.de
Mit setInsecure()
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
HTTPS-Verbindung erfolgreich
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
const char *ssid = "SSID";
// >= 8 oder <= 63 Zeichen oder NULL
const char *pass = "geheim";
const char *host = "www.wikinger-tommy.de";
const uint16_t port = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.println(host);
Serial.println("Mit Fingerprint");
const char* fingerprint = "61:26:BD:03:CA:0D:55:7A:C8:C1:75:82:4F:EE:51:2A:F6:0B:24:72";
// const char* fingerprint = "6126bd03ca0d557ac8c175824fee512af60b2472";
client.setFingerprint(fingerprint); // <--------------------------
if (!client.connect(host, port)) {
Serial.println("Connection failed");
return;
}
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
if (http.begin(client, "https://www.wikinger-tommy.de/ardutest/ardutest.php")) {
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTPS Unable to connect\n");
}
}
void loop() {}
Wir sehen, die Verbindung ist erfolgreich.
.......
WiFi connected
IP address:
192.168.178.28
Connecting to www.wikinger-tommy.de
Mit Fingerprint
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
HTTPS-Verbindung erfolgreich
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <HTTPClient.h>
#else
#error "Only ESP8266 or ESP32"
#endif
#include <WiFiClientSecure.h>
const char *ssid = "SSID";
// >= 8 oder <= 63 Zeichen oder NULL
const char *pass = "geheim";
const char *host = "www.wikinger-tommy.de";
const uint16_t port = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.println(host);
// Initialisierung Root-CA im zweiten .ino File
configCA();
if (!client.connect(host, port)) {
Serial.println("Connection failed");
return;
}
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
if (http.begin(client, "https://www.wikinger-tommy.de/ardutest/ardutest.php")) {
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTPS Unable to connect\n");
}
}
void loop() {}
Wenn wir unseren Client nur für einen bestimmten Server benötigen, dann können wir dessen Root-CA-Zertifikat auch wieder im Browser ermitteln und eintragen.
Wieder vom Schloss zum Zertifikat durchklicken und dann für Firefox auf PEM (Zertifikatskette) gehen. Es ist das letzte in der Liste.
Für Edge ist es etwas umständlicher. Oben auf Zertifizierungspfad klicken und den obersten Eintrag wählen, Details, In Datei kopieren, Base64 wählen, weiter, Dateiname vergeben, fertig.
Wenn wir unser Root-Zertifikat haben, können wir dieses im 2. File "_clientOneCA.ino" einbauen. Achtung, nicht einrücken!
// Im Browser die Website aufrufen, mit der Mouse auf das Schloß klicken, weitere Informationen
// Zertifikat anzeigen, ganz nach rechts auf das höchste Zertifikat gehen, runterscrollen bis "Verschiedenes"
// dort PEM(Zertifikat) wählen und zwischen "CERT( und )CERT"; einfügen, evtl. Gültigkeitsdatum eintragen
// gültig bis Mon, 04 Jun 2035
// das verhindert das automatische Einrücken in der IDE
// *INDENT-OFF*
const char cert_XSRG_ROOT_X1 [] PROGMEM = R"CERT(
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0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";
// *INDENT-ON*
#if defined(ESP8266)
X509List cert(cert_XSRG_ROOT_X1);
#endif
void configCA() {
#if defined(ESP8266)
client.setTrustAnchors(&cert);
#elif defined(ESP32)
client.setCACert(cert_XSRG_ROOT_X1);
#endif
Serial.println("Single CA");
}
Auch das funktioniert:
.......
WiFi connected
IP address:
192.168.178.28
Connecting to www.wikinger-tommy.de
Single CA
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
HTTPS-Verbindung erfolgreich
#include <CertStoreBearSSL.h>
#include <LittleFS.h>
// certs.ar muss im LittleFS liegen
BearSSL::CertStore certStore;
void configCA() {
Serial.println("alle CA-Zertifikate");
if (LittleFS.begin()) {
int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
client.setCertStore(&certStore);
Serial.print("Certstore. CAs: ");
Serial.println(numCerts);
}
else Serial.println("ERROR: Little FS nicht vorhanden");
}
Auch damit ist die Verbindung erfolgreich:
.......
WiFi connected
IP address:
192.168.178.28
Connecting to www.wikinger-tommy.de
alle CA-Zertifikate
Certstore. CAs: 154
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
HTTPS-Verbindung erfolgreich
Andere Websites könnt Ihr damit dann selbst ausprobieren.