#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// https://opensky-network.org/ root certificate authority, to verify the server
// change it to your server root CA
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIF4zCCBMugAwIBAgISA26mONZh4xd63YXuD4zKIRyXMA0GCSqGSIb3DQEBCwUA\n" \
" MDIxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQD\n" \
"EwJSMzAeFw0yNDAxMTYwNjExMTFaFw0yNDA0MTUwNjExMTBaMBcxFTATBgNVBAMT\n" \
"DGRhdGEuZmZ2bC5mcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBALEM\n" \
"+/EZn9mVPh1d+M9chKyraCy2BsubV3HpCiugaKnQq7oQ3BqXwyRF+S+PofcceReW\n" \
"gI3lnCrC8iqhsWLlO+5sEie8Jz1DTIc4oloNIuftwQWjwYMJZmlxa6TYFsazLb+I\n" \
"oMOR2DzQBuf56lHo/dfHBNy359u7akRvMURz0SqXtl1/ILglEZgcEeQdRCilFl5T\n" \
"Vh2nwbCoiNyvtwTuk6P30XRzH/ev9CqgRqOFBCIOznNy3Sud0/lxZYkNspULxNpI\n" \
"Cf7ZIw257LfERtnfyCNXqqcCqgjgwV8q5N6pQaURsSm09+8+lAiw7Z7LzUo8OhVa\n" \
"/q7HzUA4s+pk7XjXKG2gJuZU3AbiUrUxjPoXdPfBJpBN1dDMIKaGIbL+lyHOBdn/\n" \
"1Viqlgb9ZDY86WAftO9ab+T+YmnqQYG/CSNo5sH/HefCv12EY2PzEzkblWjSZiWb\n" \
"4OZaT81e/xbvBEQvTg+PR0YyiOuhHAMG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gjp5paGh2scY\n" \
"VPiYCz/hyEvf/8M0rpijezD5hPBritxvve5OPDBvloPXcpn+0Kj2eFBYnVhHSPn/\n" \
"i98Ms0gl2pz+utH8sC2AUv1QIfl1qwEMO6zIenLU9HXbC7r1A82w0YHg4HQtYos5\n" \
"yIpRrm2bEKKneIQ5c+kIrfYg5Jnzg1Hf1QNFjccLBz4ttxcWsRxMHxvnFQh32lJU\n" \
"DoAPn6xXyPlLGV87UE4JcCtUFFLdSGFxI07as0aagbY3UMKk2rk7TO5XdTnoeuJZ\n" \
"W7SCtu65zjT+SKGBflfCXG8731m/CUU=\n" \
"-----END CERTIFICATE-----\n";
String payload;
String API_KEY = "xxxxxxxxxxxxxxxxxA";
String Headers = "Autorisation=Basic";
void setup() {
Serial.begin(115200);
Serial.println(); Serial.print ("Sketch : "); Serial.print ("balise_FFVL_data_balise_hour1_Wokwi.ino"); Serial.println();
Serial.println();
// Initialize Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
if (bootCount == 0) //Run this only the first time
{
bootCount = bootCount + 1;
Serial.print("BootCount= "); Serial.println(bootCount);
}
}
void loop() {
WiFiClientSecure *client = new WiFiClientSecure;
if (client) {
// set secure client with certificate
//client->setCACert(rootCACertificate);
client->setInsecure();
//create an HTTPClient instance
HTTPClient https;
//String serverPath = "https://data.ffvl.fr/api/?base=balises&r=releves_meteo&key=cc7f495f5be3d9b04a874d7c037229de";
//String serverPath = "https://data.ffvl.fr/api/?base=balises&r=histo&hours=1&idbalise=60&mode=json&key=cc7f495f5be3d9b04a874d7c037229de";
String serverPath = "https://data.ffvl.fr/php/historique_relevesmeteo.php?idbalise=60&heures=1";
/*serverPath += IcaoTime;
//serverPath += BoundingBox;
serverPath += "&";
serverPath += "headers=";
serverPath += Headers;
serverPath += API_KEY;*/
Serial.print("ServerPath= "); Serial.println(serverPath);
//Initializing an HTTPS communication using the secure client
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, serverPath)) { // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.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) {
// print server response payload
payload = https.getString();
Serial.print("Payload= "); Serial.println(payload);
affichageJson();
}
}
else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
}
}
else {
Serial.printf("[HTTPS] Unable to connect\n");
}
Serial.println();
Serial.println("Waiting 2min before the next round...");
//delay(120000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void affichageJson() {
JsonDocument filter;
JsonObject filter_0 = filter.add<JsonObject>();
filter_0["idbalise"] = true;
filter_0["vitesseVentMoy"] = true;
filter_0["vitesseVentMax"] = true;
filter_0["directVentMoy"] = true;
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload, DeserializationOption::Filter(filter));
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
for (JsonObject item : doc.as<JsonArray>()) {
String idbalise = item["idbalise"];
//Serial.println(idbalise);
if (idbalise == "60") {
const char* vitesseVentMoy = item["vitesseVentMoy"];
const char* vitesseVentMax = item["vitesseVentMax"];
const char* directVentMoy = item["directVentMoy"];
Serial.print ("idbalise = "); Serial.println(idbalise);
Serial.print ("Vitesse Vent Moy = "); Serial.println(vitesseVentMoy);
Serial.print ("vitesse Vent Max = "); Serial.println(vitesseVentMax);
Serial.print ("Direction vent Moy = "); Serial.println(directVentMoy);
break;
}
}
}