The Raspberry Pi’s low power consumption makes it well suited to the role of always-on server. This post describes how to attach a simple webcam to a Raspberry Pi and have it take a snapshot every few minutes, and how to view the pictures on the web. Just like a traditional webcam.
My Raspberry Pi was purchased from New It in the UK. It was installed with Debian 7 (“Wheezy”) by applying the image “2014-01-07-wheezy-raspbian.zip” downloaded from the raspberrypi.org downloads page. The following procedure was then performed without any further pre-work (other than enabling ssh in the basic setup).
The webcam software we will use is called motion. Install motion as follows.
First, change to root user, because many of the following commands require root rights, and it is a bit easier than typing “sudo” every time.
pi@raspberrypi ~ $ sudo su - root@raspberrypi:~#
root@raspberrypi:~# apt-get install motion
Many other libraries and programs will be installed at the same time.
Connect a Camera
Plug a USB webcam into one of the Pi’s USB sockets. Then type the following command to see how the Pi reacted to the new hardware.
root@raspberrypi:~# dmesg | tail [ 382.538191] media: Linux media interface: v0.10 [ 382.581097] Linux video capture interface: v2.00 [ 382.781838] uvcvideo: Found UVC 1.00 device USB Video Camera (0471:2034) [ 382.785808] uvcvideo: Failed to register entity for entity 3 [ 382.785840] uvcvideo: Failed to register entites (-22). [ 382.786752] input: USB Video Camera as /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/input/input2 [ 382.794596] usbcore: registered new interface driver uvcvideo [ 382.794632] USB Video Class driver (1.1.1) [ 382.894443] usb_audio: Warning! Unlikely big volume range (=65535), cval->res is probably wrong. [ 382.894479] usb_audio:  FU [Mic Capture Volume] ch = 1, val = -32768/32767/1[ 382.909879] usbcore: registered new interface driver snd-usb-audio
In this case, I attached an old Philips webcam, and it seems to have been accepted, despite the messages about “failed to register”.
Edit the Motion Configuration File
Move into the motion configuration directory.
root@raspberrypi:~# cd /etc/motion
Before editing the configuration file, take a copy of it. This is always a good thing to do. It means you can move back to the original file if your changes don’t work. The copy command also includes today’s date.
root@raspberrypi:/etc/motion# cp -p motion.conf motion.conf.140119
Now edit motion.conf and make the following changes. Change the line which says
and change the line which says
and change the line saying
The effect of these changes is to prevent motion from taking a picture every time it detects movement (which could cause hundreds of images to be generated in a couple of minutes), to prevent motion from taking videos, and instead to just take one picture every 60 seconds.
Type “motion” or “/usr/bin/motion” to start the motion program and test the configuration.
root@raspberrypi:/etc/motion# motion  Processing thread 0 - config file /etc/motion/motion.conf  Motion 3.2.12 Started  ffmpeg LIBAVCODEC_BUILD 3482368 LIBAVFORMAT_BUILD 3478785 ...
A ton of stuff will be printed, all about colours, pallets, threads and so on. Finally, after 60 seconds or so, a line like this should appear, showing that motion has taken its first picture:
= File of type 2 saved to: /tmp/motion/01-20140119152800-snapshot.jpg
Every 60 seconds thereafter a similar line will appear, showing another picture has been taken. Motion is basically working.
If your webcam is equipped with an indicator LED, it will now probably be illuminated, showing that the camera is active and transferring data.
Look at those Pictures
By default, pictures are stored in /tmp/motion. cd to that directory, type ls, and you should see the jpg files (open another shell or ssh session for this, so that you can leave motion running in the first window).
root@raspberrypi:/etc/motion# cd /tmp/motion root@raspberrypi:/tmp/motion# ls 02-20140119160700-snapshot.jpg 02-20140119160800-snapshot.jpg 02-20140119160900-snapshot.jpg 02-20140119161000-snapshot.jpg lastsnap.jpg
Notice the jpg image files and also a thing called lastsnap.jpg. This is always a soft link to the latest picture. View one of the pictures and verify that the camera is working. How you do this depends on how you are using the Pi. If you are logged in over ssh, open an sftp session to the pi, transfer a picture to your local PC and view it there. If you are using the Pi’s graphical desktop, just double click on the image file or something, and it will probably open in a viewer. I am not sure as I use ssh all the time.
Stop Motion and Tidy Up
That completes the test. In the shell where motion is running, enter ctrl-c to stop it. Also, if a large number of pictures have already been generated, delete some or all of them to save space. Or just leave them.
...  File of type 2 saved to: /tmp/motion/03-20140119162000-snapshot.jpg ^C Thread exiting  Calling vid_close() from motion_cleanup  Closing video device /dev/video0  httpd - Finishing  httpd Closing  httpd thread exit  Motion terminating root@raspberrypi:/etc/motion# cd /tmp/motion root@raspberrypi:/tmp/motion# rm *jpg
Install Apache Web Server
If a web server is installed, it is possible to view the webcam pictures remotely. Install the Apache web server as follows.
root@raspberrypi:/# apt-get install apache2
Apache2 will be installed and the software will be started.
In a browser, surf to the IP address of your Raspberry Pi. If you see a mostly white page saying “It Works !”, then Apache is running correctly on the Raspberry Pi. For example, my Pi is using IP address 192.168.1.71. So I start a browser on my PC and surf to http://192.168.1.71/. Alternatively, if you are using the Pi’s graphical desktop, you could start a browser directly on the Pi, eg. Netsurf.
Reconfigure Motion to put Images in the Apache Directory
With the following reconfiguration, it is possible to make motion place its images on the web site, so that you can see them from anywhere.
Edit the motion configuration file again:
root@raspberrypi:/# vi /etc/motion/motion.conf
and change this line
and save the file.
Start motion again (just type “motion” in a shell) and wait a couple of minutes for it to take a picture or two. The output will indicate that pictures are being stored in the new /var/www location:
root@raspberrypi:/# motion ...  File of type 2 saved to: /var/www/01-20140119164100-snapshot.jpg  File of type 2 saved to: /var/www/01-20140119164200-snapshot.jpg
Reconfigure Apache so Pictures can be Browsed
Not a reconfiguration exactly, just a simple file move. Rename the default index.html file supplied with Apache.
root@raspberrypi:/tmp/motion# cd /var/www root@raspberrypi:/var/www# mv index.html index.html.140119
This index.html file is what contains the “It Works!” web page that you saw when testing Apache above. By renaming it, you will be able to see your webcam pictures instead. Check it by surfing again to http://[Pi IP address], or just refresh the page in your browser. In my case, I run a browser on my PC, and surf to http://192.168.1.71/, because my Pi is on that IP address.
What you will see in the browser is a directory listing of /var/www on the Pi, showing all of the image files generated so far. View the latest image by clicking on “lastsnap.jpg“. You should not see a gorgeous snapshot of your desk, wall, garden or whatever, with a neat time stamp in the bottom corner proving when it was taken.
Refresh the browser page at any time to see the latest image (watch the time in the corner of the picture change when you click “refresh”).
That does it for a basic webcam setup on the Raspberry Pi. There are many options to try with the motion program. By changing the motion.conf file, you can get it to:
– take higher resolution pictures
– take videos
– detect motion (as the name suggests)
– take timelapse videos
…and so on. Before making any changes, I would recommend always taking a copy of motion.conf with a name showing the date. (cp -p motion.conf motion.conf). The motion.conf file is complicated and the options numerous. Having a way of undoing any changes is always a good idea.
Likewise, the file /var/www/index.html, that you have now renamed to /var/www/index.html., can be edited to present a nice web page. If you know some HTML, you could re-write the file to, for example, present the latest snapshot in a frame, or with some text around it, or several images, or thumbnails, or whatever.
Let’s be Sensible
At the moment, your motion installation is taking a picture every 60 seconds. You probably don’t want to take that many pictures, and eventually they will fill your SD card anyway.
Press ctrl-c in the shell where motion is running to stop the program. Then proceed as follows.
Configure a Sensible Snapshot Interval
Edit the motion configuration file again and change the picture interval to something more sensible than 60 seconds. I would suggest either 5, 10 or 15 minutes. For example:
root@raspberrypi:/# vi /etc/motion/motion.conf
Change the line
for a 5 minute interval, or 600 for 10 minutes, or 900 for 15 minutes.
Remove Old Images
While motion is running, it will continue to generate picture files more or less forever. The file sizes will depend on how posh your webcam is and the resolution configured in motion.conf.
It is a good idea to clean up and remove the old files periodically. You could just do an “rm” every so often in the output directory /var/www. If you want clean up to happen automatically, proceed as follows.
Automated Image Clean Up
Create a crontab and enter job details as follows.
Note: The first two commands here are used to set the editor to “vi“. By default, the crontab -e command will bring up a nano editor session, and I don’t know how to use nano, so I set the EDITOR variable make it use vi instead. If you know nano, go straight to crontab -e, and use nano.
root@raspberrypi:/# EDITOR=vi root@raspberrypi:/# export EDITOR root@raspberrypi:/# crontab -e
… an editor session will be opened. In the editor, go to the end of the file and add this line:
15 * * * * (date; ls /var/www/*snapshot.jpg | head -n -20 | xargs rm -v) >> /tmp/images_deleted 2>&1
then save the file.
Now that the above cron job is in place, it will be executed at fifteen minutes past the hour, every hour. All snapshot images except for the 20 most recent ones will be deleted. If you want to keep more images, say 50 or 100, type crontab -e again and change the “-20” to “-50” or “-100”.
Cleanup Log File
In order to see what files the cron job has deleted, look at its log file /tmp/images_deleted after a few hours. For example:
root@raspberrypi:/var/www# cat /tmp/images_deleted Sun Jan 19 17:32:01 UTC 2014 removed `/var/www/01-20140119164100-snapshot.jpg' removed `/var/www/01-20140119164200-snapshot.jpg' removed `/var/www/01-20140119164400-snapshot.jpg' removed `/var/www/01-20140119164500-snapshot.jpg' removed `/var/www/01-20140119164600-snapshot.jpg' removed `/var/www/01-20140119164700-snapshot.jpg' removed `/var/www/01-20140119164800-snapshot.jpg' removed `/var/www/01-20140119164900-snapshot.jpg' removed `/var/www/01-20140119165000-snapshot.jpg' removed `/var/www/01-20140119165100-snapshot.jpg' removed `/var/www/01-20140119165200-snapshot.jpg' removed `/var/www/01-20140119165300-snapshot.jpg' removed `/var/www/01-20140119165400-snapshot.jpg' removed `/var/www/01-20140119165500-snapshot.jpg' Sun Jan 19 17:33:01 UTC 2014 removed `/var/www/01-20140119165600-snapshot.jpg' Sun Jan 19 17:34:02 UTC 2014 removed `/var/www/01-20140119165700-snapshot.jpg'
If you noticed that my log file shows the cron job running every minute and not once an hour, – yes, that is because I was just testing the job to make sure it worked right.
Running Motion in the Background
If the motion program is running in a shell, it will likely die when you either close the shell or logout of your Pi. If you want it to run all the time, run it “in the background” instead.
If motion is running, type ctrl-c to stop it.
Invoke motion as follows:
root@raspberrypi:/# nohup /usr/bin/motion > /tmp/motion.log 2>&1 &  4880
It is now running with a process id of 4880 (in the example). Check the log to verify:
root@raspberrypi:/etc/motion# tail /tmp/motion.log  mmap information:  frames=4  0 length=153600  1 length=153600  2 length=153600  3 length=153600  Using V4L2  Resizing pre_capture buffer to 1 items  Started stream webcam server in port 8081  File of type 2 saved to: /var/www/01-20140119184000-snapshot.jpg
You can log out of the Pi, and it will continue to take a picture every 5 minutes (or whatever) and display it on the web site, while logging its output to /tmp/motion.log.
I hope this guide has been useful and not too long or fiddly. You should now have a basic working webcam on the Raspberry Pi that can be viewed in a browser.
If you want to be able to view the webcam from across the internet, ie. from locations other than your home network, it is necessary to forward a port on your internet router. That is beyond the scope of this article. However it is quite easy to do and many guides exist on the internet.
The above article avoids some of motion’s more advanced features, eg the built in “live” webcam server, the motion detection, and the recording of videos. These features can be explored by the more advanced motion user, but they are not suitable for a introductory article, they put a higher CPU load on the Pi and can take large amounts of storage space. Also the “live” streaming feature can max out your ADSL upload capacity if viewed remotely.
UPDATE 19th February 2015 – the recently released quad core Raspberry Pi 2 could probably handle motion’s video capture features, so long as enough storage is provided.