#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
// ====== DHT CONFIG ======
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ====== WIFI ======
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ====== THINGSPEAK ======
unsigned long channelID = "GRK_Temp_Alert"; // Replace
const char* writeAPIKey = "Z2NVHMGU4EQH7VN9"; // Replace
// ====== THRESHOLD ======
float threshold = 55.0;
void setup() {
Serial.begin(115200);
// Connect WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if sensor failed
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("-------------");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// ====== SEND DATA TO THINGSPEAK ======
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + String(writeAPIKey) +
"&field1=" + String(temperature) +
"&field2=" + String(humidity);
http.begin(url);
int httpResponseCode = http.GET();
Serial.print("ThingSpeak Response: ");
Serial.println(httpResponseCode);
http.end();
}
// ====== LOCAL ALERT (SERIAL) ======
if (temperature > threshold) {
Serial.println("⚠️ ALERT: Temperature exceeded threshold!");
}
// ThingSpeak requires minimum 15 sec delay
delay(15000);
}