2

I am using a 3 pin PIR Sensor to detect motion (https://www.modmypi.com/pir-motion-sensor). This is connected to the GPIO pins. I also have the RPI Camera module attached to the Camera CSI. I would ultimately like the RPI to detect motion then take a picture. I can detect motion with a python script and I take a picture with a python script but when I combine the two, I can detect motion, but get an error when I try to take the picture. I need some help figuring out what the issue is. The camera has been enabled as proven by taking pictures in a separate python script.

Error Statement:

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
Traceback (most recent call last):
  File "motionsensor.py", line 17, in MOTION
    camera = picamera.PiCamera()
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 330, in __ini                                                                                        t__
    self._init_camera(camera_num)
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 441, in _init                                                                                        _camera
    prefix="Camera component couldn't be enabled")
  File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 127, in mmal_che                                                                                        ck
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Camera component couldn't be enabled: Out of res                                                                                        ources (other than memory)

My Code:

# coding: utf-8
import RPi.GPIO as GPIO
import picamera
import time

GPIO.setmode(GPIO.BCM)

PIR_PIN = 7

GPIO.setup(PIR_PIN, GPIO.IN)

filename = 0

def MOTION(PIR_PIN):

    print "Motion Detected!"
    camera = picamera.PiCamera()
    filename = 'image' + filenumber + '.jpg'
    camera.capture(filename)
    filenumber = filenumber + 1

print "PIR Module Test (CTRL+C to exit)"

time.sleep(2)

print "Ready"

try:

    GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)

    while 1:

        time.sleep(100)

except KeyboardInterrupt:

    print " Quit"
user2242044
  • 121
  • 2

2 Answers2

1

You are defining the camera whenever there is motion. The second time the call back runs, the there is an error, because there is already a process controlling the camera. Define the camera outside of the function, and the code should run fine. If this doesn't work reboot and run it again.

jath03
  • 426
  • 1
  • 4
  • 13
-1

Try rebooting, which'll clear your RAM

R Harrington
  • 311
  • 1
  • 2
  • 14
  • Rebooting is not a nice option.. The code needs to change as Jack Hughes said (or similar) but rebooting is masking the problem, not fixing it. – Luis Diaz Sep 22 '17 at 09:04