I must first say I am a complete beginner and so I apologise in advance if I'm missing the obvious.
I'm attempting to write code for a camera trap. I would like it to basically record video while motion is being detected and then to stop recording when there is nothing going on. So this is what I have so far:-
Code: Select all
#-*-coding utf-8-*-
import RPi.GPIO as GPIO
import time
from picamera import PiCamera
from time import sleep
camera = PiCamera()
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
Current_State = 0
Previous_State = 0
try:
print "PIR Module Test (CTRL+C to exit)"
time.sleep(2)
print("Ready")
while True:
Current_State = GPIO.input(PIR_PIN)
if Current_State==1 and Previous_State==0:
print("Motion Detected")
camera.rotation = 180
camera.start_preview(alpha=200)
for i in range(20):
camera.start_recording('/home/pi/video{}.h264'.format(i))
print('Captured pivid{}.264'.format(i))
i += 1
time.sleep(20)
if Current_State==0 and Previous_State==1:
camera.stop_recording()
camera.stop_preview()
time.sleep(1)
print " Ready"
except KeyboardInterrupt:
print (" Quit")
GPIO.cleanup()
pi@raspberrypi:~ $ sudo python pircamera.py
PIR Module Test (CTRL+C to exit)
Ready
Motion Detected
Captured pivid0.264
Traceback (most recent call last):
File "pircamera.py", line 27, in <module>
camera.start_recording('/home/pi/video{}.h264'.format(i))
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1012, in start_recording
camera_port, output_port = self._get_ports(True, splitter_port)
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 545, in _get_ports
'The camera is already using port %d ' % splitter_port)
picamera.exc.PiCameraAlreadyRecording: The camera is already using port 1
sys.excepthook is missing
lost sys.stderr
pi@raspberrypi:~ $
I'm guessing it's trying to start the camera again once it's already recording but I'm not sure how to stop it.
Any help gratefully received.
ogami