0

i am building a security system which consists of two PIRs (GPIO 17 & 18) relay GPIO 24 , video will be emailed and will receive a text message when motion is detected . the camera can see both PIRs locations . when PIR 1 or PIR 2 is activated separately the script is working perfectly ( i am receiving an email and text message and relay is working ) , however when both PIRs are activated together the script wont work it get stuck somehow . the thing is i need to record a 5 seconds video if any of the PIRs is activated separately or both at the same time . your support is needed !

thank you in advance

see below the script :

import RPi.GPIO as gpio  
import picamera       
import time   
import os  
import email   
import sys

import smtplib   
from datetime import datetime  
from picamera import PiCamera  
from email.MIMEMultipart import MIMEMultipart  
from email.MIMEText import MIMEText   
from email.MIMEBase import MIMEBase  
from email import encoders   
from twilio.rest import Client   

 relay=24   
 pir1=18   
 pir2=17   

 HIGH=1   
 LOW=0   

gpio.setwarnings(False)      
gpio.setmode(gpio.BCM)     
gpio.setup(relay, gpio.OUT)                 
gpio.setup(pir1, gpio.IN)           
gpio.setup(pir2, gpio.IN)           

def start_recording_video():      
    video_name = datetime.now().strftime("%m-%d-%Y_%H.%M.%S")       
    camera.start_recording(video_name + h264_video)      
    time.sleep(5)      
    camera.stop_recording()      

os.system("MP4Box -add " + video_name + h264_video + " " + video_name + 
mp4_video) 

os.system("rm " + video_name + h264_video) 
footage = video_name + mp4_video

f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M:%S")
msg = MIMEMultipart()
msg["Subject"] = f_time
msg["From"] = "xxxxxx@gmail.com"
msg["To"] = "xxxxx@gmail.com"
text = MIMEText(" Motion Detected ")
msg.attach(text)

part = MIMEBase("application", "octet-stream")
part.set_payload(open(footage, "rb").read())
email.encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename= %s" % 
os.path.basename(footage))
msg.attach(part)

server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login("xxxxx@gmail.com","xxxxx")
server.sendmail("xxxxxx@gmail.com", "xxxxxx@gmail.com", msg.as_string())
server.quit()

os.system("rm " + footage) 


def sending_a_text_message_1():     
    account_sid = 'xxxxxxxxx'    
    auth_token = 'xxxxxxxx'     
    client = Client(account_sid, auth_token)     
    message = client.messages \     
        .create(      
            body='Intruder Pir_1',    
            from_='+xxxxxx',   
            to='+xxxxxx'     
         )      

print(message.sid)     

def sending_a_text_message_2():      
    account_sid = 'xxxxxxx'      
    auth_token = 'xxxxxxxxx'       
    client = Client(account_sid, auth_token)     
    message = client.messages \  .create(     
             body='Intruder Pir_2',    
             from_='+xxxxx',      
             to='+xxxxxx'
         )    
print(message.sid)     

 gpio.output(relay , 0)    

 camera = PiCamera()
 camera.rotation = 180 
 h264_video = ".h264" 
 mp4_video = ".mp4"

while 1:            
   if gpio.input(pir1)==1 and gpio.input(pir2)==0 :      
       print " PIR1 Motion detected!"      
       gpio.output(relay, HIGH)     
       start_recording_video()     
       sending_a_text_message_1()     
       while(gpio.input(pir1)==1 and gpio.input(pir2)==0):    
           time.sleep(1)       

elif gpio.input(pir1)==0 and gpio.input(pir2)==1 :      
    print " PIR2 Motion detected!"     
    gpio.output(relay, HIGH)     
    start_recording_video()     
    sending_a_text_message_2()     
    while(gpio.input(pir1)==0 and gpio.input(pir2)==1):      
        time.sleep(1)      

elif gpio.input(pir1)==1 and gpio.input(pir2)==1 :      
    print " PIR1 & PIR2 Motion detected!"     
    gpio.output(relay, HIGH)     
    start_recording_video()          
    while(gpio.input(pir1)==1 and gpio.input(pir2)==1):      
        time.sleep(1)
else:                   
    gpio.output(relay, LOW)      
    time.sleep(0.01)      
  • Please edit your post so all code is in a code block. If your using Thonny on the Pi, select all code, press tab to indent all code, then paste into your post. – CoderMike Jan 27 at 20:39
  • why do you have two functions for sending text messages? – jsotola Jan 28 at 4:30
  • separate the detection phase and the action phase (record,send) ...... only set flags when motion is detected ....... after the detection phase, run actions, depending on the state of the flags – jsotola Jan 28 at 4:36
  • @jsotola hello , the body of each message is different , just to know from where the detection is coming whether PIR1 or PIR 2 – Rami Jan 28 at 6:40
  • @jsotola thanks for your valuable reply , to be honest i am very new with programming , would you mind giving me a script as an example ? – Rami Jan 28 at 6:43
1

Do you not need a third if section?? You've got if if gpio.input(pir1)==1 and gpio.input(pir2)==0 : then elif gpio.input(pir1)==0 and gpio.input(pir2)==1 : but nothing for when when both inputs = 1 so nothing will happen because you don't have a if statement that covers that contingency. I think you need a third elif statement that covers elif gpio.input(pir1)==1 and gpio.input(pir2)==1 :

  • i actually tried this but didnt work : elif gpio.input(pir1)==1 and gpio.input(pir2)==1 : print " PIR1 and PRI2 Motion detected!" gpio.output(relay, HIGH) start_recording_video() while(gpio.input(pir1)==1 and gpio.input(pir2)==1): time.sleep(1) – Rami Jan 27 at 21:31
  • Hmmmm. Not too sure then to be honest. When you say it doesn't work is it presenting any specific error or behaviour? Just on a bit of a tangent have you tried combining your current if statements into one so elif gpio.input(pir1)==1 or gpio.input(pir2)==1 : and then having your second if as the and. – rohtua Jan 27 at 21:36
  • it didnt show any error , it just freeze – Rami Jan 27 at 21:36
  • Have you ran it in a python IDLE you should see some debug info if you run the script in there. Do you need to specify pir2 off or vice versa? Would it not work the same way if it was while 1: if gpio.input(pir1)==1 : print " PIR1 Motion detected!" gpio.output(relay, HIGH) start_recording_video() sending_a_text_message_1() while(gpio.input(pir1)==1: time.sleep(1) elif gpio.input(pir1) : print " PIR2 Motion detected!" gpio.output(relay, HIGH) start_recording_video() sending_a_text_message_2() while(gpio.input(pir1)==0): time.sleep(1) else: gpio.output(relay, LOW) time.sleep(0.01) – rohtua Jan 27 at 21:45
  • i didnt ran it in a python IDLE .. yes i have to specify ... it would work using the script you have given = if PIR1 is activated i will receive a message and email and if PIR1 not activated i will receive an email and text message without PIR2 sensor being activated also . if PIR2 is activated or not i will not receive anything ( no message and no email ) ... you got my point ? – Rami Jan 27 at 22:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.