0% found this document useful (0 votes)
25 views26 pages

Final Report Prakash (EC23I2013)

The Smart Home Automation System utilizes Arduino or ESP32 microcontrollers to enhance home safety and convenience through six core modules, including car parking assistance, a burglar alarm, and a keypad-RFID door lock. Additional features include a fire and gas alert system, a smart dustbin with an auto-opening lid, and a real-time temperature and humidity display. The system aims to address common household challenges by integrating various sensors and actuators for efficient and secure automation.

Uploaded by

mkarunbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views26 pages

Final Report Prakash (EC23I2013)

The Smart Home Automation System utilizes Arduino or ESP32 microcontrollers to enhance home safety and convenience through six core modules, including car parking assistance, a burglar alarm, and a keypad-RFID door lock. Additional features include a fire and gas alert system, a smart dustbin with an auto-opening lid, and a real-time temperature and humidity display. The system aims to address common household challenges by integrating various sensors and actuators for efficient and secure automation.

Uploaded by

mkarunbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

‭Title:-Smart Home Automation‬

‭System‬

‭rakash-ec23i2013‬
P
DL.Raghu-ec23b1130‬

P.Pramod kumar-ec23i1013‬

‭ABSTRACT:-‬

‭ he Smart Home Automation System integrates six core modules powered by Arduino‬
T
‭or ESP32 microcontrollers, enhancing home safety and convenience. These include car‬
‭parking assistance using ultrasonic sensors, a burglar alarm with PIR and microwave‬
‭sensors, and a keypad-RFID based door lock controlled by a servo motor. Each module‬
‭uses relevant sensors and actuators to automate specific household tasks efficiently‬
‭and securely.‬

‭ dditional features include a fire and gas alert system using KY-026 and MQ-2 sensors,‬
A
‭which triggers alarms and sends email notifications. The smart dustbin module‬
‭promotes hygiene with an auto-opening lid using a servo motor and ultrasonic sensors.‬
‭Lastly, a real-time time and temperature display module employs the DHT22 and RTC‬
‭to provide accurate environmental data. Together, these modules create a responsive‬
‭and automated home environment.‬

‭Introduction:-‬
‭ he rapid advancement of embedded systems, sensors, and IoT technology has made‬
T
‭smart home automation both practical and accessible. These systems integrate various‬
‭components—such as microcontrollers, sensors, and wireless communication‬
‭modules—to intelligently manage and automate household operations. Their goal is to‬
‭improve daily life by offering enhanced safety, convenience, and energy efficiency.‬
‭ odern households face challenges like break-ins, fire and gas hazards, inefficient‬
M
‭waste handling, and temperature regulation. Traditional methods often lack real-time‬
‭responsiveness and require manual input, highlighting the need for a smarter, more‬
‭reliable, and cost-effective home automation solution.‬

‭Literature Survey:-‬
‭ ome automation has progressed remarkably over the past two decades, thanks to‬
H
‭advances in microcontrollers, sensors, wireless communication, and IoT. Numerous‬
‭studies and projects have explored solutions targeting various aspects of smart homes,‬
‭including safety, energy efficiency, and comfort. This review highlights key findings from‬
‭prior work relevant to the current project.‬

‭ rduino-based systems are popular in early smart home prototypes due to their‬
A
‭affordability and ease of use. For instance, in 2017, a simple setup using Arduino UNO‬
‭and Bluetooth allowed remote appliance control, though it lacked advanced sensor‬
‭integration and internet connectivity. ESP32 offers a significant upgrade by enabling IoT‬
‭functionalities.‬

I‭ntrusion detection commonly relies on PIR sensors, which detect motion based on‬
‭infrared radiation. However, they have limitations in coverage and sensitivity. Microwave‬
‭sensors like the RCWL-0516 provide 360° detection and better performance through‬
‭obstacles. Combining both sensors improves accuracy and reliability.‬

‭ igital door locks often use RFID tags and keypads for access control. Studies have‬
D
‭shown that integrating these with servo motors and LCDs creates secure and‬
‭user-friendly entry systems. Multi-factor authentication strengthens overall security.‬

‭ as and fire safety systems typically use MQ-2 and KY-026 sensors. These can detect‬
G
‭various gases and flames, triggering alarms and sometimes sending notifications via‬
‭email or SMS. This ensures both local and remote alerts for better safety.‬

‭ mart dustbins became popular during the COVID-19 era for promoting contactless‬
S
‭hygiene. Projects using ultrasonic sensors and servo motors automatically open lids‬
‭when a hand is near, preventing contamination and enhancing usability in homes and‬
‭public places.‬

