Skip to content

Commit

Permalink
First rev of updated coded. Used config.py to set code variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmaker committed Jun 18, 2015
1 parent 2a9adab commit e2d3405
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 46 deletions.
83 changes: 68 additions & 15 deletions alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,36 @@
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"

os.system("sudo echo -n 0 > /home/pi/trush_workdir/scripts/homealarm/trigger.txt")
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)

# set up GPIO output channel for pin 11 GPIO 17
#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)
Expand All @@ -31,43 +46,81 @@ def blink(pin):
def kill_child_process():
print "Killing RFSniffer process pid %s"%p0.pid
os.killpg(p0.pid,signal.SIGTERM)
print "Killing Motion process pid %s"%p1.pid
os.killpg(p1.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("/home/pi/trush_workdir/scripts/homealarm/armed.txt", "r") as fo:
with open(armed_file, "r") as fo:
fo.seek(0, 0)
armed_status = fo.read(1)
fo.closed
print "Armed status: " + str(armed_status)
#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):
p0 = subprocess.Popen(["sudo","/home/pi/trush_workdir/rf_xmit/433Utils/RPi_utils/RFSniffer"],preexec_fn=os.setsid)
p1 = subprocess.Popen(["sudo","motion","-n"],preexec_fn=os.setsid)

#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
time.sleep(1)

with open("/home/pi/trush_workdir/scripts/homealarm/trigger.txt", "r") as fo1:
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!!!"
subprocess.call(["bash","intruder_mail.sh"])
for i in range(0,20):
blink(led)

#Else if system is disarmed - Clear trigger status for next run
#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"):
os.system("sudo echo -n 0 > /home/pi/trush_workdir/scripts/homealarm/trigger.txt")
#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"
Expand Down
2 changes: 1 addition & 1 deletion armed.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0
1
43 changes: 43 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from collections import defaultdict

config = defaultdict()

#Directory where git code is present
config['home_alarm_git_dir'] = "/home/pi/trush_workdir/scripts/homealarm_git"

#Dir where 433 MHz scripts are installed
config['rf433_dir'] = "/home/pi/trush_workdir/rf_xmit/433Utils/RPi_utils"

#Keypad arm/disarm code
config['keypad_code'] = 123

#Webserer port
config['port'] = 8085

#Webserver username
config['browser_username'] = "anyusername"

#Webserver password
config['browser_passwd'] = "anypassword"

#RF code for first switch outlet
config['switch1'] = "1398209"

#RF code for second switch outlet
config['switch2'] = "1398065"

#RF code for third switch outlet
config['switch3'] = "1398029"

#RF code for enableing the RF Siren
config['siren_enable'] = "1568816"

#RF code for disabling the RF Siren
config['siren_disable'] = "1568771"

#Send gtalk message using a script
config['send_gtalk_message'] = "/home/pi/trush_workdir/raspi_gtalk_robot/tkgtalk.py"
config['enable_gtalk_message'] = 0

#Enable webcam motion tracking
config['webcam_motion_enable']=0
30 changes: 26 additions & 4 deletions control.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,45 @@
import subprocess
import time
import signal
from config import config

# Retrieve GPIO lib
GPIO = webiopi.GPIO

os.system("sudo echo -n 0 > /home/pi/trush_workdir/scripts/homealarm/armed.txt")
tx_pin = 17 #gpio 17
GPIO.setFunction(tx_pin, GPIO.OUT)

armed_file = config['home_alarm_git_dir'] + "/armed.txt"
send_script = config['rf433_dir'] + "/send "

os.system("sudo echo -n 0 > " +armed_file)

def arm():
os.system("sudo echo -n 1 > /home/pi/trush_workdir/scripts/homealarm/armed.txt")
os.system("sudo echo -n 1 > " + armed_file)

def disarm():
os.system("sudo echo -n 0 > /home/pi/trush_workdir/scripts/homealarm/armed.txt")
os.system("sudo echo -n 0 > " + armed_file)


def sw1_toggle():
os.system("sudo "+ send_script + config ['switch1'])

def sw2_toggle():
os.system("sudo " + send_script + config ['switch2'])


def sw3_toggle():
os.system("sudo " + send_script + config ['switch3'])


# Instantiate the server on the port 8085, it starts immediately in its own thread
server = webiopi.Server(port=8085, login="tk", password="tk")
server = webiopi.Server(port=config['port'], login=config['browser_username'], password=config['browser_passwd'])

server.addMacro(arm)
server.addMacro(disarm)
server.addMacro(sw2_toggle)
server.addMacro(sw1_toggle)
server.addMacro(sw3_toggle)

#start keypad.py and alarm.py process
p0 = subprocess.Popen(["sudo","python","keypad.py"],preexec_fn=os.setsid)
Expand Down
39 changes: 23 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
var button;


button = webiopi().createButton("bt_arm", "Arm", arm);
button = webiopi().createButton("bt_arm", "System Arm", arm);
$("#middle").append(button);

button = webiopi().createButton("bt_disarm", "Disarm", disarm);
button = webiopi().createButton("bt_disarm", "System Disarm", disarm);
$("#middle").append(button);

button = webiopi().createButton("bt_l0lighton", "Light 0 - On", l0_on);
$("#middle").append(button);
button = webiopi().createButton("bt_sw1", "Switch 1 On/Off",sw1_toggle);
$("#middle").append(button);

button = webiopi().createButton("bt_sw2", "Switch 2 On/Off", sw2_toggle);
$("#middle").append(button);

button = webiopi().createButton("bt_l0lightoff", "Light 0 - Off", l0_off);
$("#middle").append(button);
button = webiopi().createButton("bt_sw3", "Switch 3 On/Off",sw3_toggle);
$("#middle").append(button);

}

