-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_door.py.1
92 lines (87 loc) · 2.3 KB
/
safe_door.py.1
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
87
88
89
90
91
92
#!/usr/bin/python
import time
import smtplib
import RPi.GPIO as io
def door_is_open():
# global door_open
open_for = time.time() +30
while (io.input(door_pin) == 1):
if time.time() > open_for:
# alert_still_open()
print "alert_still_open()"
print "The timeout of %s was reached." % open_for
break
else:
time.sleep(.05)
print "Door still open"
# else:
# door_open = 0
# print "Door Closed again"
def alert_opened():
sender = "[email protected]"
recipient = "[email protected]"
cc = "[email protected]"
email_subject = "SAFE ALART - DOOR OPEN"
email_body = "I've detected that the door of the safe is OPEN!"
headers = "\r\n".join([
"from: " + sender,
"subject: " + email_subject,
"to: " + recipient,
"cc: " + cc,
"mime-version: 1.0",
"content-type: text/html"])
toaddrs = [recipient, cc]
content = headers + "\r\n\r\n" + email_body
try:
session = smtplib.SMTP('smtp.gmail.com',587)
session.set_debuglevel(0)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'vygqvutorvirnpsa')
session.sendmail(sender,toaddrs,content)
session.quit()
except smtplib.SMTPException:
print('Error')
return;
def alert_still_open():
sender = "[email protected]"
recipient = "[email protected]"
cc = "[email protected]"
email_subject = "SAFE ALART - DOOR LEFT OPEN!"
email_body = "I've detected that the door of the safe has been open for longer than 5 minutes"
headers = "\r\n".join([
"from: " + sender,
"subject: " + email_subject,
"to: " + recipient,
"cc: " + cc,
"mime-version: 1.0",
"content-type: text/html"])
toaddrs = [recipient, cc]
content = headers + "\r\n\r\n" + email_body
try:
session = smtplib.SMTP('smtp.gmail.com',587)
session.set_debuglevel(0)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'vygqvutorvirnpsa')
session.sendmail(sender,toaddrs,content)
session.quit()
except smtplib.SMTPException:
print('Error')
return;
io.setmode(io.BCM)
pir_pin = 18
door_pin = 23
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp
while True:
if io.input(door_pin):
# print("DOOR ALARM!")
time.sleep(.5)
# door_open = 1
print "The door opened"
# alart_opened()
# break
door_is_open()
time.sleep(.001)