‭ nvironmental monitoring is vital for comfort and safety. The DHT22 sensor measures‬
E
‭temperature and humidity accurately, while the DS3231 RTC module tracks real-time‬
‭data. Displaying this information on LCDs or dashboards helps users make informed‬
‭decisions, such as adjusting ventilation.‬
‭Methodology:-‬
‭ he Smart Home Automation System is designed based on a modular and integrated‬
T
‭design methodology, in which every subsystem tackles a unique function of smart home‬
‭automation—security, safety, convenience, and environment monitoring. The approach‬
‭is to divide the project into six functional blocks, which are developed individually with‬
‭suitable sensors, actuators, and microcontrollers, and then assembled into a single‬
‭framework.‬

‭Calculations / Flowcharts / Algorithms‬

‭1. Car Parking Assistance System:-‬

‭Flowchart:-‬

‭Algorithms:-‬
‭ .‬ I‭nitialize the ultrasonic sensor, buzzer, and LED pins.‬
1
‭2.‬ ‭Send a 10µs HIGH pulse to the Trigger pin of the ultrasonic sensor.‬
‭ .‬ W
3 ‭ ait for the Echo pin to go HIGH and measure the duration of the received pulse.‬
‭4.‬ ‭Calculate distance using the formula:‬
‭Distance (cm) = (Echo time in µs × Speed of sound) / 2‬
‭(Note: Speed of sound ≈ 0.034 cm/µs)‬
‭5.‬ ‭If the calculated distance is less than 15 cm:‬
‭○‬ ‭Turn ON the buzzer and/or LED.‬
‭6.‬ ‭Else:‬
‭○‬ ‭Keep the buzzer and LED OFF.‬
‭7.‬ ‭Repeat the above steps continuously in a loop.‬

‭2. Keypad Lock for Door:-‬

‭Flowchart:-‬

‭Algorithms:-‬
‭ .‬ I‭nitialize LCD, keypad, RFID, and servo motor.‬
1
‭2.‬ ‭Prompt users to enter a password or scan card.‬
‭3.‬ ‭If password matches or RFID is valid:‬
‭○‬ ‭Rotate servo to open.‬
‭○‬ ‭Show “Access Granted”.‬
‭4.‬ ‭Else:‬
‭○‬ ‭Show “Access Denied”.‬
‭5.‬ ‭After a delay, lock the door again.‬

‭3. Burglar Alarm & Intrusion Detection:-‬


‭Flowchart:-‬

‭Algorithm:-‬
‭ .‬
1 I‭nitialize GPIO pins for input (PIR, RCWL sensors) and output (buzzer, LED).‬
‭2.‬ ‭Start a continuous monitoring loop.‬
‭3.‬ ‭Read digital signals from both PIR and RCWL motion sensors.‬
‭4.‬ ‭If either sensor detects motion (i.e., output is HIGH):‬
‭ a. Turn ON the buzzer and LED to alert.‬
‭ b. Wait for a short delay (e.g., 1–2 seconds).‬
‭ .‬ ‭Turn OFF the buzzer and LED.‬
5
‭6.‬ ‭Repeat the process (go back to step 3).‬

‭4. Fire and Gas Leakage Alert:-‬

‭Flowchart:-‬
‭Algorithms:-‬
‭1.‬ I‭nitialize all components: MQ-2 gas sensor, KY-026 flame sensor, LCD display, buzzer,‬
‭and ESP32 (for email functionality).‬
‭2.‬ ‭Continuously monitor the analog output from the MQ-2 sensor.‬
‭3.‬ ‭Continuously check the digital output from the KY-026 sensor.‬
‭4.‬ ‭If‬‭gas concentration exceeds threshold‬‭OR‬‭flame is detected:‬
‭a. Activate the buzzer to alert nearby individuals.‬
‭b. Display a danger warning on the LCD (e.g., “Gas/Flame Detected!”).‬
‭c.‬‭If‬‭ESP32 is configured: send an alert email using the SMTP client.‬
‭5.‬ ‭Else‬‭:‬
‭a. Display “Status: Safe” on the LCD.‬
‭b. Keep the buzzer turned off.‬
‭6.‬ ‭Repeat the process continuously.‬

‭5. Smart Dustbin:-‬

‭Flowchart:-‬
‭Algorithms:-‬

‭1.‬ ‭Initialize system components:‬