Expand All @@ -33,21 +36,25 @@
webiopi().callMacro("disarm");
}

function l0_on() {
webiopi().callMacro("l0_on");
function sw2_toggle() {
webiopi().callMacro("sw2_toggle");
}

function l0_off() {
webiopi().callMacro("l0_off");
function sw1_toggle() {
webiopi().callMacro("sw1_toggle");
}

webiopi().ready(init);
function sw3_toggle() {
webiopi().callMacro("sw3_toggle");
}

webiopi().ready(init);

</script>
<style type="text/css">
button {
margin: 15px 15px 15px 15px;
width: 150px;
width: 250px;
height: 50px;
font-size: 14pt;
font-weight: bold;
Expand All @@ -60,12 +67,12 @@
</head>
<body>
<div id="content" align="center">
Home Alarm System Interface
Home Automation System Interface
<div id="middle"></div>
</div>

<p> <w> Webcam-1 Webcam-2 </p></w>
<img width="320" height="240" src="http://192.168.1.76:8082/?action=stream" align="left">
<img width="320" height="240" src="http://192.168.1.76:8081/?action=stream" align="center"><br/>

<p> <w> Webcam-1 </p></w>
<img width="320" height="240" src="http://192.168.1.76:8081/?action=stream" align="left"><br/>
</body>
</html>
9 changes: 2 additions & 7 deletions intruder_mail.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#!/bin/bash

############ Parameters ############
user="pi"
mittente="<from_email>@gmail.com"
dest="<to_email>@gmail.com"
smtp="smtp.gmail.com:587"
username="<from_email>@gmail.com"
pass="<my password>"
dest="[email protected]"
############ End Parameters ############


subject="Home Alarm Motion Detect!"
message="WARNING! Motion detected on home alarm system"
sendEmail -f $mittente -t $dest -u $subject -s $smtp -xu $username -xp $pass -m $message
echo $message | mail -s "$subject" $dest
exit


9 changes: 6 additions & 3 deletions keypad.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import RPi.GPIO as GPIO
import time
import subprocess

from config import config

class keypad():
# CONSTANTS
KEYPAD = [
Expand All @@ -25,7 +26,7 @@ class keypad():
]

ROW = [18,23,24,25]
COLUMN = [4,17,22]
COLUMN = [4,22,10]

def __init__(self):
GPIO.setmode(GPIO.BCM)
Expand Down Expand Up @@ -94,7 +95,7 @@ def exit(self):

#setup a 3 digit code.Each attempt will be called "code"
code = "000"
passcode = "123"
passcode = config['keypad_code']
haltcode = "555"

with open("/home/pi/trush_workdir/scripts/homealarm/armed.txt", "r+") as fo:
Expand Down Expand Up @@ -138,6 +139,8 @@ def exit(self):
fo.closed
#Else system was not armed - arm it now
else:
#Allow some delay to leave home - 1 min here
time.sleep(60)
with open("/home/pi/trush_workdir/scripts/homealarm/armed.txt", "r+") as fo:
fo.seek(0, 0)
fo.write("1")
Expand Down
1 change: 1 addition & 0 deletions trigger.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0

0 comments on commit e2d3405

Please sign in to comment.