// #include "DHTesp.h"
// const int DHT_PIN = 17;
// const int PIRPIN = 16;
// const int LEDPIN = 18;
// const int BUZZERPIN = 19;
// DHTesp dhtSensor;
// void setup() {
// Serial.begin(115200);
// dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Change to DHT11 if needed
// pinMode(PIRPIN, INPUT);
// pinMode(LEDPIN, OUTPUT);
// pinMode(BUZZERPIN, OUTPUT);
// Serial.println("System Started");
// }
// void loop() {
// // Read temperature
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Serial.print("Temperature: ");
// Serial.print(data.temperature);
// Serial.println(" °C");
// // Buzzer ON if temp > 30°C
// if (data.temperature > 30) {
// digitalWrite(BUZZERPIN, HIGH);
// Serial.println("Buzzer ON");
// } else {
// digitalWrite(BUZZERPIN, LOW);
// Serial.println("Buzzer OFF");
// }
// // Motion detection
// int motion = digitalRead(PIRPIN);
// if (motion == HIGH) {
// digitalWrite(LEDPIN, HIGH);
// Serial.println("Motion Detected");
// } else {
// digitalWrite(LEDPIN, LOW);
// }
// delay(1000);
// }
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHTesp.h"
// WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Adafruit IO MQTT
const char* mqtt_server = "io.adafruit.com";
const int mqtt_port = 1883;
const char* mqtt_user = "kjnhoij90j9oj";
const char* mqtt_password = "aio_dvJD20njMwGBjNiRJfvyi4NoSgta";
// Pins
const int DHT_PIN = 17;
const int PIRPIN = 16;
const int LEDPIN = 18;
const int BUZZERPIN = 19;
// DHT
DHTesp dhtSensor;
// MQTT
WiFiClient espClient;
PubSubClient client(espClient);
// Last states
int lastMotion = -1;
int lastBuzzer = -1;
float lastTemp = -1000;
// Threshold to avoid noise
const float TEMP_THRESHOLD = 0.5;
void setup_wifi() {
Serial.println();
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi Connected!");
}
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting MQTT...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
Serial.println("MQTT Connected!");
} else {
Serial.print("Failed rc=");
Serial.println(client.state());
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(PIRPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
Serial.println("System Started");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// -----------------------
// Read Temperature
// -----------------------
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperature = data.temperature;
// Publish only if changed significantly
if (abs(temperature - lastTemp) >= TEMP_THRESHOLD) {
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
client.publish(
"kjnhoij90j9oj/feeds/temperature",
tempString
);
Serial.print("Temp Updated: ");
Serial.println(tempString);
lastTemp = temperature;
}
// -----------------------
// Buzzer Logic
// -----------------------
int buzzerState = (temperature > 30) ? 1 : 0;
digitalWrite(BUZZERPIN, buzzerState);
if (buzzerState != lastBuzzer) {
client.publish(
"kjnhoij90j9oj/feeds/buzzer",
buzzerState ? "1" : "0"
);
Serial.println(buzzerState ? "Buzzer ON" : "Buzzer OFF");
lastBuzzer = buzzerState;
}
// -----------------------
// Motion Logic
// -----------------------
int motion = digitalRead(PIRPIN);
digitalWrite(LEDPIN, motion);
if (motion != lastMotion) {
client.publish(
"kjnhoij90j9oj/feeds/motion",
motion ? "1" : "0"
);
Serial.println(motion ? "Motion ON" : "Motion OFF");
lastMotion = motion;
}
delay(200);
}