‭a.‬ ‭Set up MQ-2 sensor for analog data reading.‬
‭b.‬ ‭Set up KY-026 sensor for digital output.‬
‭c.‬ ‭Initialize buzzer and LCD display.‬
‭d.‬ ‭Set up ESP32 SMTP client for email notifications.‬
‭2.‬ ‭Continuously monitor sensor data:‬
‭a.‬ ‭Continuously read analog data from the MQ-2 sensor to check for‬
‭gas detection.‬
‭b.‬ ‭Check digital output from the KY-026 sensor to detect flames or fire.‬
‭3.‬ ‭Evaluate sensor conditions:‬
‭a.‬ ‭If gas or flame detection threshold is exceeded (based on sensor‬
‭data):‬
‭i.‬ ‭Trigger buzzer to alert of danger.‬
‭ii.‬ ‭Display “Danger Detected!” on the LCD screen.‬
‭iii.‬ ‭Optionally, send an email notification via ESP32 SMTP client‬
‭to a pre-configured address.‬
‭4.‬ ‭Safe state:‬
‭a.‬ ‭If no danger is detected:‬
‭i.‬ ‭Display “Safe” on the LCD.‬
‭ii.‬ ‭Ensure the buzzer is off, maintaining a quiet state.‬
‭5.‬ ‭Repeat monitoring:‬
‭a.‬ ‭Continue reading sensors and checking conditions in real-time for‬
‭updates.‬

‭6. Time and Temperature Display:-‬

‭Flowchart:-‬
‭Algorithms:-‬

‭1.‬ I‭nitialize the DHT22 sensor and DS3231 RTC module.‬


‭2.‬ ‭Fetch the current time from the DS3231 RTC module.‬
‭3.‬ ‭Read temperature and humidity data from the DHT22 sensor.‬
‭4.‬ ‭Format the output to display time, temperature, and humidity on the 16x2 LCD.‬
‭5.‬ ‭Update the display every second with the latest data.‬
‭6.‬ ‭Repeat the process continuously.‬

‭Hardware Components:-‬
‭●‬ ‭Microcontrollers‬‭:-‬
‭Arduino UNO, ESP32‬
‭●‬ ‭Actuators‬‭:-‬
‭Buzzers, Servo Motors, LEDs‬
‭●‬ ‭Sensors‬‭:-‬
‭Ultrasonic, PIR, RCWL-0516, KY-026, MQ-2, DHT22‬
‭●‬ ‭Modules‬‭:-‬
‭DS3231 RTC, 16x2 LCD, 4x4 Keypad, RFID‬

‭Selection of Hardware Components:-‬

‭1. Arduino UNO‬


‭‬ E
● ‭ asy to program using the Arduino IDE‬
‭●‬ ‭Sufficient number of digital and analog pins‬
‭●‬ ‭Strong community support and extensive‬
r‭ esources‬
‭ ‬ ‭Budget-friendly and ideal for beginners‬

‭ESP32:-‬

‭ ‬ I‭ntegrated Wi-Fi and Bluetooth capabilities.‬



‭●‬ ‭Dual-core processor for faster performance‬
t‭han Arduino.‬
‭ ‬ ‭More GPIO pins compared to NodeMCU.‬

‭●‬ ‭Ideal for wireless data transmission and IoT applications.‬

‭Ultrasonic Sensor (HC-SR04):-‬

‭‬
● ‭ rovides precise short-range distance measurement.‬
P
‭●‬ ‭Easy integration with microcontrollers.‬
‭●‬ ‭Non-contact operation, ideal for hygienic applications like dustbins.‬
‭●‬ ‭Low cost and readily available.‬

‭PIR Sensor:-‬

‭‬
● ‭ rovides precise short-range distance measurement.‬
P
‭●‬ ‭Easy microcontroller interfacing.‬
‭●‬ ‭Non-contact operation, ideal for hygiene.‬
‭●‬ ‭Low cost and readily available‬

‭RCWL-0516 Microwave Sensor:-‬


‭‬ C
● ‭ an sense motion through walls/glass‬
‭●‬ ‭More sensitive and wider range than PIR‬
‭●‬ ‭Operates in dark conditions unlike PIR‬

‭4×4 Matrix Keypad:-‬


‭‬ E
● ‭ asy to accept secure numeric input‬
‭●‬ ‭Durable and reusable‬
‭●‬ ‭Small size with 16 buttons (ideal for‬
‭passwords)‬

‭16×2 LCD Display:-‬


‭‬ C
● ‭ lear, simple visual feedback‬
‭●‬ ‭Supports both I2C and parallel interfaces‬
‭●‬ ‭Nice display for small projects‬

‭RFID Module (RC522):-‬


‭‬ S
● ‭ ecure contactless identification‬
‭●‬ ‭Good scanning speed with good range‬
‭●‬ ‭Inexpensive and easy to interface with Arduino‬

