Using webcam with Raspberry Pi

The Raspberry Pi allows you to connect a camera, either a dedicated one or a Webcam which can be connected via USB port. This post covers how to get an USB Webcam to work.

Installation

First of all, when you connect the USB Webcam to the free USB port it should appear in /dev. If it doesn’t appear there it means that the camera is not supported by the system. This involves installing dedicated driver for the Webcam. If it is supported a new device like /dev/video0 should be present.

After the camera is successful recognized by the system you need a piece of software which will allow you to capture the image from the device. The fswebcam is a small useful program which allows you capture an image from an external device. To install it put:

sudo apt-get install fswebcam

into the terminal.

After doing so you can simply start capturing images. For example:

fswebcam image.jpg

It will capture an image and save it into the image.jpg file.

Capture image periodically

One way of capturing images periodically is to use CRON. First of all it is required to create a script which will do the job for us. The script is as follows:

#!/bin/bash

fswebcam /dev/shm/image.jpg
lastdate=date
echo $lastdate > /dev/shm/last_date.txt

You can also download this script here.

As you can see it doesn’t only capture an image but also it stores a time stamp that indicates the time when it was taken. What comes in handy is the tmpfs. This file system allows you to write files into the RAM memory. The key advantage of this way of storing files is that you do not write periodically on SD card on which Raspberry Pi is running. There is also a disadvantage when the RPi is disconnected from power source all the data stored in the RAM are lost, however. As long as you do not need to store the images you can benefit from tmpfs!

Run

crontab -e

to set up the periodic task. For example you can put line like this

*/10 * * * * /home/pi/path/to/script/capture_image.sh

to capture image every 10 minutes using previously prepared BASH script.

Publish images

Based on the article how to start a web server you have a platform which is capable of hosting a web page. You can write a plain HTML file which will only include an image. To make it more convenient you can create an symbolic link which will point at the captured image.

There is always a risk of unauthorized access to the data you store on the web server, however. That is why it is recommended to secure it up a bit. You can do this by enhancing previous HTML file with some PHP. You can find an example under this link (Using webcam with Raspberry Pi PHP files BASH script). Each time when you try to access the PHP file it asks you to enter the password. Furthermore, the password is hard coded into the PHP file. To prevent it from being read by unauthorized users you can set appropriate privileges for the file with chmod command.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.