#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
const char* ssid = "Wokwi.GUEST";
const char* password = "";
String payload;
const String url = "http://api.thingspeak.com/update?";
String myApikey = "UH13ML5YAX14OMA9";
DHTesp dht;
HTTPClient http;
WiFiClient wiwiwi;
void setup() {
dht.setup(16, DHTesp::DHT11);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi Connected");
http.begin(wiwiwi, url);
Serial.println("HTTP Started");
}
void loop() {
float y = dht.getHumidity();
float x = dht.getTemperature();
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST
String httpRequestData = "{\"api_key\":\"" + myApikey + "\",\"field1\":\"" + String(x)+ "\",\"field1\":\"" + String(y)+"\"}";
// Send HTTP POST request
Serial.println("httpRequestData");Serial.println("\n\n");
int httpResponseCode = http.POST(httpRequestData);
Serial.print(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);
}