Setting up a Weather WebCam on Linux

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.

An example weather webcam image
An example weather webcam image

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.

capture.sh

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

decorate.sh

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.

Author: petermount1

Prolific Open Source developer who also works in the online gaming industry during the day. Develops in Java, Go, Python & any other language as necessary. Still develops on retro machines like BBC Micro & Amiga A1200

4 thoughts on “Setting up a Weather WebCam on Linux”

  1. Dear mister Peter,

    Since a view days I try to figure out, how to get the temperature in a webcam pic. I have a raspberry with a dht22 sensor attached. I tried to Google it. To find similar code. The closest I got are your pictures. Could you explain toe how you did it?

    With kind regards

    Stefan

    1. I’ve got a side project which I need to resume when I get some time where I’m building a weather station from the individual components using a Raspberry PI.

      So for that I wrote the software in C which then takes a picture from the PI’s camera once a minute then annotates the image with the sensor readings which the application also reads every minute over I2C, 1Wire or GPIO.

      It’s not really in a ready state yet, I got to starting to write the docs etc but not touched it for a while.

      Anyhow from the C side of things I’m using the libgd library to handle the drawing over the top of the image if that helps.

      The homepage is at http://piweather.center whilst the source (including some 3D printer models for some components) are on github https://github.com/peter-mount/piweather.center/

      Hopefully over the next few weeks I’ll get back to getting the final station running & the doc’s completed.

      1. Thank you for the quick reply, installing the software, I will propably will ask a lot, but for now is the installation clear

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: