#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "DHTesp.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
const String url = "http://api.thingspeak.com/?update";
const String url1="http://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,relativehumidity_2m&timezone=Europe%2FLondon";
HTTPClient http;
DHTesp xyz;
void gettemperature (){
http.begin(url1);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
StaticJsonDocument<1024> cod;
deserializeJson(cod,payload);
float temp = cod["current"]["temperature_2m"] ;
float hum = cod["current"]["relativehumidity_2m"];
Serial.println("la temperature du meteo= "+ temp + "°C");
Serial.println("la humidité du meteo= "+ hum + "%");
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void lectureDHT() {
float humidity = xyz.getHumidity();
float temperature = xyz.getTemperature();
Serial.print("temp dht=");Serial.print(temperature);Serial.println("°C");
Serial.print("hum dht=");Serial.print(humidity);Serial.println("%");
}
void sendsp(float temp_meteo,float hum_meteo,float tempdht,float humdht){
http.begin(url);
http.addHeader("Content-Type","application/json");
StaticJsonDocument<256> fxy;
fxy["api_key"] ="";
fxy["field1"] = string(temperature);
fxy["field2"] = string(humidity);
serializeJson(fxy,esp);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
http.end();
} else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
xyz.setup(22, DHTesp::DHT22);
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching " + url + "... ");
http.begin(url);
}
void loop() {
gettemperature();
lectureDHT();
// Disconnect
http.end();
delay(8000);
}