Weather webcams are always popular and it is easy and free to set one up yourself. This article will show how to setup a simple USB webcam to produce still images and serve them on a local apache webserver.
First the camera. What you need is one which will work with Linux. Fortunately most new cameras use the USB Video Camera specification so no special drivers are needed. Here I’m using a cheap camera from Asda which cost £8 although that’s not the only place you can get them.
Next is the software. Here we need something to capture still images from the camera and place them in a directory where apache will serve them to the web:
peter@dione ~ $ sudo apt-get install uvccapture imagemagick
This installs uvccapture which will get the images from the webcam and imagemagick which will be used to annotate the images.
You might also find a couple of other applications useful at first when setting up your camera. The first one is called cheese is useful. It’s a gui application which would enable you to test to see if the camera is working by displaying what the camera is seeing. The second one is guvcview which allows you to configure the camera’s settings.
Now we have the required software we need to write a couple of scripts. The first one is a wrapper around uvccapture. The second will be run by uvccapture so that when it’s captured an image it will decorate it with some text and then make it available to apache.
This script will capture an image from our camera once every 30 seconds and then pass it to the decorate.sh script:
#!/bin/bash # Output dimensions, 640x480 is the norm although you could use 320x240 for a smaller image WIDTH=640 HEIGHT=480 # Delay in seconds between capturing an image DELAY=30 # The jpeg image quality of the final image QUALITY=75 # path to decorate.sh DECORATE=/var/www/webcam/decorate.sh # path to uvccapture UVCCAPTURE=/usr/bin/uvccapture cd /var/www/webcam exec $UVCCAPTURE -d/dev/video0 -x$WIDTH -y$HEIGHT -t$DELAY -q$QUALITY -c$DECORATE
This script will take the image filename used by uvccapture and then add some text to it, here I use the site name, camera name and the date.
It also generates a thumbnail of the final image before moving it to the apache directory ready for serving – this latter part is essential as we don’t want apache to serve a partially generated image:
#!/bin/bash # # Bash script to take the output image and decorate it with details about the camera, timestamp etc # # Syntax: decorate sourceimage # SOURCE=$1 TEMP=/tmp/webcam.jpg TEMP_THUMB=/tmp/webcam_t.jpg DEST=/var/www/webcam.jpg DEST_THUMB=/var/www/webcam_t.jpg convert $SOURCE \ -fill '#0008' -draw 'rectangle 0,0,640,12' \ -fill white \ -gravity NorthWest -annotate 0 " MaidstoneWeather.com" \ -gravity North -annotate 0 "Cam North West" \ -gravity NorthEast -annotate 0 "$(date)" \ $TEMP convert $TEMP -thumbnail 100x100 $TEMP_THUMB mv $TEMP $DEST mv $TEMP_THUMB $DEST_THUMB
Once done you should now be able to run the camera:
peter@dione ~ $ nohup /usr/local/www/webcam/capture.sh &
That’s about it. You can access this specific camera over on MaidstoneWeather.
Running the camera on a Raspberry PI
This specific webcam is running on an old Linux NetBook but it should run on a Raspberry Pi without any issues – I’ve not tried it as I don’t have a spare camera (yet).
The one thing I could see as a problem is that all flash devices have a limited number of writes before they fail, so here as we write several images each time we perform a capture then that could be a problem later on.
That said if your pi is networked then you could use something like NFS to mount a remote networked drive and use that or if you rewrite display.sh to just record the full image (include the date/time in the file) you could have a cheap wildlife camera, get the pi to record and just swap the SD Flash card when needed.