‭Servo Motor (SG90 / MG995):-‬

‭‬ P
● ‭ rovides accurate angle rotation (0°–180°).‬
‭●‬ ‭Requires low torque for small doors/lids.‬
‭●‬ ‭Compact and power-efficient design.‬

‭KY-026 Flame Sensor:-‬


‭●‬ F ‭ lame detection through infrared light‬
‭detection‬
‭●‬ ‭Quick response time‬
‭●‬ ‭Cost-effective and small footprint‬

‭MQ-2 Gas Sensor:-‬


‭‬ D
● ‭ etects LPG, methane, smoke, and hydrogen‬
‭●‬ ‭Good sensitivity and low cost‬
‭●‬ ‭Easy to interface with analog/digital pins‬

‭DHT22 Temperature and Humidity Sensor:-‬


‭‬ H
● ‭ igher accuracy and range than DHT11‬
‭●‬ ‭Simple one-wire communication‬
‭●‬ ‭Reliable for indoor climate monitoring‬

‭DS3231 RTC Module:-‬


‭‬ K
● ‭ eeps accurate time even when power is off (has‬ ‭battery)‬
‭●‬ ‭Built-in temperature-compensated crystal‬
‭oscillator‬
‭●‬ ‭Easy to interface with I2C‬

‭Buzzers:-‬
‭ ‬ I‭ssues audible warnings in case of emergency‬

‭●‬ ‭Low power consumption‬
‭●‬ ‭Small and inexpensive‬
‭LED:-‬
‭‬ V
● ‭ isual feedback‬
‭●‬ ‭Simple to use and debug systems‬
‭●‬ ‭Inexpensive and reliable‬

‭Breadboards & Jumper Wires:-‬


‭‬ R
● ‭ eusable for prototyping‬
‭●‬ ‭No soldering required‬
‭●‬ ‭Rapid circuit testing and modification‬

‭Power Supply (9V Adapter, USB, Batteries):-‬


‭‬ D
● ‭ elivers clean power to microcontrollers and peripherals‬
‭●‬ ‭May alternate between portable or continuous supply‬

‭Circuit diagram with connections:-‬


‭Connections for Arduino UNO:-‬

‭Components‬ ‭PIN name‬ ‭Arduino UNO pin‬


‭I2C module‬ ‭SDA‬ ‭A4‬

‭SCL‬ ‭A5‬

‭Flame Sensor‬ ‭Digital pin‬ ‭2‬

‭Sensor pin‬ ‭A0‬

‭Gas sensor‬ ‭Sensor pin‬ ‭A1‬

‭Buzzer‬ ‭-‬ ‭3‬

‭Button‬ ‭-‬ ‭4‬

‭Ultrasonic (dustbin)‬ ‭TRIG‬ ‭5‬

‭ECHO‬ ‭6‬

‭Ultrasonic (parking assist)‬ ‭TRIG‬ ‭9‬

‭ECHO‬ ‭10‬

‭Servo‬ ‭-‬ ‭7‬

‭DHT11 sensor‬ ‭-‬ ‭8‬

‭Connections for esp-32:-‬

‭Components‬ ‭PIN name‬ ‭ESP-32 pin‬


‭RFID reader‬ ‭SDA‬ ‭21‬

‭SCK‬ ‭18‬

‭MOSI‬ ‭23‬

‭MISO‬ ‭19‬

‭RST‬ ‭22‬

‭4x4 matrix Keypad‬ ‭R1‬ ‭13‬

‭R2‬ ‭14‬
‭R3‬ ‭26‬

‭R4‬ ‭27‬

‭C1‬ ‭32‬

‭C2‬ ‭33‬

‭C3‬ ‭25‬

‭C4‬ ‭15‬

‭Servo motor‬ ‭Signal‬ ‭12‬

‭PIR sensor‬ ‭Out‬ ‭17‬

‭Button‬ ‭-‬ ‭34‬

‭LCD‬ ‭SDA‬ ‭4‬

‭SCL‬ ‭2‬

‭Buzzer‬ ‭-‬ ‭16‬

‭Alert LED‬ ‭-‬ ‭5‬

‭Code for Arduino UNO:-‬

‭ define FLAME_SENSOR_PIN A0‬


#
‭#define FLAME_DIGITAL_PIN 2‬

/‭/ MQ-2 Gas Sensor‬


‭#define GAS_SENSOR_PIN A1‬

/‭/ Buzzer and Button‬


‭#define BUZZER_PIN 3‬
‭#define BUTTON_PIN 4‬

/‭/ Ultrasonic Dustbin Sensor‬


