/
home_security.py
86 lines (72 loc) · 2.45 KB
/
home_security.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import time
import RPi.GPIO as GPIO
from subprocess import call
import string
import smtplib
GPIO.setwarnings (False)
GPIO.setmode (GPIO.BOARD)
time_stamp = time.time() #for debouncing
#set pins
#pin 11 = motion sensor
GPIO.setup (11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#pin 13 = magnetic sensor
GPIO.setup (13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#pin 15 = limit switch
GPIO.setup (15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#pin 19 = pressure switch
GPIO.setup (19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def take_pic(sensor):
call([“raspistill -o image” + sensor + “.jpg”], shell=True)
time.sleep(0.5) #wait 1/2 second for pic to be taken before continuing
def send_text(details):
HOST = “smtp.gmail.com”
SUBJECT = “Break-in!”
TO = “xxxxxxxxxx@txt.att.net”
FROM = “python@mydomain.com”
text = details
BODY = string.join((“From: %s” % FROM, “To: %s” % TO, “Subject: %s” % SUBJECT, “”, text), “\r\n”)
s = smtplib.SMTP(‘smtp.gmail.com’,587)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.login(“username@gmail.com”, “mypassword”)
s.sendmail(FROM, [TO], BODY)
s.quit()
def motion_callback(channel):
global time_stamp
time_now = time.time()
if (time_now - time_stamp) >= 0.3: #check for debouncing
print “Motion detector detected.”
send_text(“Motion detector”)
take_pic(“motion”)
time_stamp = time_now
def limit_callback(channel):
global time_stamp
time_now = time.time()
if (time_now - time_stamp) >= 0.3: #check for debouncing
print “Limit switch pressed.”
send_text(“Limit switch”)
take_pic(“limit”)
time_stamp = time_now
def magnet_callback(channel):
global time_stamp
time_now = time.time()
if (time_now - time_stamp) >= 0.3: #check for debouncing
print “Magnetic sensor tripped.”
send_text(“Magnetic sensor”)
take_pic(“magnet”)
time_stamp = time_now
#main body
raw_input(“Press enter to start program\n”)
GPIO.add_event_detect(11, GPIO.RISING, callback=motion_callback)
GPIO.add_event_detect(13, GPIO.RISING, callback=magnet_callback)
GPIO.add_event_detect(15, GPIO.RISING, callback=limit_callback)
# pressure switch ends the program
# you could easily add a unique callback for the pressure switch
# and add another switch just to turn off the network
try:
print “Waiting for sensors...”
GPIO.wait_for_edge(19, GPIO.RISING)
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()