Raspberry Pi Camera Setup for Time-Lapse

From Wurst-Wasser.net
Revision as of 09:53, 30 March 2014 by Heiko (talk | contribs)
Jump to navigation Jump to search

This article describes the setup of the Raspberry Pi Camera to create a time-lapse movie. If you're looking for a way to set up motion detection, see Raspberry Pi Camera Setup - Motion detected movie creation

Setup

Get package list

apt-get update

Update all packages

apt-get upgrade

Enable the camera

raspi-config

Raspicam01.png
Then answer the following stupid questions ("really enable?" and "reboot now?"), and you're done.

Configuration

Since using motion was some hazzle I decied to try a KISS-approach, using only the raspistill and convert[1] to implement.

Make the Pi take pictures

Make PiCam Device accessible

Since /usr/lib/cgi-bin/ is the default CGI-directory...

  • make sure, everyone can access the camera device[2]:
chmod o+rwx /dev/vchiq 

Setup script for taking a picture

  • create this script named takePicture.sh:
#!/bin/bash
#
# Globals
#
GFOLDERTARGET="/var/www/webcam-archive"
GFILELOCK="${GFOLDERTARGET}/takePicture.lck"
GFILELOCKAGEMAX="60"
GTIMESTAMP="`date +'%Y-%m-%d_%H-%M-%S'`"
GFILETARGET="${GFOLDERTARGET}/${GTIMESTAMP}.jpg"
GFILELASTSNAP="/var/www/webcam/lastsnap.jpg"
#
#
#
# Main
#
# Check locking
if [ -f "${GFILELOCK}" ]; then
  LOCKFILEAGE="`stat --format=\"%Z\" \"${GFILELOCK}\"`"
  NOW="`date +'%s'`"
  AGE="`expr ${NOW} - ${LOCKFILEAGE}`"
  if [ "${AGE}" -gt "${GFILELOCKAGEMAX}" ]; then
     echo "Lockfile is ${AGE} seconds old, removing it."
     rm "${GFILELOCK}"
  else
     echo "Lockfile is ${AGE} seconds old, exiting."
     exit 1
  fi
fi
#
#
# Lock
echo $$ > "${GFILELOCK}"
#
# timeout should not be 0, since images will be to dark and are noisy
# Since my cam is mounted wrong, I prefer to flip it :) (and flop it)
raspistill --nopreview -w 1024 -h 768 -o - | convert - -flip -flop - > "${GFOLDERTARGET}/lastsnap.tmp"
mv "${GFOLDERTARGET}/lastsnap.tmp" "${GFILETARGET}"
cp "${GFILETARGET}" "${GFILELASTSNAP}"
#
# Unlock
rm "${GFILELOCK}"
#
# Exit
exit 0
  • create a folder named webcam and webcam-archive:
cd /var/www
mkdir webcam
chown pi webcam-
mkdir webcam-archive
chown pi webcam-archive

Setup script for taking pictures

  • create this script named takePictures.sh[3]:
#!/bin/bash
#
# Globals
GSNAPSHOTSPERMINUTE=4
GSNAPSHOTINTERVAL="`expr 60 / 4`"
#
# Main
#for i in {0.."${GSNAPSHOTSPERMINUTE}"..1} # fail
#for i in {0..4..1} # works
for ((i=1; i<=${GSNAPSHOTSPERMINUTE}; i++)) 
do
  TIMEBEFORESNAPSHOT="`date +'%s'`"
  echo "Taking snapshot at `date`"
  /home/pi/Development/motion_webcam/takePicture.sh
  TIMEAFTERSNAPSHOT="`date +'%s'`"
  DURATIONOFSNAPSHOT="`expr ${TIMEAFTERSNAPSHOT} - ${TIMEBEFORESNAPSHOT}`"
  if [ "${DURATIONOFSNAPSHOT}" -lt "${GSNAPSHOTINTERVAL}" ]; then
     DELAY="`expr ${GSNAPSHOTINTERVAL} - ${DURATIONOFSNAPSHOT}`"
     echo "Sleeping for ${DELAY} seconds."
     sleep ${DELAY}
  fi
done
  • Add this to pi's crontab:
# PiCam
* * * * *       /home/pi/Development/motion_webcam/takePictures.sh > /dev/null 2>&1

Create a time-lapse movie

  • Create a new script mergeAllPicturesInOneAnimatedGIF.sh:
#!/bin/bash
# 
# Script: mergeAllPicturesInOneAnimatedGIF.sh
# Author: Heiko Kretschmer
# Purpose: Merge all .jpg into one animated .gif
#
# History:
# 2014-03-30,	09:52, h:	Creation. Note. default ticks in -delay is n/100 of a second
#

# Globals
#
GFOLDERSOURCE="/var/www/webcam-archive"
GSUFFIXSOURCE="jpg"
GTIMESTAMP="`date +'%Y-%m-%d'`"
GFILETARGET="/var/www/webcam-archive/${GTIMESTAMP}.gif"

# (Un)Main
#
convert -limit area 0 -delay 25 -loop 1 "${GFOLDERSOURCE}/*.${GSUFFIXSOURCE}" "${GFILETARGET}"
  • Add a crontab entry:
# PiCam - Merge Images to time-lapse-movie
0 0 * * *       /home/pi/Development/motion_webcam/mergeAllPicturesInOneAnimatedGIF.sh > /dev/null 2>&1
  • Carefully watch your disk, this process could easily eat up all your free space, especially check /tmp, where imagemagick will create temporary files!

Check if it works

How do the scripts work?

  • The takePictures.sh script will be fired every minute and will take, launching takePicture.sh a picture avery 15 seconds (4 per minute)
  • The mergeAllPicturesInOneAnimatedGIF.sh merges once a day all snapshots into one animated .gif

  1. Which is part of imagemagick
  2. including web users
  3. Execution of /home/pi/Development/motion_webcam/takePicture.sh takes approx. 7 seconds!