‭#include <Wire.h>‬
‭#include <LiquidCrystal_I2C.h>‬
‭#include <DHT.h>‬
‭#include <RTClib.h>‬
‭#include <Servo.h>‬
/‭/ I2C connections‬
‭// SDA - A4‬
‭// SCL - A5‬

/‭/ Flame Sensor (KY-026)‬


‭// DHT11 Sensor‬
‭#define DHTPIN 8‬
‭#define DHTTYPE DHT11‬

/‭/ LCD configuration‬


‭#define LCD_COLS 16‬
‭#define LCD_ROWS 2‬

/‭/ Thresholds‬
‭#define FLAME_THRESHOLD 500‬
‭#define GAS_THRESHOLD 300‬
‭#define DUSTBIN_DISTANCE 15‬
‭#define PARKING_DISTANCE 15‬

/‭/ Objects‬
‭LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);‬
‭DHT dht(DHTPIN, DHTTYPE);‬
‭RTC_DS1307 rtc;‬
‭Servo dustbinServo;‬

/‭/ Variables‬
‭bool alarmActive = false;‬
‭unsigned long lastUpdateTime = 0;‬
‭const unsigned long updateInterval = 1000;‬

‭void setup() {‬
‭Serial.begin(9600);‬

/‭/setupFlameGasSystem(); // Uncomment if needed‬


‭setupSmartDustbin();‬
‭setupTimeTempDisplay();‬
‭setupSmartParking(); // New function‬
‭}‬
‭void loop() {‬
‭//flameGasDetection(); // Uncomment if needed‬
‭smartDustbin();‬
‭timeTempDisplay();‬
‭smartParkingAlert(); // Call the new parking function‬
‭}‬

‭// ---------------- SETUP FUNCTIONS ---------------- //‬

‭void setupSmartDustbin() {‬
‭pinMode(TRIG_PIN, OUTPUT);‬
‭pinMode(ECHO_PIN, INPUT);‬
‭dustbinServo.attach(SERVO_PIN);‬
‭dustbinServo.write(0);‬
‭}‬

‭void setupTimeTempDisplay() {‬
‭Wire.begin();‬
‭lcd.init();‬
‭lcd.backlight();‬
‭dht.begin();‬
‭}‬

‭void setupSmartParking() {‬
‭pinMode(PARKING_TRIG_PIN, OUTPUT);‬
‭pinMode(PARKING_ECHO_PIN, INPUT);‬
‭pinMode(BUZZER_PIN, OUTPUT);‬
‭}‬

‭// ---------------- FEATURE FUNCTIONS ---------------- //‬

‭void smartDustbin() {‬
‭long duration = triggerUltrasonic(TRIG_PIN, ECHO_PIN);‬
‭int distance = duration * 0.034 / 2;‬
‭Serial.print("Dustbin Distance: ");‬
‭Serial.println(distance);‬

‭if (distance < DUSTBIN_DISTANCE && distance != 0) {‬


‭dustbinServo.write(180);‬
‭ elay(3000);‬
d
‭} else {‬
‭dustbinServo.write(0);‬
‭}‬
‭}‬

‭void timeTempDisplay() {‬
‭unsigned long currentMillis = millis();‬
‭if (currentMillis - lastUpdateTime >= updateInterval) {‬
‭lastUpdateTime = currentMillis;‬

f‭loat temp = dht.readTemperature();‬


‭float humidity = dht.readHumidity();‬

l‭cd.clear();‬
‭lcd.setCursor(0, 1);‬
‭if (isnan(temp) || isnan(humidity)) {‬
‭lcd.print("DHT Error");‬
‭} else {‬
‭lcd.print(temp, 1);‬
‭lcd.print("C ");‬
‭lcd.print(humidity, 0);‬
‭lcd.print("%");‬
‭}‬
‭}‬
‭}‬

‭void smartParkingAlert() {‬
‭long duration = triggerUltrasonic(PARKING_TRIG_PIN, PARKING_ECHO_PIN);‬
‭int distance = duration * 0.034 / 2;‬
‭Serial.print("Parking Distance: ");‬
‭Serial.println(distance);‬

‭if (distance < PARKING_DISTANCE && distance > 0) {‬


‭digitalWrite(BUZZER_PIN, HIGH);‬
‭} else {‬
‭digitalWrite(BUZZER_PIN, LOW);‬
‭}‬
‭}‬
‭// ---------------- UTILITY FUNCTION ---------------- //‬

