#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST"; // Wokwi simulated WiFi SSID
const char* password = ""; // No password for Wokwi-GUEST
String server = "http://api.thingspeak.com/update?api_key=WM7B7EEKXLB0ZABA";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
dht.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if reads failed and skip upload if so
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor");
delay(2000);
return;
}
String url = server + "&field1=" + String(temperature) + "&field2=" + String(humidity);
http.begin(url); // Specify destination for HTTP request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.printf("Error sending data: %d\n", httpResponseCode);
}
http.end(); // Free resources
}
delay(20000); // ThingSpeak requires 15 seconds minimum interval between updates
}