1

I've built a trail camera using a RPi Zero, the camera module and a PIR sensor to detect motion. I use a python program set to run on boot that records the footage and saves it to an attached USB device. The whole thing is powered by an USB power bank. Straight forward enough.

Recently, I've been noticing that after the first or second video has been recorded, subsequent video files are empty (0kb).

I've attached the full program below. I wondered if this might be something to do with how the PIR is being polled and if it is not getting enough of a break in order to save the file properly.

from gpiozero import MotionSensor
import logging
from datetime import datetime
from subprocess import call
import picamera
import time
import os

logfile ="/home/pi/trailcam_log/trailcam_log"+str(datetime.now().strftime("%Y%m%d%H%M"))+".csv"
logging.basicConfig(filename=logfile, level=logging.DEBUG,
format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d, %H:%M:%S,')

pir = MotionSensor(17)

print('Starting')
logging.info('Starting')

# Wait an initial duration
time.sleep(10)

while True:
    pir.wait_for_motion()
    logging.info('Motion detected')
    print('Motion detected')
    while pir.motion_detected:
        print('Taking photo')
        ts = '{:%Y%m%d-%H%M%S}'.format(datetime.now())
        logging.info('Taking photo: '+ str(ts)+'.jpg')
        with picamera.PiCamera() as cam:
            cam.resolution=(1024,768)
            cam.annotate_background = picamera.Color('black')

            cam.start_recording('/home/pi/video.h264')
            start = datetime.now()
            while (datetime.now() - start).seconds < 30:
                cam.annotate_text = "Peak Nature "+datetime.now().strftime('%d-%m-%y %H:%M:%S')
                cam.wait_recording(0.2)
            cam.stop_recording()
        time.sleep(2)
        timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
        input_video = "/home/pi/video.h264"

        logging.info('Attempting to save image')

        if os.path.isdir('mnt/usb1/videos'):
            logging.info('Saving to /mnt/usb1/videos/')
            output_video = "/mnt/usb1/videos/{}.mp4".format(timestamp)
        elif os.path.isdir('mnt/usb2/videos'):
            logging.info('Saving to /mnt/usb2/videos/')
            output_video = "/mnt/usb2/videos/{}.mp4".format(timestamp)
        else:
            logging.info('Saving to /home/pi/videos/')
            output_video = "/home/pi/videos/{}.mp4".format(timestamp)

        call(["MP4Box", "-add", input_video, output_video])
        time.sleep(10)
    print('Motion Ended')
    logging.info('Motion Ended')

Thanks for any advice.

Chris

  • Have you tried increasing your sleep timers to allow the sensor more time? – goobering Apr 12 '17 at 14:52
  • Hi @goobering, yes. See my answer for details. – Chris J Apr 13 '17 at 9:17
1

It looks as if reducing the sensitivity and the delay potentiometers on the PIR itself has done the trick. I had them set too high.

I tested the camera over about 12 hours and all the resulting video files were properly created (40-50Mbs).

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.