‭long triggerUltrasonic(int trigPin, int echoPin) {‬


‭digitalWrite(trigPin, LOW);‬
‭delayMicroseconds(2);‬
‭digitalWrite(trigPin, HIGH);‬
‭delayMicroseconds(10);‬
‭digitalWrite(trigPin, LOW);‬
‭return pulseIn(echoPin, HIGH);‬
‭}‬
‭#define TRIG_PIN 5‬
‭#define ECHO_PIN 6‬

/‭/ Ultrasonic Parking Sensor‬


‭#define PARKING_TRIG_PIN 9‬
‭#define PARKING_ECHO_PIN 10‬

/‭/ Servo‬
‭#define SERVO_PIN 7‬

‭Code for ESP-32:-‬

/‭*‬
‭Revised Wiring (PIR_1 removed):‬
‭1. RFID Reader (MFRC522)‬
‭SDA -> Pin 21‬
‭SCK -> Pin 18‬
‭MOSI -> Pin 23‬
‭MISO -> Pin 19‬
‭RST -> Pin 22‬
‭VCC -> 3.3V‬
‭GND -> GND‬

‭2. Keypad (4x4 Matrix Keypad)‬


‭Rows -> Pins 13,14,26,27‬
‭Cols -> Pins 32,33,25,15‬
‭3. Servo Motor‬
‭Signal -> Pin 12‬
‭VCC -> 5V‬
‭GND -> GND‬

‭4. PIR Sensor (Only PIR_2 remains)‬


‭VCC -> 5V‬
‭GND -> GND‬
‭OUT -> Pin 17 (formerly PIR_2)‬

‭5. Push Button‬


‭Pin -> 34 (with internal pull-up)‬
‭GND -> GND‬

‭6. LCD (I2C Display)‬


‭SDA -> Pin 4‬
‭SCL -> Pin 2‬
‭VCC -> 5V‬
‭GND -> GND‬

‭7. Buzzer‬
‭Positive -> Pin 16‬
‭Negative -> GND‬

‭8. LED‬
‭Anode -> Pin 4 (with resistor) // NOT NEEDED‬
‭Cathode -> GND‬

‭9. Alert LED‬


‭Anode -> Pin 5 (with resistor)‬
‭Cathode -> GND‬
‭*/‬
‭// Create a separate I2C bus instance for LCD‬
‭//TwoWire I2CLCD = TwoWire(0);‬

/‭/ Remapped I2C Pins for LCD‬


‭#define LCD_SDA_PIN 4‬
‭#define LCD_SCL_PIN 2‬
/‭/ Libraries (unchanged)‬
‭#include <WiFi.h>‬
‭#include <Keypad.h>‬
‭#include <Wire.h>‬
‭#include <LiquidCrystal_I2C.h>‬
‭#include <ESP32Servo.h>‬
‭#include <MFRC522.h>‬
‭#include <ESP_Mail_Client.h>‬
‭#include <mbedtls/md.h>‬
‭#include <time.h>‬

/‭/ WiFi & Mail Credentials (unchanged)‬


‭#define WIFI_SSID "hotspot"‬
‭#define WIFI_PASSWORD "0123456789"‬
‭#define SMTP_server "smtp.gmail.com"‬
‭#define SMTP_Port 587‬
‭#define sender_email "[email protected]"‬
‭#define sender_password "qyjydusgxduqtuey"‬
‭#define Recipient_email "[email protected]"‬
‭#define Recipient_name ""‬

/‭/ Keypad setup (unchanged)‬


‭const byte rows = 4, col = 4;‬
‭char keys[rows][col] = {‬
‭{'1','2','3','A'},‬
‭{'4','5','6','B'},‬
‭{'7','8','9','C'},‬
‭{'*','0','#','D'}‬
‭};‬
‭byte rowPins[rows] = {13,14,26,27};‬
‭byte colPins[col] = {32,33,25,15};‬
‭Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, col);‬

/‭/ RFID setup (unchanged)‬


‭#define RST_PIN 22‬
‭#define SS_PIN 21‬
‭MFRC522 mfrc522(SS_PIN, RST_PIN);‬

/‭/ Other Components (unchanged)‬


‭Servo myServo;‬
‭ iquidCrystal_I2C lcd(0x27, 16, 2);‬
L
‭SMTPSession smtp;‬

/‭/ Pins (PIR_1 removed)‬


‭const int servoPin = 12;‬
‭const int pirPin = 17; // Only PIR now (formerly PIR_2)‬
‭//const int ledPin = 4, buzzerPin = 16;‬
‭const int buzzerPin = 16;‬
‭const int pushButtonPin = 34, alertLedPin = 5;‬

/‭/ State Variables (unchanged)‬


