ALL CODE
NODE MCU+ DHT 11
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
const char* thingspeakApiKey = "YOUR_THINGSPEAK_WRITE_KEY";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("DHT read failed");
delay(2000);
return;
}
// Build ThingSpeak update URL
String url = "/update?api_key=" + String(thingspeakApiKey) + "&field1=" + String(t) + "&field2=" + String(h);
WiFiClient client;
if (client.connect("api.thingspeak.com", 80)) {
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: api.thingspeak.com\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Data sent to ThingSpeak: T=" + String(t) + " H=" + String(h));
}
client.stop();
delay(20000);
}
DHT 11 NODEMCU+BUZZER
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11
#define BUZZER_PIN D5
const float TEMP_THRESHOLD = 30.0; // °C — change as needed
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
dht.begin();
}
void loop() {
float t = dht.readTemperature();
if (isnan(t)) { Serial.println("DHT fail"); delay(2000); return; }
Serial.println(String("Temp: ") + t);
if (t > TEMP_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH); // alarm on
} else {
digitalWrite(BUZZER_PIN, LOW);
}
delay(1000);
}