-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalarm.py
132 lines (103 loc) · 4.15 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
# Imports
import webiopi
import sys,os
import subprocess
import time
import signal
import RPi.GPIO as GPIO
from config import config
armed_file = config['home_alarm_git_dir'] + "/armed.txt"
trigger_file = config['home_alarm_git_dir'] + "/trigger.txt"
send_script = config['rf433_dir'] + "/send "
rf_sniffer_script = config['rf433_dir'] + "/RFSniffer"
print "Clearing trigger file"
os.system("sudo echo -n 0 > "+ trigger_file)
#initialize variables
proc_start = 0
mail_sent = 0
siren_triggered =0
#GPIO used in program
led = 26 #gpio 7
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Setup GPIO Direction
GPIO.setup(led, GPIO.OUT)
GPIO.output(led,GPIO.LOW)
#Function to be used by LED
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(1)
return
def kill_child_process():
print "Killing RFSniffer process pid %s"%p0.pid
os.killpg(p0.pid,signal.SIGTERM)
if (config['webcam_motion_enable']):
print "Killing Motion process pid %s"%p1.pid
os.killpg(p1.pid,signal.SIGTERM)
while True:
try:
#open armed.txt and check current status
with open(armed_file, "r") as fo:
fo.seek(0, 0)
armed_status = fo.read(1)
fo.closed
#print "Armed status: " + str(armed_status)
time.sleep(1)
#if armed, start RF sniffer process if not already started. Check for trigger to sound alarm.
if (armed_status == "1"):
if (proc_start == 0):
#Blink 2 times to indicate arm
for i in range(0,2):
blink(led)
#Start switch 3 - Webcam will be connected to this
if (config['webcam_motion_enable']):
os.system("sudo "+ send_script + config['switch3'])
#Allow some delay to leave home - 1 min here
time.sleep(60)
p0 = subprocess.Popen(["sudo",rf_sniffer_script],preexec_fn=os.setsid)
if (config['webcam_motion_enable']):
p1 = subprocess.Popen(["sudo","motion","-n"],preexec_fn=os.setsid)
proc_start = 1
with open(trigger_file, "r") as fo1:
fo1.seek(0, 0)
trigger_status = fo1.read(1)
fo1.closed
if (trigger_status == "1"):
#Allow some time (45 sec) to disarm the system
time.sleep(45)
print "Trigger recvd. Sound the alarm!!!"
#Trigger siren if not already done
if (siren_triggered == 0):
if (config['enable_gtalk_message']):
print("Sending gtalk message")
os.system("sudo python " + config ['send_gtalk_message'])
os.system("sudo " + send_script + config['siren_enable'])
siren_triggered =1
#Send an email if not already sent
if (mail_sent == 0):
subprocess.call(["bash","intruder_mail.sh"])
mail_sent = 1
#Else if system is disarmed
elif (armed_status == "0"):
#Do below only if system was armed before
if (proc_start == 1):
#Turn off siren
os.system("sudo " + send_script + config['siren_disable'])
#Clear trigger for next run
os.system("sudo echo -n 0 > " + trigger_file)
mail_sent = 0
kill_child_process()
#Stop switch 3 - Webcam will be connected to this
os.system("sudo " + send_script + config['switch3'])
proc_start = 0
#Blink 4 times to indicate disarm
for i in range(0,4):
blink(led)
except KeyboardInterrupt:
print "Program ended by user\n"
kill_child_process()
break
GPIO.cleanup()