#include <ESP32Servo.h>
#include <DHT.h>
// LED pins
#define LED1 14
#define LED2 12
#define LED3 13
// PIR sensor pins
#define PIR1 22
#define PIR2 23
#define PIR3 26
// Ultrasonic sensor pins
#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
// Flame Sensor pins
#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);
// Threshold for flame detection
const int flameThreshold = 800;
// Detection range for ultrasonic sensors
const int detectionRange = 20;
// Delay time
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;
return distance;
}
void setup() {
Serial.begin(115200);
// 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();
}
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!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
int motion1 = digitalRead(PIR1);
int motion2 = digitalRead(PIR2);
int motion3 = digitalRead(PIR3);
// 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);
// Detect flame using LDR values and activate buzzer
bool flameDetected = false;
if (ldr1Value < flameThreshold) {
Serial.println("Flame Detected in Room 1!");
flameDetected = true;
}
if (ldr2Value < flameThreshold) {
Serial.println("Flame Detected in Room 2!");
flameDetected = true;
}
if (ldr3Value < flameThreshold) {
Serial.println("Flame Detected in Room 3!");
flameDetected = true;
}
if (flameDetected) {
digitalWrite(BUZZER, HIGH); // Activate buzzer
} else {
digitalWrite(BUZZER, LOW); // Deactivate buzzer
}
delay(100); // Small delay to stabilize sensor readings
}
// 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) {
servo.write(90); // Rotate to 90 degrees
isPersonDetected = true;
}
} else {
if (isPersonDetected) {
delay(delayTime); // Wait for 5 seconds
servo.write(0); // Return servo to 0 degrees
isPersonDetected = false;
}
}
}