-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_door.py.new
118 lines (108 loc) · 3.61 KB
/
safe_door.py.new
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
import time, requests, logging, RPi.GPIO as io, Adafruit_DHT as dht, httplib, urllib
from multiprocessing import Process
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
door = ""
still_open = ""
temperature = ""
humidity = ""
def alert(door_state):
request_url = "https://api.mailgun.net/v3/mg.kansashunter.org/messages"
api_key = "key-e7a8070aaf3649a7494f8333edf04e1b"
fromaddr = "Safe Monitor <[email protected]>"
toaddr = ["[email protected]", "[email protected]"]
open_body = "The door to the safe has been OPENED!"
still_open_body = "The door to the safe has been OPENED \
for longer than the TIMEOUT!"
logging.captureWarnings(True)
if (door_state == "open"):
return requests.post(
request_url,
auth=("api", api_key),
data={"from": fromaddr,
"to": toaddr,
"subject": "SAFE ALART",
"text": open_body})
elif (door_state == "still_open"):
return requests.post(
request_url,
auth=("api", api_key),
data={"from": fromaddr,
"to": toaddr,
"subject": "SAFE ALART",
"text": still_open_body})
#def update_thing():
# thing_url = "https://api.thingspeak.com"
# thing_api_key = "WXM1DA1C87VPHTIZ"
# return requests.post(
# thing_url,
def update_thing():
global temperature
global humidity
#params = urllib.urlencode({'field1': temperature, 'key' : 'WXM1DA1C87VPHTIZ'})
params = urllib.urlencode({'field1': temperature, 'field2' : humidity, 'key' : 'WXM1DA1C87VPHTIZ'})
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPSConnection("api.thingspeak.com:443")
#try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print temperature, humidity
print response.status, response.reason
data = response.read()
conn.close()
#except:
# print "connection failed"
def door_is_open():
global still_open
open_for = time.time() +5
while (io.input(door_pin) == 1):
if time.time() > open_for:
#alert("still_open")
print "alert_still_open"
still_open = "yes"
break
else:
time.sleep(1)
print "Door still open"
door = "closed"
def monitor_door():
global still_open
global door
while True:
if io.input(door_pin) == 0:
door = "closed"
still_open = ""
time.sleep(.01)
elif io.input(door_pin) == 1:
door = "open"
if door == "open":
if still_open == "yes":
door_is_open()
elif still_open == "":
print "open"
door_is_open()
time.sleep(.1)
def monitor_temp():
global temperature
global humidity
high_temp = 32
high_humidity = 30
dht_pin = 4
while True:
humidity, raw_temp = dht.read_retry(dht.DHT22, dht_pin)
#temperature = 9.0/5.0 * raw_temp +32
temperature = round( 9.0/5.0 * raw_temp +32, 1)
if humidity is not None and raw_temp is not None:
print 'Temp={0:0.1f}*F Humidity={1:0.1f}%'.format(temperature, humidity)
update_thing()
else:
print 'Failed to get reading. Try again!'
time.sleep(300)
if __name__ == '__main__':
p1 = Process(target=monitor_temp)
p2 = Process(target=monitor_door)
p1.start()
p2.start()