Webcam and Raspberry¶
![Raspberry Pit](../_images/logo-raspberry_pi.png)
I’ve got a new toy. A [Raspberry PI](http://en.wikipedia.org/wiki/Raspberry_Pi). Until a couple of days ago I just didn’t know what to do with it. That’s not a problem at all. A Raspberry is one of the things you can just buy anyway, because you just might need it one day anyway. And it’s not money being thrown out of the window either. Because?!
For a long time shot of his house a friend requested something being able to take a picture an hour or so for a couple of months. Suddenly the Raspberry became quite an alternative. Equipped with a webcam the Raspberry can take pictures and store them away. That shouldn’t be much more than a quick finger exercise?
Installing the Raspberry is well documented and the description on how to do that can be found on the Raspberry PI website quite easily. Installing the graphical interface made it very easy as well to verify that the webcam is working. But a graphical interface isn’t really useful if no Monitor is connected.
One of the many tools available is fswebcam. Installation under Debian is as usual, nothing to report here. Doing some test shots.
The first webcam I connected was working great. At least in the room. The second I pointed it outside, the image got too bright. That means some webcams are sold with a fixed aperture so they don’t adjust to changing light conditions. As soon as you want to take a picture on the outside, it’s getting useless. So I needed another one: the Logitech HD Webcam C270 does work quite well. It doesn’t make pictures like with a professional camera, but it will serve its purpose.
When connected the device shows up as /dev/video0
on the Raspberry. A bit of fiddling around with the parameters left me with the script below, which I placed in a sub-folder in the user pi
’s home-directory in ~/bin/webcam.pl
and created a folder for the data output with the right rights in /data/webcam
:
#!/usr/bin/perl
use strict;
use warnings;
my $pathoutput = '/data/webcam';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $timestamp = sprintf ( "%04d%02d%02d%02d%02d%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec);
exec('fswebcam -r 1280x720 -d v4l2:/dev/video0 --set "Backlight Compensation"=0 --no-banner ' . $pathoutput .'/' . $timestamp .'-webcam.jpg');
That script that put into the crontab
of the user with the runtime adjusted and the work is done. There’s not much daylight during the year before 0600 in the morning and 2200 in the evening. And even if there is: These times are so far out of the usual working hours, than everything between 2200 and 0600 can be paused and that will save some disk card space as well.
# CronJOB
## Crontab for running the webcam command three times every hour between 6 and 22
00,20,40 6-22 * * * ~/bin/webcam.pl
I tried for testing to disable the White balance auto correction for a day when testing the setup, but the pictures showed up in false colors, so I reached better results with the auto white balance enabled.
At this point I can take the captured images and convert them quickly into an animated GIF up to 1280x720 pixel (with an insanely big file size!) or scale it a big smaller.
$ convert -delay 10 -loop 0 *-webcam.jpg ~/animation.gif
Converting the images of a whole year into a movie isn’t more complicated than that.
There are some other points, which I haven’t figured out yet what to do with them:
Cold : During winter the whole setup will be in the outside, still trying to take pictures. I’m not very confident that the dropping temperatures will not kill either the cam or the Raspberry or the SD-card or all together.
Disk: The Raspberry is running from a SD-Card. The disk space is quite limited on it if you want to record and store continuously information. Taking a backup of an ext3 partition is also quite tricky for inexperienced people.
Timing: The setup requires only a power connection for the PI to boot and start taking picture. As beautiful as it is, without a data connection the PI will not sync its local time. That time is used to make the timestamps in the pictures. Disconnecting the re-connecting the PI could cause a reset of the time stamp and images being overwritten. That’s something we don’t want, is it? This requires further testing.