Recently, I decided I wanted to take time lapse photos of plants in my hydroponics rig as they grew. I looked around to see if a device existed which would do that job, since a couple of friends bought and set up security cameras for their houses over the winter holidays. Everything I found was overkill for my needs; what I basically needed was a laptop with a webcam to take photos every minute or so and stitch them together into a video. So, I bought a Raspberry Pi 3 from Amazon and hooked up my Microsoft LifeCam. Here’s how I set it up:
Raspberry Pi
The Model 3 B comes with wifi built in, so all you really need is a webcam and a power supply. It’s powered by a 5V 2A micro USB port, so you need something like a wall charger – PC USB ports don’t supply enough current.
Webcam
It’s a Microsoft LifeCam 720p Cinema. I forgot where I got it, but I think I won it as a prize a few years ago. Make sure to check online if your webcam will work with Linux/Raspbian.
Out-of-the-box configuration
I downloaded the non-GUI version of Raspbian and imaged it to an SD card; after plugging in the keyboard, monitor, and power supply, the first thing I did was change the password of the default account by running passwd
. The second order of business was getting the wifi connected by editing /etc/wpa_supplicant/wpa_supplicant.conf
. While editing the wpa_supplicant.conf
file, I noticed the keyboard mapping was set to Great Britain by default, so I ran sudo raspi-config
to set the locale, time zone, keyboard layout, and enabled the SSH server so I wouldn’t need to keep my keyboard and monitor plugged in.
Packages
With localization configured and the wifi successfully connected, it was time to start downloading packages. I started with sudo apt-get update
and sudo apt-get upgrade
. I installed the webcam package with sudo apt-get install fswebcam
, and the video encoding tools with sudo apt-get install libav-tools
.
Putting it all together
I wrote a bash script which:
- Creates a directory for the day, if it doesn’t exist, and then changes into it
- Takes a snapshot and saves it in the format imgNNNN.jpg, starting with 0001
- Stitches toether a video of all the .jpgs in the current directory
- Uploads it to iechoi.net
The full script is here.
There are already resources online which describe how to use fswebcam; the first few snapshots were inconsistent because the webcam needed some time to settle on the lighting conditions, so I added the -S option:
# -S 5 skips the first 5 frames; allows the camera to get settled with
# lighting conditions
fswebcam -S 5 img$n.jpg
Raspbian doesn’t have ffmpeg; it has avconv instead, which runs more or less the same:
avconv -r 25 -f image2 -i img%04d.jpg -c:v h264 -crf 1 -y plant-$DAY.mp4
Uploading to iechoi.net:
rsync -avzhe ssh *.mp4 xxxx@iechoi.net:www/mov/
However, I didn’t want to enter my password for every rsync call, so I set up an SSH key on the Raspberry Pi by running ssh-keygen -t rsa
, then copied ~/.ssh/id_rsa.pub to ~/.ssh on iechoi.net. I also had to authorize the key in my web host’s SSH settings.
Finally, I set the script to run every 15 minutes between the hours of 5am and 10pm by running crontab -e
and then entering the following line:
*/15 5-22 * * * ./capture.sh
Video: