// Complete project details: http://RandomNerdTutorials.com/esp32-http-requests/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
// tentative appel en http, le server RTE retourne code 302 pour rediriger vers https
String urlhttp = "http://www.services-rte.com/cms/open_data/v1/tempoLight";
String urlhttps = "https://www.services-rte.com/cms/open_data/v1/tempoLight";
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println();
// Initialize Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
String askTempohttp(String url) {
String payload = "-";
WiFiClient *client = new WiFiClient;
if(client) {
HTTPClient http;//create an HTTPClient instance
//Initializing an http communication using the secure client
Serial.printf("[http] begin ... %s\n",url.c_str());
if (http.begin(*client, url.c_str())) { // http
Serial.print("[http] 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) {
payload = http.getString();
//Serial.println(payload);// print server response payload
}
} else {
Serial.printf("[http] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("Problem during http.begin('%s')\n",url.c_str());
}
} else {
Serial.printf("[http] Unable to connect\n");
}
delete client;
payload.trim();
return payload;
}
String askTempohttps(String url) {
String payload = "-";
WiFiClientSecure *client = new WiFiClientSecure;
if(client) {
client->setInsecure();// set secure client without certificate
HTTPClient http;//create an HTTPClient instance
//Initializing an http communication using the secure client
Serial.printf("[https] begin ... %s\n",url.c_str());
if (http.begin(*client, url.c_str())) { // http
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) {
payload = http.getString();
//Serial.println(payload);// print server response payload
}
} else {
Serial.printf("[https] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("Problem during http.begin('%s')\n",url.c_str());
}
} else {
Serial.printf("[https] Unable to connect\n");
}
delete client;
payload.trim();
return payload;
}
void loop() {
Serial.printf("Free mem : %d\n", esp_get_free_heap_size());
Serial.println("AskTempohttp");
Serial.println(askTempohttp(urlhttp));
Serial.println();
Serial.println("AskTempohttps");
Serial.println(askTempohttps(urlhttps));
Serial.println();
Serial.println("Waiting before the next round...");
delay(15000);
}