#include <WiFi.h>
#include "ThingSpeak.h"
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long channelID = 3055273;
const char* writeAPIKey = "WJQN8AX7W3QDQIV1";
WiFiClient client;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(500);
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT!");
} else {
Serial.printf("Temp: %.2f °C | Hum: %.2f %%\n", t, h);
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
int code = ThingSpeak.writeFields(channelID, writeAPIKey);
if (code == 200) {
Serial.println("ThingSpeak update OK!");
} else {
Serial.printf("ThingSpeak error, HTTP code: %d\n", code);
}
}
delay(60000);
}