1

I have a simple code for PIR sensor to detect a movement, which works as expected. Code:

from gpiozero import MotionSensor
import time
pir = MotionSensor(4)
while True:
 pir.wait_for_motion()
 print("You moved")
 pir.wait_for_no_motion()

Now when I want to use it with my camera, what happens is it works the first time, if a movement is presented it will take a picture and loops around, but after the second movement it crashes each time. Code:

from gpiozero import MotionSensor
from picamera import PiCamera
import time
pir = MotionSensor(4)
while True:
 pir.wait_for_motion()
 camera = PiCamera()
 camera.rotation = 180
 camera.start_preview(fullscreen=False, window = (50, 25, 640, 480))
 print("You moved")
 time.sleep(1)
 camera.capture('/home/pi/Desktop/image.jpg')
 #camera.stop_preview()
 pir.wait_for_no_motion()

And this is the dump of the error message:

pi@raspberrypi:~/Desktop $ python test.py 
You moved
mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x88c990 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled
Traceback (most recent call last):
 File "test.py", line 10, in <module>
 camera = PiCamera()
 File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 433, in __init__
 self._init_preview()
 File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 513, in _init_preview
 self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
 File "/usr/lib/python2.7/dist-packages/picamera/renderers.py", line 558, in __init__
 self.renderer.inputs[0].connect(source).enable()
 File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 2212, in enable
 prefix="Failed to enable connection")
 File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 184, in mmal_check
 raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources
asked Apr 23, 2019 at 4:20

1 Answer 1

1

You should move the camera initialisation stuff outside the while loop.

from gpiozero import MotionSensor 
from picamera import PiCamera 
import time
pir = MotionSensor(4)
camera = PiCamera()
camera.rotation = 180
camera.start_preview(fullscreen=False, window = (50, 25, 640, 480))
while True:
 pir.wait_for_motion()
 print("You moved")
 time.sleep(1)
 camera.capture('/home/pi/Desktop/image.jpg')
 #camera.stop_preview() 
 pir.wait_for_no_motion()

That should solve your issue in my opinion.

answered Apr 23, 2019 at 5:01
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.