#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
#define LEDPIN 2 // LED on GPIO 2
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String server = "http://api.thingspeak.com/update?api_key=8BIT79HNKE0H5PXF";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
// WiFi connect
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
dht.begin();
// LED setup
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW); // start OFF
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Threshold LED action
if (temperature > 30) {
digitalWrite(LEDPIN, HIGH); // turn ON
Serial.println("LED ON - Temp too high!");
} else {
digitalWrite(LEDPIN, LOW); // turn OFF
}
// Send to ThingSpeak
String url = server + "&field1=" + String(temperature) + "&field2=" + String(humidity);
http.begin(url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Error sending data");
}
http.end();
}
delay(20000); // ThingSpeak requires 15s minimum interval
}