I'm trying to make request from esp8266 to my Flask app running on localhost.
I'm able to send http request from my esp6288 when the Flask app is running on http with:
app.run(host="0.0.0.0", debug=True)
Now I'm trying to run Flask app locally on https using self-signed certificate, and requests from browser works.
app.run(host="0.0.0.0", ssl_context=('https_keys/cert.pem', 'https_keys/key.pem'), debug=True)
But requests from esp8266 fail on my app but work on another online website (with appropriate certificate).
Here is my code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
const char* ssid = "mySSID"; // Replace with your WiFi SSID
const char* password = "password"; // Replace with your WiFi password
// Initialize BearSSL
BearSSL::WiFiClientSecure client;
// Root certificate for howsmyssl.com
const char IRG_Root_X1 [] PROGMEM = R"CERT(
-----BEGIN CERTIFICATE-----
MIIF4zCCA8ugAwIBAgIUfjixYjpiV7ZKTGaO+uHCHY8PokowDQYJKoZIhvcNAQEL
BQAwgYAxCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhOZXcgWW9yazERMA8GA1UEBwwI
TmV3IFlvcmsxDTALBgNVBAoMBFRvdG8xDTALBgNVBAsMBHVuaXQxDTALBgNVBAMM
BG5hbWUxHjAcBgkqhkiG9w0BCQEWD290b3RAaXJ3ZW9yLmNvbTAeFw0yNDAxMTYw
OTE2MjhaFw0yNTAxMTUwOTE2MjhaMIGAMQswCQYDVQQGEwJVUzERMA8GA1UECAwI
TmV3IFlvcmsxETAPBgNVBAcMCE5ldyBZb3JrMQ0wCwYDVQQKDARUb3RvMQ0wCwYD
VQQLDAR1bml0MQ0wCwYDVQQDDARuYW1lMR4wHAYJKoZIhvcNAQkBFg9vdG90QGly
d2Vvci5jb20wggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDMxtq54xdb
X49eSc431LVyv0hoQxj3v0nft08TSM5yvpVKdyO9ICZiz2OMD04Rb2fV5NHStSVr
11vJ2bZDiwwazkaEzb3EcM85iKk2gIMQP/3wLybqyNGQvocGbk6jc9aaNWpMvaEe
lii7SM1Y8TDx060S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XOQNYoJhi5
4fndG0S4LGLGTcPj9nFDB8HpTfWNbS09cx7mK06kN8XdH1Mn64xDrEyibeBU75dV
T4VVvI58HvLp9Z6tMjSmZTxantjCI9djfevnNp9s3P20oe5veDEo67Cmz7xhhC41
TGKODRzkBrm11dNx/51tCRUSfw08mwlOBHZA3TAsfmXgtyAPLWpoGi0nl7o+fMyf
3ZytEJ+/PKDTwPrUdyb/Gitg5zC1xxwAyYPszmijW68iBB1UqJXDm/uRm488/U9n
z2PtWkpdpzknAt8S0vBNV+clk6pZOfU=
-----END CERTIFICATE-----
)CERT";
// Create a list of certificates with the server certificate
X509List cert(IRG_Root_X1);
String serverURL = "https://192.168.1.79:5000/a=455&b=-73";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set time via NTP, as required for x.509 validation
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
void loop() {
// Example usage of sendHttpRequest function
sendHttpRequest(serverURL);
delay(5000); // Wait for 5 seconds before making the next request
}
void sendHttpRequest(String url) {
client.setTrustAnchors(&cert);
//client.setInsecure();
HTTPClient https;
Serial.println("URL: " + url);
// Make a GET request
https.begin(client, url);
int httpCode = https.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = https.getString();
Serial.println("Http request Response:");
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET request failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end(); // Close connection
}
I got error: "[HTTP] GET request failed, error: connection failed".
What could be the problem?
Thanks