forked from UnicycleDumpTruck/PiLarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarmd.py
61 lines (52 loc) · 2.56 KB
/
alarmd.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
#!/usr/bin/python
import subprocess
import datetime
import time
import os
import RPi.GPIO as io
import tweetpony
api = tweetpony.API(consumer_key = "abcd", consumer_secret = "efgh", access_token = "ijkl", access_token_secret = "mnop")
io.setmode(io.BCM)
pir_pin = 18
flashingLight_pin = 7
io.setup(pir_pin, io.IN)
io.setup(flashingLight_pin, io.OUT)
io.output(flashingLight_pin, io.LOW)
# --------- Main Program ---------
previous_pir=0
while True:
current_pir=io.input(pir_pin)
if previous_pir==0 and current_pir==1:
with open("/home/pi/Alarm/armed.txt", "r") as fo:
fo.seek(0, 0)
status = fo.read(1)
fo.closed
print "Motion detected, armed status: " + str(status)
if (status == "1"):
subprocess.call("mpg123 /home/pi/Alarm/motiondetect.mp3", shell=True)
time.sleep(10)
with open("/home/pi/Alarm/armed.txt", "r") as fo:
fo.seek(0, 0)
status = fo.read(1)
fo.closed
if (status == "1"):
print "Correct passcode not entered, emailing picture and sounding alarm."
grab_cam = subprocess.Popen("sudo fswebcam -r 640x480 -d /dev/video0 -q /home/pi/Alarm/pictures/%m-%d-%y-%H%M.jpg", shell=True)
grab_cam.wait()
todays_date = datetime.datetime.today()
image_name = todays_date.strftime('%m-%d-%y-%H%M')
image_path = '/home/pi/Alarm/pictures/' + image_name + '.jpg'
subprocess.Popen('echo "Here is your intruder:" | mail -a ' + image_path + ' -s "Intruder Alert" [email protected]', shell=True)
try:
api.update_status_with_media(status = ("Intruder alert: " + todays_date.strftime('%m-%d-%y-%H%M')), media= image_path)
except tweetpony.APIError as err:
print "Oops, something went wrong! Twitter returned error #%i and said: %s" % (err.code, err.description)
io.output(flashingLight_pin, io.HIGH)
subprocess.call("mpg123 /home/pi/Alarm/alarm.mp3", shell=True)
subprocess.call("mpg123 /home/pi/Alarm/surrender.mp3", shell=True)
subprocess.call("mpg123 /home/pi/Alarm/alarm.mp3", shell=True)
io.output(flashingLight_pin, io.LOW)
del_img = subprocess.Popen("sudo rm -rf " + image_path, shell=True)
del_img.wait()
previous_pir=current_pir
time.sleep(1)