I am relatively new to programming and thought what better way to get into it than with a Raspberry Pi! Apologies in advance if my question is very basic or I have missed some information but here goes..
I am currently tinkering with a project I saw on the Raspberry Pi website called the PiLarm.
I have everything working so far but i'm struggling to add additional sensors to the code.
At the moment I have 1 PIR sensor which when triggered sends a notification email with image attached. I would like to add an additional PIR sensor and a reed switch also. I have tried multiple ways of adding these into the code but so far none have worked.
I do have the PIR and Reed Switch working individually in a separate programming but i just can't get my head around integrating them into my code so that when triggered each sensor will send the notification email.
I have attached my code and any help would be greatly appreciated!
Thanks in advance.
Code: Select all
#!/usr/bin/python
import subprocess
import datetime
import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pir_pin = 1
blue_LED_pin = 7
GPIO.setup(pir_pin, GPIO.IN)
GPIO.setup(blue_LED_pin, GPIO.OUT)
GPIO.output(blue_LED_pin, GPIO.LOW)
# --------- Main Program ---------
previous_pir = 0
while True:
current_pir=GPIO.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(5)
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" *****@gmail.com', shell=True)
GPIO.output(blue_LED_pin, GPIO.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)
GPIO.output(flashingLight_pin, GPIO.LOW)
del_img = subprocess.Popen("sudo rm -rf " + image_path, shell=True)
del_img.wait()
previous_pir=current_pir
time.sleep(1)