-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.py
70 lines (60 loc) · 1.63 KB
/
alarm.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
#!/usr/bin/python
import urllib2
import piface.pfio as pfio
from datetime import datetime
from time import sleep
#connect to mysql
#db = MySQLdb.connect(host="localhost", # your host, usually localhost
# user="root", # your username
# passwd="", # your password
# db="delano") # name of the database
# Cursor object for using mysql.
# cur = db.cursor()
def notify(sensor, status):
# cur = db.cursor()
# now = datetime.now()
print sensor + ' ' + status
# try:
# cur.execute("""INSERT INTO alarm (time, sensor, status) VALUES (%s, %s, %s)""", (now, sensor, status))
# db.commit()
# except:
# print "DB WRITE ERROR"
# db.rollback()
## We used to send to a web service, leving this here for reference later - if we want to do away with mysql
script_path = "http://10.0.2.2:8001/alarm/update/%s/%s/" % (str(sensor), str(status))
try:
rt=urllib2.urlopen(script_path)
except urllib2.HTTPError:
print "HTTP Error"
return False
# cur.close()
pfio.init()
prev_patio=None
prev_entry=None
prev_motion=None
while(1):
patio=pfio.digital_read(4)
data=[patio]
if patio != prev_patio:
if data[0]==0:
notify('patio','open')
if data[0]==1:
notify('patio','closed')
prev_patio = patio
entry=pfio.digital_read(5)
data=[entry]
if entry != prev_entry:
if data[0]==0:
notify('entry','open')
if data[0]==1:
notify('entry','closed')
prev_entry = entry
motion=pfio.digital_read(6)
data=[motion]
if motion != prev_motion:
if data[0]==0:
notify('motion','detected')
if data[0]==1:
notify('motion','none')
prev_motion = motion
sleep(.2)