-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.py
executable file
·54 lines (46 loc) · 1.61 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
import time
import RPi.GPIO as io
import requests;
import ConfigParser
import os
#!/usr/bin/python
print("Alarm monitor started")
# load config
config = ConfigParser.ConfigParser()
config.readfp(open(os.path.dirname(os.path.realpath(__file__)) + '/config.txt'))
pushoverUser = config.get('pushover', 'user')
pushoverApplication = config.get('pushover','application')
# GPIO configuration
io.setmode(io.BCM)
door_pin = 23
window_pin = 24
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp
io.setup(window_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp
# pushover configuration
def notifyPushover(eventName,pushoverApplication,pushoverUser):
message = 'Office ' + eventName
response = requests.post('https://api.pushover.net/1/messages.json',data={'token':pushoverApplication,'user':pushoverUser,'message':message,'title':'Office Alarm'})
print(response)
# assume the door is open so we don't get a false alarm on startup
doorIsOpen = True;
windowIsOpen = True;
# poll the pins to see if they have changed
while True:
# check the door
if io.input(door_pin):
if not doorIsOpen:
print("Door Opened")
notifyPushover("DoorOpened",pushoverApplication,pushoverUser)
doorIsOpen = True
else:
doorIsOpen = False
# check the window
if io.input(window_pin):
if not windowIsOpen:
print("Window Opened")
notifyPushover("WindowOpened",pushoverApplication,pushoverUser)
windowIsOpen = True
else:
windowIsOpen = False
# wait for next poll interval
time.sleep(0.5)