#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
float temp = 0;
float hum = 0;
int value = 0;
unsigned long lastMsg = 0;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *thingSpeakURL = "http://api.thingspeak.com/update";
const char *apiKey = "Z0E224WVPSFLL92Y";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temp = String(data.temperature, 2);
Serial.println("Temperature: "+temp);
String hum = String(data.humidity, 1);
Serial.println("Humidity: "+hum);
String url = "https://api.thingspeak.com/update?api_key=Z0E224WVPSFLL92Y&field1=";
url += String(temp);
url += "&field2=";
url += String(hum);
// Send HTTP GET request
HTTPClient http;
//sending data of temperature
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak. Response code: "+String(httpResponseCode));
// Serial.println(httpResponseCode);
} else {
Serial.println("Error sending data to ThingSpeak. Response code: "+String(httpResponseCode));
// Serial.println(httpResponseCode);
}
http.end();
delay(15000);
}