SMART HOME AND SECURITY SYSTEM
by Robotics club of CEG in Circuits > Arduino
1849 Views, 4 Favorites, 0 Comments
SMART HOME AND SECURITY SYSTEM
In this rapidly growing world, using technology to create a smart home that ensures both safety and power efficiency is becoming a primary demand. Home Automation and Security system is an IoT based project wherein the electric appliances and devices like air conditioners, lights, door locks and gates can be controlled and monitored remotely. The Internet of things (IOT) refers to embedding things with sensors, software and other technologies over internet for data exchange. This project focuses on developing a smart home that embodies smart door system; controlling the appliances in a room from the smart phone; face detection using camera; and smoke and LPG detection. It sends notifications about detection of smoke and gas leakage. The security is ensured by surveilling the entry points like doors in case of any suspicion or breach, the owner is notified with the image captured by the sensor at the instant. The proposed system is designed using Wi-Fi as an Internet connection protocol. Android app is used to control the home appliances.
​COMPONENTS REQUIRED:
1. ESP 32 CAM Module
2. Arduino UNO
3. 4-channel relay module
4. MQ-2 Sensor
5. REED Sensor
6. PIR Sensor
7. DC motor
The android application used for controlling the smart home devices is BLYNK.
HAAR algorithm is used for face detection.
Hardware Assembly
Programming the Node MCU
Connect the node MCU to laptop/computer and upload the code.
DATABASE:
Database for Faces: This program asks for a person to provide a unique id. Then it collects 30 pictures of faces of the person and then converts it to grayScale. It saves all the data of that person in database or updates if existing. Here cv2 is the openCV library and the database use is sqlite3.The cascade classifier haarcascade_frontalface_default.xml is used for face recognising system.
TRAINER
In this part the program trains the faces and saves the data in a .yml format.
CODE:
import os
import cv2
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create();
path='dataSet'
def getImagesWithID(path):
imagepaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagepath in imagepaths:
faceImg=Image.open(imagepath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagepath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("training",faceNp)
cv2.waitKey(10)
return np.array(IDs),faces
IDs,faces=getImagesWithID(path)
recognizer.train(faces,IDs)
recognizer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
RECOGNITION
This part takes photos of faces from the url,converts them to grayScale format,then it compares the photos with the photos from database.
import cv2
import urllib.request
import numpy as np
import sqlite3
import serial
faceDetect=cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml');
url = "http://*ipaddress*/cam-lo.jpg"
rec=cv2.face_LBPHFaceRecognizer.create();
cv2.namedWindow("Face",cv2.WINDOW_AUTOSIZE)
rec.read("recognizer/trainningData.yml")
font = cv2.FONT_HERSHEY_SIMPLEX
ser = serial.Serial('COM*',115200)
ser.baudrate = 115200
ser.port = str("COM*")
def getProfile(id):
conn=sqlite3.connect("FaceBase")
cmd="SELECT * FROM People WHERE Id="+str(id)
cursor=conn.execute(cmd)
profile=None
for row in cursor:
profile=row
conn.close()
return profile
while(True):
imgResponse = urllib.request.urlopen(url)
imgnp = np.array(bytearray(imgResponse.read()),dtype=np.uint8)
img=cv2.imdecode(imgnp,-1)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
profile=getProfile(id)
if(profile!=None):
cv2.cv.PutText(cv2.cv.fromarray(img),"Hello Subject : "+str(profile[1]),(x,y+h+20),font,(0,255,0));
ser.write('y')
else:
ser.write('n')
cv2.imshow("Face",img);
if(cv2.waitKey(1)==ord('q')):
break;
cv2.destroyAllWindows()
Working on ARDUINO
This program is to be uploaded in the Arduino. If motion is detected by the pir sensor, the camera turns on. If the camera finds a match, the motor trigger pin turns on and the motor rotates. The motor here is for the door lock system.
#define pir_Pin 2
#define Motor_L 10
#define Motor_R 11
#define Motor_Trig 12
#define CAM 8
int pirStat = 0;
long prevMillis = 0;
int interval = 60000;
void setup() {
// put your setup code here, to run once:
pinMode(pir_Pin, INPUT);
pinMode (Motor_Trig, INPUT);
pinMode (Motor_L, OUTPUT);
pinMode (Motor_R, OUTPUT);
delay(20000);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
pirStat = digitalRead(pir_Pin);
if(pirStat == HIGH){
digitalWrite(CAM,HIGH);
Serial.println("Turning On Camera");
prevMillis = millis();
}
if ( millis() - prevMillis > interval){
digitalWrite(CAM,LOW);
}
if(digitalRead(Motor_Trig)== HIGH){
digitalWrite(Motor_L,HIGH);
digitalWrite(Motor_R,LOW);
delay(750);
digitalWrite(Motor_L,LOW);
digitalWrite(Motor_R,LOW);
delay(60000);
digitalWrite(Motor_L,LOW);
digitalWrite(Motor_R,HIGH);
delay(750);
digitalWrite(Motor_L,LOW);
digitalWrite(Motor_R,LOW);
delay(60000);
}
}
Setting Up Blynk App
Create a Blynk Account and create a New Project. Choose the hardware. An authentication token will be sent to your email id for every new project created. Create the project and add the widgets as shown
CONCLUSION
In this project, we have developed a system through which the user can control their household appliances through an android based application. The android-based smart home application communicates with a Wi-Fi module which acts as an access point. Using android application user could control and monitor the smart home environment. This system can be used to communicate with many numbers of devices. It minimizes the wastage of electricity and it consumes less time, also it helps the old aged and dis-abled people in doing the basic domestic works on their own. This also gives users the ability to automate their home without the need to buy expensive smart appliances.