/*
ESP32 HTTPClient HTTP JSON Example
Copyright (C) 2022, Uri Shaked
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String url = "https://ws-home.000webhostapp.com/insert.php?func=";
void setup() {
pinMode(0, OUTPUT);
WiFi.begin(ssid, password, 6);
Serial.begin(115200);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.print("\nOK! IP=");
Serial.print(WiFi.localIP());
}
void loop() {
HTTPClient http;
http.useHTTP10(true);
/*
//----------------- FUNZIONE POST JSON -----------------------
http.begin(url);
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(2048);
int soc = 99;
doc["daily_pv1"] = "12";
doc["p_input1"] = "0";
doc["i_panel1"] = "5";
doc["p_panel1"] = "2000";
doc["soc1"] = soc;
doc["last_update"] = "2024-05-14 00:01:02";
// Serialize JSON document
String dati_post;
serializeJson(doc, dati_post);
//String dati_post = "{\"out1\":\"1\",\"out2\":\"0\",\"out3\":\"1\",\"out4\":\"0\",\"reboot\":\"0\",\"daily_pv1\":\"10.1\",\"p_input1\":\"250\",\"i_panel1\":\"10\",\"p_panel1\":\"4\",\"soc1\":\"99\",\"daily_pv2\":\"11.3\",\"p_input2\":\"330\",\"i_panel2\":\"11\",\"p_panel2\":\"7\",\"soc2\":\"100\",\"last_update\":\"2024-04-22 19:26:22\"}";
int httpResponseCode = http.POST(dati_post);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
*/
//----------------- FUNZIONE GET -------------------
String data_log = "2024-06-16";
http.begin(url + "6&day=" + data_log);
http.GET();
String result = http.getString();
Serial.print("\n *** Result = " + result);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, result);
// Test if parsing succeeds.
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}else{
//float daily_pv1 = doc["daily_pv1"].as<float>();
//float daily_pv2 = doc["daily_pv2"].as<float>();
float t_min = doc["t_min"].as<int>();
float t_max = doc["t_max"].as<int>();
//String reset = doc["reset"].as<String>();
//String reset = doc["reset"].as<String>();
//uint8_t out1 = doc["out1"].as<uint8_t>();
Serial.print("\n" + String(t_min) + "; " + String(t_max) + "; ");
//digitalWrite(0, out1);
}
http.end();
delay(5000);
}