#include <WiFi.h>
#include<HTTPClient.h>
#include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
const String url = "http://api.thingspeak.com/update?";
String myApiKey="28I0S98IC9Z3HFZL";
HTTPClient http;
DHTesp dht;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("WiFi connected");
}
dht.setup(15, DHTesp::DHT22); // Connect DHT sensor to GPIO 17
http.begin(url);
Serial.println("HTTP STARTED");
}
void loop()
{
float x = dht.getHumidity();
float y = dht.getTemperature();
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST
String httpRequestData = "{\"api_key\":\""+ myApiKey +"\",\"field5\":\"" + String(x)+"\",\"field6\":\""+String(y)+"\"}";
// Send HTTP POST request
Serial.println(httpRequestData);
Serial.println("\n\n");
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
Serial.println("Json Sent");
Serial.print("temp=");Serial.print(x);Serial.println("°C");
Serial.print("hum=");Serial.print(y);Serial.println("%");
delay(2000);
http.end();
}
else{
Serial.print("Error code:");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
delay(10000);
}