#include "DHTesp.h"
#define DHTPIN 12 // DHT22 data pin at 12
#define PIRPIN 27 // PIR motion sensor pin
#define BUZZERPIN 5 // Digital pin connected to the buzzer
#define TRIGPIN 15 // HC-SR04 Trig pin
#define ECHOPIN 14 // HC-SR04 Echo pin
DHTesp dhtSensor;
int motionDetected = 0; // flag to indicate motion detection
int ledPin = 13; // built-in LED pin
const float maxTemperature = 45.0;
// Set the maximum temperature limit
const float minTemperature = 10.0;
// Set the minimum temperature limit
const float maxHumidity = 80.0;
// Set the maximum humidity limit
const float minHumidity = 40.0;
// Set the minimum humidity limit
void setup() {
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
// serial monitor begin
Serial.begin(9600);
pinMode(PIRPIN, INPUT); // PIR input
pinMode(ledPin, OUTPUT); // LED output
pinMode(BUZZERPIN, OUTPUT); // Buzzer OUTPUT
pinMode(TRIGPIN, OUTPUT); // HC-SR04 Trig pin
pinMode(ECHOPIN, INPUT); // HC-SR04 Echo pin
// Initialize LED control
ledcSetup(0, 5000, 8); // LEDC channel 0, 5000 Hz, 8-bit depth
ledcAttachPin(ledPin, 0); // Attach LED to channel 0
}
float getUltrasonicDistance() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
return pulseIn(ECHOPIN, HIGH) * 0.0343 / 2;
// Speed of sound is approximately 343 meters/second
}
void loop() {
float temperature = dhtSensor.getTemperature(); // Float temperature
float humidity = dhtSensor.getHumidity(); // Float humidity
motionDetected = digitalRead(PIRPIN); // Digital read from PIR
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
// Check if temperature exceeds the limits
if (temperature > maxTemperature || temperature < minTemperature) {
Serial.println("ALERT: Temperature out of range!");
}
// Check if humidity exceeds the limits
if (humidity > maxHumidity || humidity < minHumidity) {
Serial.println("ALERT: Humidity out of range!");
}
// Simulate Ultrasonic sensor distance
float distance = getUltrasonicDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the water level is above the maximum threshold
if (distance <= 10.0) { // Set thareshold Distance
// Water level is too high, sound the buzzer
digitalWrite(ledPin, HIGH); // turn on the LED
tone(BUZZERPIN, 1000);
// turn on buzzer
Serial.println("Water level too High! Alarm activated.");
} else {
// Water level is within the safe range, turn off the buzzer
digitalWrite(ledPin, LOW); // turn off the LED
noTone(BUZZERPIN);
// turn off buzzer
Serial.println ("Water level is Normal.");
}
delay(2000); //delay of 2 sec
if (motionDetected == HIGH) {
Serial.println("Motion Detected! Someone is at the Door");
digitalWrite(ledPin, HIGH); // turn on the LED
tone(BUZZERPIN, 1000);
// turn on buzzer
} else {
Serial.println("No Motion Detected");
digitalWrite(ledPin, LOW); // turn off the LED
noTone(BUZZERPIN);
// turn off buzzer
}
delay(2000); // delay of 2 sec
}