himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Help needed for Multiple PIR sensors...

Thu Mar 31, 2016 1:21 pm

Hello,
I want to use multiple PIR sensors..
like whenever sensor #1 is active it should click pictures... and whenever sensor#2 is active it should record video..

I have performed both the above codes individually and they worked :twisted: ...now I want to club below 2 codes.in 1 program...


code #1

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

		

sensor=40
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state=False
current_state=False

cam=picamera.PiCamera()

while True:
	time.sleep(0.1)
	previous_state=current_state
	current_state=GPIO.input(sensor)
	if current_state!=previous_state:
		new_state="HIGH" if current_state else "LOW"
		print("GPIO pin %s is %s"%(sensor,new_state))
		if current_state:
                        fileName=get_file_name()
                        cam.start_preview()
                        cam.start_recording(fileName)
                else:
                        cam.stop_preview()
                        cam.stop_recording()
code #2

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera

sensor=4


GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state=False
current_state=False

cam=picamera.PiCamera()

while True:
	time.sleep(0.1)
	previous_state=current_state
	current_state=GPIO.input(sensor)
	if current_state!=previous_state:
		new_state="HIGH" if current_state else "LOW"
		print("GPIO pin %s is %s"%(sensor,new_state))
		if current_state: 
                        cam.start_preview()                      
                else:
                        cam.stop_preview()
                       
thank u..

User avatar
coltonj96
Posts: 26
Joined: Sat Jan 30, 2016 6:16 pm

Re: Help needed for Multiple PIR sensors...

Thu Mar 31, 2016 1:49 pm

Just create a method from both codes then add both methods to one loop, see if that works

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Thu Mar 31, 2016 2:28 pm

try this...

Note sensor 4 was trying to use pin4 which is 5v !! changed to 26
Last edited by gordon77 on Mon Apr 04, 2016 1:06 pm, edited 1 time in total.

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: Help needed for Multiple PIR sensors...

Sat Apr 02, 2016 1:42 pm

whenever I run this code.. it is showing "GPIO pin 26 is HIGH"..
even though there is no motion... :oops:

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Sat Apr 02, 2016 2:07 pm

Can you show us your wiring?

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: Help needed for Multiple PIR sensors...

Sat Apr 02, 2016 2:57 pm

yes..
Attachments
IMG-20160402-WA0006-min-min (1).jpg
IMG-20160402-WA0006-min-min (1).jpg (58.49 KiB) Viewed 7851 times

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Sat Apr 02, 2016 3:39 pm

Is the purple wire on pin28 not 26?

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Sat Apr 02, 2016 7:37 pm

Did moving it to Pin26 fix the problem?

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: Help needed for Multiple PIR sensors...

Sun Apr 03, 2016 12:07 pm

nopes...its on 26 only... :(

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 7:26 am

Looks like the purple wire is on 28 to me in the photo

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 12:41 pm

actually my problem is:
whenever I am executing this program..It will print GPIO pin 26 is high..
even though there is no motion..
n then it will only detect the motion of pin #40...

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 12:45 pm

himani18 wrote:actually my problem is:
whenever I am executing this program..It will print GPIO pin 26 is high..
even though there is no motion..
n then it will only detect the motion of pin #40...
What if you swap over the sensors ?

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 12:55 pm

I've tested the code and it works OK for me, here it is again in case it got corrupted

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
     

sensor1=40
sensor2=26

video = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state1 = False
current_state1  = False
previous_state2 = False
current_state2  = False

cam=picamera.PiCamera()

while True:
   time.sleep(0.1)
   previous_state1=current_state1
   current_state1=GPIO.input(sensor1)
   previous_state2=current_state2
   current_state2=GPIO.input(sensor2)
   
   if current_state1!=previous_state1:
      new_state1="HIGH" if current_state1 else "LOW"
      print("GPIO pin %s is %s"%(sensor1,new_state1))
      if current_state1:
         video = 1
         fileName=get_file_name()
         cam.start_preview()
         cam.start_recording(fileName)
      else:
         cam.stop_preview()
         cam.stop_recording()
         video = 0
         
   if current_state2!=previous_state2 and video == 0:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 1:04 pm

thanks a lot..
it worked :D

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Mon Apr 04, 2016 1:25 pm

Here's an update..it should capture stills even it is taking a video..

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
     

sensor1=40
sensor2=26

video = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state1 = False
current_state1  = False
previous_state2 = False
current_state2  = False

cam=picamera.PiCamera()

while True:
   time.sleep(0.1)
   previous_state1=current_state1
   current_state1=GPIO.input(sensor1)
   previous_state2=current_state2
   current_state2=GPIO.input(sensor2)
   
   if current_state1!=previous_state1:
      new_state1="HIGH" if current_state1 else "LOW"
      print("GPIO pin %s is %s"%(sensor1,new_state1))
      if current_state1:
         video = 1
         fileName=get_file_name()
         cam.start_preview()
         cam.start_recording(fileName)
      else:
         cam.stop_preview()
         cam.stop_recording()
         video = 0
         
   if current_state2!=previous_state2 and video == 0:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()
         
   if current_state2!=previous_state2 and video == 1:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.capture(fileName + '.jpg', use_video_port = True)

petecarroll
Posts: 3
Joined: Mon Apr 04, 2016 11:55 am

Re: Help needed for Multiple PIR sensors...

Tue Apr 05, 2016 8:08 am

is there any way to add email alerts to this?

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Tue Apr 05, 2016 8:11 am

take a look at this... and try adding it instead of or as well as taking video / stills


https://docs.python.org/2/library/email-examples.html
http://www.tutorialspoint.com/python/py ... _email.htm

petecarroll
Posts: 3
Joined: Mon Apr 04, 2016 11:55 am

Re: Help needed for Multiple PIR sensors...

Tue Apr 05, 2016 8:35 am

thanks for that Gordon I`ll have a look at that, also how long could the wires from the pir to the pi be? 10 meters?

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Tue Apr 05, 2016 9:09 am

Sorry I don't know.

BMS Doug
Posts: 4423
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Help needed for Multiple PIR sensors...

Tue Apr 05, 2016 12:20 pm

petecarroll wrote:thanks for that Gordon I`ll have a look at that, also how long could the wires from the pir to the pi be? 10 meters?
10 meters sounds like it could present issues with noise, I think you'd need to use screened cable for that sort of run and even then you may still have problems.
Doug.
Building Management Systems Engineer.

Rami_B
Posts: 3
Joined: Sun Jan 27, 2019 8:37 pm

Re: Help needed for Multiple PIR sensors...

Mon Jan 28, 2019 8:39 am

hello guys , if i want to take a video of 5 seconds if PIR1 or PIR2 or both PIRs are activated ,

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 . would you mind helping with the script please ?

thank you in advance

Code: Select all

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 and 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)

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Mon Jan 28, 2019 6:07 pm

