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"