‭bool locked = true;‬
‭char enteredCode[5];‬
‭int codeIndex = 0;‬
‭int incorrectAttempts = 0;‬
‭bool lockoutActive = false;‬
‭bool pushButtonState = false, previousPushButtonState = false;‬

/‭/ Password Hash (unchanged)‬


‭String storedHash =‬
‭"03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4";‬

/‭/ Helper functions (unchanged except PIR references)‬


‭String removeSpaces(String str) {‬
‭str.replace(" ", "");‬
‭return str;‬
‭}‬

‭void connectWiFi() {‬
‭Serial.print("Connecting to WiFi...");‬
‭WiFi.begin(WIFI_SSID, WIFI_PASSWORD);‬
‭int attempts = 0;‬
‭while (WiFi.status() != WL_CONNECTED && attempts < 20) {‬
‭Serial.print(".");‬
‭delay(500);‬
‭attempts++;‬
‭}‬
‭if (WiFi.status() == WL_CONNECTED) {‬
‭Serial.println("\nWiFi connected. IP: " + WiFi.localIP().toString());‬
‭} else {‬
‭ erial.println("\nFailed to connect to WiFi.");‬
S
‭}‬
‭}‬

‭void initializeHardware() {‬
‭myServo.attach(servoPin);‬
‭// Initialize custom I2C bus for LCD‬
‭//I2C LCD.begin(LCD_SDA_PIN, LCD_SCL_PIN);‬
‭Wire.begin(LCD_SDA_PIN, LCD_SCL_PIN);‬
‭lcd.begin(16,2);‬
‭lcd.backlight();‬
‭pinMode(pirPin, INPUT); // Only one PIR now‬
‭//pinMode(ledPin, OUTPUT);‬
‭pinMode(buzzerPin, OUTPUT);‬
‭pinMode(pushButtonPin, INPUT_PULLUP);‬
‭pinMode(alertLedPin, OUTPUT);‬

‭ PI.begin();‬
S
‭mfrc522.PCD_Init();‬
‭delay(4); // Short delay for RFID init‬
‭}‬

/‭/ Lock/unlock functions (unchanged)‬


‭void updateLockStatus() {‬
‭lcd.clear();‬
‭lcd.print(locked ? "Door Locked" : "Door Unlocked");‬
‭delay(500);‬
‭if (locked) {‬
‭lcd.clear();‬
‭lcd.print("Enter Code:");‬
‭}‬
‭}‬

‭void unlockDoor() {‬
‭locked = false;‬
‭myServo.write(90);‬
‭updateLockStatus();‬
‭}‬

‭void lockDoor() {‬
l‭ocked = true;‬
‭myServo.write(0);‬
‭updateLockStatus();‬
‭}‬

/‭/ SHA256 function (unchanged)‬


‭String calculateSHA256(String input) {‬
‭unsigned char hash[32];‬
‭mbedtls_md_context_t ctx;‬
‭mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;‬

‭ bedtls_md_init(&ctx);‬
m
‭mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0);‬
‭mbedtls_md_starts(&ctx);‬
‭mbedtls_md_update(&ctx, (const unsigned char*)input.c_str(), input.length());‬
‭mbedtls_md_finish(&ctx, hash);‬
‭mbedtls_md_free(&ctx);‬

‭ tring hashString = "";‬


S
‭for (int i = 0; i < 32; i++) {‬
‭if (hash[i] < 16) hashString += "0";‬
‭hashString += String(hash[i], HEX);‬
‭}‬
‭return hashString;‬
‭}‬

/‭/ Email function (unchanged)‬


‭void sendMessage() {‬
‭smtp.debug(1);‬
‭ESP_Mail_Session session;‬
‭session.server.host_name = SMTP_server;‬
‭session.server.port = SMTP_Port;‬
‭session.login.email = sender_email;‬
‭session.login.password = sender_password;‬
‭session.login.user_domain = "";‬

‭ MTP_Message message;‬
S
‭message.sender.name = "ESP32";‬
‭message.sender.email = sender_email;‬
‭message.subject = "ESP32 Security Alert";‬
‭message.addRecipient(Recipient_name, Recipient_email);‬

‭ essage.text.content = "Alert!!! Intruder detected.";‬


m
‭message.text.charSet = "us-ascii";‬
‭message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;‬

i‭f (!smtp.connect(&session)) return;‬


‭if (!MailClient.sendMail(&smtp, &message)) return;‬
‭}‬

/‭/ Keypad handling (unchanged)‬