I would assume it always see one pir before the other, so triggers either of them rather than both.

Want happens when it gets stuck? Any errors reported?

Rami_B
Posts: 3
Joined: Sun Jan 27, 2019 8:37 pm

Re: Help needed for Multiple PIR sensors...

Mon Jan 28, 2019 6:30 pm

Hi Gordon , thanks for your reply . When PIR1 detects movement , the relay will go on , then the email takes some time ( like 1 min or more ) to be uploaded and sent , once the email is uploaded i receive a text message and then the relay will be off . If During this process PIR2 is activated , i will not receive a message or email , the PIR1 wont work after that I will have to press control C (no errors at all will be printed on the screen ) So both PIR are working perfectly if they are detected seperatly .

gvinco
Posts: 3
Joined: Mon Aug 31, 2020 6:31 pm

Re: Help needed for Multiple PIR sensors...

Mon Aug 31, 2020 6:36 pm

gordon77 wrote:
Mon Apr 04, 2016 12:55 pm
I've tested the code and it works OK for me, here it is again in case it got corrupted

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
     

sensor1=40
sensor2=26

video = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state1 = False
current_state1  = False
previous_state2 = False
current_state2  = False

cam=picamera.PiCamera()

while True:
   time.sleep(0.1)
   previous_state1=current_state1
   current_state1=GPIO.input(sensor1)
   previous_state2=current_state2
   current_state2=GPIO.input(sensor2)
   
   if current_state1!=previous_state1:
      new_state1="HIGH" if current_state1 else "LOW"
      print("GPIO pin %s is %s"%(sensor1,new_state1))
      if current_state1:
         video = 1
         fileName=get_file_name()
         cam.start_preview()
         cam.start_recording(fileName)
      else:
         cam.stop_preview()
         cam.stop_recording()
         video = 0
         
   if current_state2!=previous_state2 and video == 0:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()

If I wanted to use 3 sensors instead of 2, how would I go about altering this code? I've tried a few things and nothing has worked. I'm not even sure I'm on the right path. Any suggestions or guidance?

gordon77
Posts: 6623
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help needed for Multiple PIR sensors...

Wed Sep 02, 2020 7:57 am

try this, the 3rd sensor will take photos as sensor 2

Code: Select all

import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
     

sensor1=40
sensor2=26
sensor3=36

video = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor1, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor3, GPIO.IN, GPIO.PUD_DOWN)

previous_state1 = False
current_state1  = False
previous_state2 = False
current_state2  = False
previous_state3 = False
current_state3  = False

cam=picamera.PiCamera()

while True:
   time.sleep(0.1)
   previous_state1=current_state1
   current_state1=GPIO.input(sensor1)
   previous_state2=current_state2
   current_state2=GPIO.input(sensor2)
   previous_state3=current_state3
   current_state3=GPIO.input(sensor3)
   
   if current_state1!=previous_state1:
      new_state1="HIGH" if current_state1 else "LOW"
      print("GPIO pin %s is %s"%(sensor1,new_state1))
      if current_state1:
         video = 1
         fileName=get_file_name()
         cam.start_preview()
         cam.start_recording(fileName + '.h264')
      else:
         cam.stop_preview()
         cam.stop_recording()
         video = 0
         
   if current_state2!=previous_state2 and video == 0:
      new_state2="HIGH" if current_state2 else "LOW"
      print("GPIO pin %s is %s"%(sensor2,new_state2))
      if current_state2:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()

   if current_state3!=previous_state3 and video == 0:
      new_state3="HIGH" if current_state3 else "LOW"
      print("GPIO pin %s is %s"%(sensor3,new_state3))
      if current_state3:
         fileName=get_file_name()
         cam.start_preview()
         time.sleep(2)
         cam.capture(fileName + '.jpg')
         cam.stop_preview()

Return to “Beginners”