#include <HTTPClient.h>
#include <WiFi.h>
#include "DHTesp.h"
DHTesp dht ;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
WiFiClient IOT ;
HTTPClient http;
const char* url="http://api.thingspeak.com/update?";
String myApikey="9UGSVQ679IM9RG9W";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
http.begin(IOT,url);
dht.setup(15,DHTesp::DHT22);
}
void loop() {
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.println(humidity,1);
Serial.println(temperature,1);
http.addHeader("Content-Type","application/json");
String httpRequestData="{\"api_key\":\""+myApikey+"\",\"field2\":\""+String(humidity)+"\",\"field1\":\""+String(temperature)+"\"}";
int httpResponseCode =http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
// Disconnect
http.end();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
delay(2000);
}