‭void handleKeypadInput(char key) {‬
‭if (locked) {‬
‭if (key == '#' && codeIndex > 0) {‬
‭enteredCode[codeIndex] = '\0';‬
‭String hash = calculateSHA256(enteredCode);‬
‭if (hash == storedHash) {‬
‭unlockDoor();‬
‭incorrectAttempts = 0;‬
‭} else {‬
‭incorrectAttempts++;‬
‭lcd.clear();‬
‭lcd.print("Incorrect Pin!");‬
‭delay(2000);‬
‭if (incorrectAttempts > 2) lockoutActive = true;‬
‭if (!lockoutActive) updateLockStatus();‬
‭}‬
‭memset(enteredCode, 0, sizeof(enteredCode));‬
‭codeIndex = 0;‬
‭} else if (key == 'C' && codeIndex > 0) {‬
‭codeIndex--;‬
‭enteredCode[codeIndex] = '\0';‬
‭lcd.setCursor(codeIndex, 1);‬
‭lcd.print(" ");‬
‭} else if (key != '#' && key != 'C' && codeIndex < sizeof(enteredCode) - 1) {‬
‭enteredCode[codeIndex] = key;‬
‭lcd.setCursor(codeIndex, 1);‬
‭lcd.print('*');‬
‭codeIndex++;‬
‭}‬
‭} else if (key == '*') {‬
‭lockDoor();‬
‭}‬
‭}‬

/‭/ RFID handling (unchanged)‬


‭void handleRFID() {‬
‭if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {‬
‭String rfidUID = "";‬
‭for (byte i = 0; i < mfrc522.uid.size; i++) {‬
‭rfidUID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");‬
‭rfidUID += String(mfrc522.uid.uidByte[i], HEX);‬
‭}‬
‭rfidUID.toUpperCase();‬
‭rfidUID = removeSpaces(rfidUID);‬

‭ tring authorizedRFID1 = "D7DAC101";‬


S
‭String authorizedRFID2 = "11DD3B03";‬

‭if (rfidUID == authorizedRFID1 || rfidUID == authorizedRFID2) {‬


‭unlockDoor();‬
‭} else {‬
‭lcd.clear();‬
‭lcd.print("Unauthorized RFID!");‬
‭delay(2000);‬
‭updateLockStatus();‬
‭}‬
‭mfrc522.PICC_HaltA();‬
‭}‬
‭}‬

/‭/ Push button handling (updated for single PIR)‬


‭void handlePushButton() {‬
‭if (digitalRead(pushButtonPin) == LOW) {‬
‭delay(50);‬
‭if (!previousPushButtonState) {‬
‭pushButtonState = !pushButtonState;‬
‭previousPushButtonState = true;‬
‭}‬
‭} else {‬
‭ reviousPushButtonState = false;‬
p
‭}‬

‭ igitalWrite(alertLedPin, pushButtonState);‬
d
‭if (pushButtonState && digitalRead(pirPin) == HIGH) {‬
‭digitalWrite(buzzerPin, HIGH);‬
‭sendMessage();‬
‭} else {‬
‭digitalWrite(buzzerPin, LOW);‬
‭}‬
‭}‬

‭void setup() {‬
‭Serial.begin(115200);‬
‭connectWiFi();‬
‭initializeHardware();‬
‭updateLockStatus();‬
‭}‬

‭void loop() {‬
‭handleRFID();‬
‭char key = keypad.getKey();‬
‭if (key) handleKeypadInput(key);‬
‭handlePushButton();‬
‭}‬

‭Result:-‬

‭ he Smart Home Automation System performed seamlessly, with all modules operating‬
T
‭as intended. Features such as distance sensing, motion detection, door locking, fire and‬
‭gas alerts, automatic dustbin operation, and real-time time-temperature display were‬
‭fully functional. The system proved to be efficient, dependable, and well-suited for smart‬
‭home applications.‬

‭Conclusion:-‬

‭ he Smart Home Automation System successfully integrated six key modules‬


T
‭into a cohesive solution, enhancing home safety, cleanliness, and convenience.‬
‭ ach subsystem—car parking assistance, intruder alarm, smart lock, fire and gas‬
E
‭detection, smart dustbin, and environmental monitoring—functioned reliably and‬
‭efficiently. By leveraging sensors and microcontrollers, the system provided‬
‭real-time responses, making it highly suitable for modern homes and adaptable‬
‭to diverse needs.‬

‭References:-‬

‭1.‬ ‭Arduino Official Documentation:‬‭https://www.arduino.cc‬

‭2.‬ ‭Espressif ESP32 Guide:‬‭https://docs.espressif.com‬

‭3.‬ ‭Datasheets for MQ-2, KY-026, RCWL-0516, DHT22 sensors.‬

‭4.‬ ‭Research papers on IoT-enabled Smart Home Systems.‬

You might also like