Project Details:

Fundamental parts of the project

Continuation

Hardware Requirements:

  1. Raspberry PI
  2. PIR sensor
  3. DHT11 sensor
  4. Passive Buzzer
  5. Camera
  6. Jumper cables

Circuit Images:

Poster:

Capture

Python code for the project:

DHT11:

import sys
from pymongo import MongoClient
import Adafruit_DHT
client = MongoClient(‘mongodb://harsha:harsha@ds119081.mlab.com:19081/security’)
mydb=client[‘security’]
import datetime
import time
# Parse command line parameters.
sensor_args = { ’11’: Adafruit_DHT.DHT11,
’22’: Adafruit_DHT.DHT22,
‘2302’: Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print(‘usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#’)
print(‘example: sudo ./Adafruit_DHT.py 2302 4 – Read from an AM2302 connected to GPIO #4′)
sys.exit(1)
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
x='{0:0.1f}* humidity={1:0.1f}%’.format(temperature,humidity)
print(‘Temp={0:0.1f}* Humidity={1:0.1f}%’.format(temperature, humidity))
else:
print(‘Failed to get reading. Try again!’)
sys.exit(1)

temperature=int(x[0]+x[1])
myrecord = {
“user_id”:”alla.sriharsha94@gmail.com”,
“temp”: temperature,
“date” : datetime.datetime.now()
}
record_id = mydb.temp.insert(myrecord)
#time.sleep(600)

Explaination:

DHT11.py program will enable raspberry PI to connect to the MongoDB in the cloud and transfer temperature of the room in to the database for each and every 10 minutes(600 seconds).

Intrusion:

main.py

from threading import Thread
import os
def security():
os.system(‘python /home/pi/project/dbsr.py’)
def temperature():
os.system(‘python /home/pi/project/test/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 04’)
def loop():
thread_sec=Thread(target=temperature)
thread_sec.start()
thread_temp=Thread(target=security)
thread_temp.start()
def destroy():
GPIO.cleanup()
if __name__ == ‘__main__’: # Program start from here
print ‘Press Ctrl+C to end the program…’
try:
loop()
except KeyboardInterrupt: # When ‘Ctrl+C’ is pressed, the child progra$
destroy()

Explaination:

Main.py program runs the components of both intrusion and temperature sensings systems parallel. In order to do so, threads are used.

DBSR.py

from pymongo import MongoClient
import os
client = MongoClient(‘mongodb://harsha:harsha@ds119081.mlab.com:19081/security’)
mydb = client[‘security’]

object= mydb.intrusion.find({‘user_id’:’alla.sriharsha94@gmail.com’})
for obj in object:
#print obj[‘user_id’]
if obj[‘activation’]==1:
os.system(‘python /home/pi/project/pir.py’)
object= mydb.intrusion.find({‘user_id’:’alla.sriharsha94@gmail.com’})

Explaination:

DBSR.py program will check each and every time that security system is turned on or not. It verifies the values of the ‘activation’ column for the user ‘alla.sriharsha94@gmail.com’.

PIR.py

from threading import Thread
import os
from gpiozero import Buzzer
import RPi.GPIO as GPIO
import time

def setup():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
def buzzer():
os.system(‘python /home/pi/project/buz.py’)
def mail():
os.system(‘python /home/pi/project/mail.py’)

def telecast():
os.system(‘raspivid -o – -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i – -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/f0gc-d3xd-ah6z-4xwq’)
def destroy():
GPIO.cleanup()
def loop():
while True:
i=GPIO.input(11)
if i==0:
time.sleep(1)
elif i==1:
print “Intruder detected”
thread1=Thread(target=buzzer)
thread1.start()
os.system(‘python /home/pi/project/camera.py’)
os.system(‘python /home/pi/project/mail.py’)
os.system(‘raspivid -o – -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i – -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/f0gc-d3xd-ah6z-4xwq’)
time.sleep(1)

if __name__ == ‘__main__’: # Program start from here
print ‘Press Ctrl+C to end the program…’
setup()
try:
loop()
except KeyboardInterrupt: # When ‘Ctrl+C’ is pressed, the child progra$
destroy()

Explaination:

PIR.py program will take care of PIR sensor and camera. When PIR sensor detects the motion camara.py, buz.py, mail.py and a new thread will execute live telecast to the smart home system’s youtube channel. New thread is required for the live telecast because my intension is to create an independent system.

Buzzer:

import RPi.GPIO as GPIO
import time

beep=12
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(beep,GPIO.OUT)
GPIO.output(beep,GPIO.HIGH)
def loop():
count=0
#while count<1000:
while True:
GPIO.output(beep,GPIO.LOW)
time.sleep(0.001)
GPIO.output(beep,GPIO.HIGH)
time.sleep(0.001)
count+=1
def destroy():
GPIO.output(beep, GPIO.HIGH) # beep off
GPIO.cleanup()
if __name__ == ‘__main__’: # Program start from here
print ‘Press Ctrl+C to end the program…’
setup()
try:
loop()
except KeyboardInterrupt: # When ‘Ctrl+C’ is pressed, the child progra$
destroy()

Camera:

import picamera
camera=picamera.PiCamera()
camera.capture(‘image1.jpg’)
camera.capture(‘image2.jpg’)
camera.capture(‘image3.jpg’)
camera.capture(‘image4.jpg’)
camera.capture(‘image5.jpg’)

Mail.py:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = “alert.yourhome@gmail.com”
toaddr = “alla.sriharsha94@gmail.com”
msg = MIMEMultipart()
msg[‘From’] = fromaddr
msg[‘To’] = toaddr
msg[‘Subject’] = “For live, proceed to following link”
body = “https://www.youtube.com/channel/UCELoEYg2Jz63d-s7HFsewkg&#8221;
msg.attach(MIMEText(body, ‘plain’))

filenames = (‘/home/pi/project/image1.jpg’,’/home/pi/project/image2.jpg’,’/home/pi/project/image3.jpg’,’/home/pi/project/image4.jpg’,’/home/pi/project/image5.jpg’)

for file in filenames:
part = MIMEBase(‘application’, ‘octet-stream’)
part.set_payload(open(file,’rb’).read())
encoders.encode_base64(part)
part.add_header(‘Content-Disposition’, “attachment; filename= %s” % file)

msg.attach(part)

server = smtplib.SMTP(‘smtp.gmail.com’, 587)
server.starttls()
server.login(fromaddr, “4848026296”)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Explaination:

I have used MIME protocol to send the images of the inturder to the user through the email.

 

Results:

Video Demonstration:

Software Link:

https://mlab.com

Cloud Database

https://github.com/allaharsha/IoT

This is the GitHub repository for the project.

 

Conclusion:

In conclusion, Home automation system will enable the user to lead an easy and secure life. As the system can integrate new components, it is easy for the developer to add without changing existing system much.

Personal Insight and Future work:

I would like to implement with other sensors and integrate with the system. All components will be working independently. Temperatures given by DHT11 must be saved in the cloud. And these values can be used to analyze. Based on the analysis, we can predict the desired temperature of the user.

References:

Buzzer

PiCamera

Email

PIR sensor

DHT11

MongoDB

Advertisements