Python Loop Help (Camera and PIR Motion Sensor)

118 views
Skip to first unread message

craisondigital

unread,
Dec 29, 2015, 3:21:16 PM12/29/15
to WebIOPi
I have a PIR motion sensor hooked up to the PI.  I set up a timer in python using code Toshi gave me in a previous post so that when Motion is detected the timer starts counting down 30 seconds.

I would like my camera to start recording once motion is detected and stop recording if the timer reaches zero.

I have a command line code to start the recording which i plan to set up as a macro for webiopi so that once motion is detected the camera starts.

The issue i see is that if i set this up in the loop, the command to start the camera will execute with every loop.  I only need it to execute once.
I have confused myself numerous times in trying to figure out a solution.  I am  thinking i just need to set a 3rd variable somehow.  Thanks for reviewing this, I apprecitate your time.

Craig

Toshi Bass

unread,
Dec 30, 2015, 3:39:39 AM12/30/15
to WebIOPi
Create a variable

Record = False


Then in your loop add :

       if Record == False and timer != 0:
           # call macro that starts recording
           Record = True

      if Record == True and timer == 0:
             # call macro that stops recording 
           Record = False        

craisondigital

unread,
Dec 30, 2015, 11:12:32 AM12/30/15
to WebIOPi
Thanks Toshi, with your code it appears it would keep starting the recording as long as the timer is not zero.  I've adjusted it slightly.  Here is the working code.  Thanks for the help..


import webiopi
import datetime
import os
import sys
GPIO = webiopi.GPIO
SEN_M = 4
timer = 0
cam = 0
 
def setup():
 
        GPIO.setFunction(SEN_M, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
def loop():
        global timer, cam
 
        STATE_SEN_M = GPIO.input(SEN_M)
 
        if STATE_SEN_M == 1 and cam == 0:
                print ("Motion - Cam On")
                os.system ("sudo sh /home/pi/scripts/record.sh &")
                timer = 15
                cam = 1
 
        if STATE_SEN_M == 1 and cam == 1:
                print ("Motion")
                timer = 14
 
        if timer != 1:
                timer -= 1
                print (timer)
 
        if timer == 1:
                print ("Camera OFF")
                os.system ("sudo sh /home/pi/scripts/record_stop.sh")
                cam = 0
 
        webiopi.sleep(1)

Toshi Bass

unread,
Dec 30, 2015, 12:43:22 PM12/30/15
to WebIOPi
Glad you got it working how you wanted it, 

However your statement "with your code it appears it would keep starting the recording as long as the timer is not zero" is incorrect.


if Record == False and timer != 0:  # The if only proceeds if Record = False and the timer is not 0
   # call macro that starts recording # Assuming the pir tripped the timer then Recording will start
   Record = True      # next loop the if wont proceed because Record is True not False even though timer is not = 0 so the macro to start will not get called again

if Record == True and timer == 0:   # OK Record is True but until the timer = 0 nothing happens
   # call macro that stops recording # when the timer =0 the Recording stops
   Record = False     # The only time Record = False again is when the timer = 0 then of coarse the first if statement will only proceed if the timer is not 0 ie when the                                        pir trips again

craisondigital

unread,
Dec 30, 2015, 3:28:40 PM12/30/15
to WebIOPi
Yes. I see.  Sorry I need sleep.  Thank you again!
Reply all
Reply to author
Forward
0 new messages