#include <ESP32Servo.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
// LED pins (controlled by PIR sensors)
#define LED1 14
#define LED2 12
#define LED3 13
// PIR sensor pins
#define PIR1 22
#define PIR2 23
#define PIR3 26
// Ultrasonic sensor pins (Trig and Echo)
#define TRIG1 2
#define ECHO1 4
#define TRIG2 5
#define ECHO2 18
#define TRIG3 19
#define ECHO3 21
// Servo pins
#define SERVO1_PIN 27
#define SERVO2_PIN 32
#define SERVO3_PIN 33
// LDR (Flame Sensor) pins for three rooms
#define LDR1_PIN 34
#define LDR2_PIN 35
#define LDR3_PIN 36
// Buzzer pin
#define BUZZER 15
// DHT22 sensor pin
#define DHTPIN 25
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API settings
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "MBVQCQWEFH3MISVV";
// Threshold for flame detection (adjust based on LDR readings)
const int flameThreshold = 800;
// Detection range for ultrasonic sensors
const int detectionRange = 20; // in cm
// Delay time (in ms) for returning servo to 0 degrees
const int delayTime = 5000;
// Servo objects
Servo servo1;
Servo servo2;
Servo servo3;
// Variables to track detection status
bool isPersonDetected1 = false;
bool isPersonDetected2 = false;
bool isPersonDetected3 = false;
// Function to measure distance using an ultrasonic sensor
long measureDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2; // Convert duration to cm
return distance;
}
void setup() {
Serial.begin(115200);
// Initialize Wi-Fi connection
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected!");
// Initialize LEDs
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
// Initialize PIR sensors
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(PIR3, INPUT);
// Initialize ultrasonic sensors
pinMode(TRIG1, OUTPUT);
pinMode(ECHO1, INPUT);
pinMode(TRIG2, OUTPUT);
pinMode(ECHO2, INPUT);
pinMode(TRIG3, OUTPUT);
pinMode(ECHO3, INPUT);
// Initialize buzzer
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
// Attach servos to pins
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo3.attach(SERVO3_PIN);
// Initialize servos to 0 degrees
servo1.write(0);
servo2.write(0);
servo3.write(0);
// Initialize DHT22 sensor
dht.begin();
Serial.println("Setup complete");
}
void loop() {
// Read temperature and humidity from DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if DHT readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
}
int motion1 = digitalRead(PIR1);
int motion2 = digitalRead(PIR2);
int motion3 = digitalRead(PIR3);
Serial.println(motion1);
// Control LEDs based on PIR sensors
digitalWrite(LED1, motion1 ? HIGH : LOW);
digitalWrite(LED2, motion2 ? HIGH : LOW);
digitalWrite(LED3, motion3 ? HIGH : LOW);
// Ultrasonic sensor and servo control logic
controlServoWithDelay(TRIG1, ECHO1, servo1, isPersonDetected1);
controlServoWithDelay(TRIG2, ECHO2, servo2, isPersonDetected2);
controlServoWithDelay(TRIG3, ECHO3, servo3, isPersonDetected3);
// Read LDR values for flame detection
int ldr1Value = analogRead(LDR1_PIN);
int ldr2Value = analogRead(LDR2_PIN);
int ldr3Value = analogRead(LDR3_PIN);
bool flameDetected = (ldr1Value < flameThreshold || ldr2Value < flameThreshold || ldr3Value < flameThreshold);
if (flameDetected) {
Serial.println("Flame detected!");
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(temperature) +
"&field2=" + String(humidity) +
"&field3=" + String(motion1) +
"&field4=" + String(motion2) +
"&field5=" + String(motion3) +
"&field6=" + String(flameDetected);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Failed to send data to ThingSpeak");
}
http.end();
}
delay(2000); // Wait 2 seconds before sending the next data
}
// Function to control servo with delay logic for ultrasonic sensors
void controlServoWithDelay(int trigPin, int echoPin, Servo& servo, bool& isPersonDetected) {
long distance = measureDistance(trigPin, echoPin);
if (distance > 0 && distance <= detectionRange) {
if (!isPersonDetected) { // If not already detected
Serial.println("Person detected! Rotating servo to 90 degrees...");
servo.write(90); // Rotate to 90 degrees
isPersonDetected = true;
}
} else {
if (isPersonDetected) { // If previously detected but now out of range
Serial.println("Person moved out of range. Waiting 5 seconds...");
delay(delayTime); // Wait for 5 seconds
servo.write(0); // Return servo to 0 degrees
Serial.println("Servo returned to 0 degrees.");
isPersonDetected = false;
}
}
}