0

I have written this code to take a picture when it detects motion however when I run the code it prints 'picture taken' but does not save the picture. I know my camera works as I tested it in LX terminal with the raspistill command. I have also tried changing the path for the file to be saved. If you can see where i have been going wrong an answer would be greatly appreciated. Thanks

import RPi.GPIO as GPIO
import time
import picamera

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

cam = picamera.PiCamera()
time.sleep(1)
if GPIO.input(4):
    cam.capture('/home/pi/Eaglecam/surveillance.jpg')
print('picture taken')
2

As malleYay already has pointed out you need something like a loop to realize continuously motion detection.

I am using the same setup as you and implemented it with the following code. Maybe it gives you a rough guidance:

#!/usr/bin/env python

# Import required Python libraries
import RPi.GPIO as GPIO
import picamera

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 7
GPIO.setup(GPIO_PIR, GPIO.IN)

Current_State = 0
Previous_State = 0

try:
    print "Waiting for PIR to settle ..."

    # Loop until PIR output is 0
    while GPIO.input(GPIO_PIR) == 1:
        Current_State = 0
    print "Ready"

    #Loop until users quits with CTRL-C
    while True:

        # Read PIR state
        Current_State = GPIO.input(GPIO_PIR)

        if Current_State == 1 and Previous_State == 0:
            # PIR is triggered
            camera = picamera.PiCamera()
            print "Motion detected!"
            camera.capture('motion.jpg');
            # Record previous state
            Previous_State = 1
            camera.close()
        elif Current_State == 0 and Previous_State == 1:
            # PIR has returned to ready state
            print "Ready"
            Previous_State = 0

        # Wait for 10 milliseconds
        time.sleep(0.01)

except KeyboardInterrupt:
    print "-> Quit"
    # Reset GPIO settings
    GPIO.cleanup()
0

When starting your script you check once if there is any motion, since this isn't the case the cam.capture part will not be executed and you print 'picture taken' (the print statement will be executed every time you run your script, wether you took a picture or not).

You want to check for motion in a loop until you end your script and only print 'picture taken' when this really happened.

Try something like this:

while True:
    if GPIO.input(4):
        cam.capture('/home/pi/Eaglecam/surveillance.jpg')
        print('picture taken')
    time.sleep(0.1) #just to slow down this script and give your pi time to do other stuff
0

this is what your code is doing:

cam = picamera.PiCamera() # the camera initialises
time.sleep(1) # delay
#right after the delay, you check ONCE to see if there is motion:
if GPIO.input(4): 
#if there is, you take the picture:
    cam.capture('/home/pi/Eaglecam/surveillance.jpg')

#but because that only happens ONCE only a second after the prgram starts, 
#there is probably not going to be any motion.

#then you print "picture taken". but because this is not indented,
#it is not part of the `if` block. so it does this either way, even if
#there's no motion:

print('picture taken') 

#then your programs over, and it stops until you start it again.

here is the corrected code:

import RPi.GPIO as GPIO
import time
import picamera

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN)
cam = picamera.PiCamera()
time.sleep(1)

#we put the main part of the code into a `while True:` loop, 
#which makes it run over and over again.

while True:
    if GPIO.input(4):
        cam.capture('/home/pi/Eaglecam/surveillance.jpg')
        print('picture taken')
    #if there's no motion, we do nothing.
    delay(0.1)

ps. I just realized this is an old post. woops!

  • old post woops! – scitronboy Mar 6 '18 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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