Ever since this tiny, credit-card-sized computer, Raspberry Pi, appeared in the market, it has caught the imagination of every electronics and computer hobbyist around the world. The powerful Linux operating system combined with 26 input-output (I/O) pins can do many amazing things out-of-the-box. This article explains how to interface a USB camera and Wi-Fi with Raspberry Pi. It also explains the steps for making a webcam server using VLC Apache2 server software.
Raspberry Pi operating system
Several formats of Linux are available for installation on this tiny board. Among these, the most popular ones are Archlinux, Wheezy Linux, qtonpi, Fedora and xbmc.
I prefer Wheezy Linux because it is Debian style, where installation and tweaking is quite easy. The 2014-09-09-wheezy-raspbian.zip is available for download on raspberrypi.org. The wireless adapter, which comes in the form of a Wi-Fi dongle, can be installed out-of-the-box, so can be the USB cameras.
The steps to interface a USB webcam and Wi-Fi with a Raspberry Pi (model-B) board are given below. The USB webcam interfaced to a Raspberry Pi board is shown in Fig. 1.
Preparing the operating system. After downloading the zip file (wheezy-raspbian), extract the image in a folder on your computer and insert a 2GB or bigger-size SD card in the card reader slot. At this stage, check the identity of the SD card—sda, sdb or sdc—as shown in your Linux PC. After inserting the SD card, issue an ‘ls /dev/sd*’ command at a terminal prompt and see what name has been given to the card.
/dev/sda /dev/sda2 /dev/sda4 /dev/sda6
/dev/sda1 /dev/sda3 /dev/sda5 /dev/sda7
/dev/sdb1 /dev/sdb2 /dev/sdc
Here, my SD card has been identified as /dev/sdc. Now, go to the folder where you have extracted 2012-09-18-wheezy-raspbian.img file. This image file now needs to be written on the SD card to make it work as the operating system. Any mistake here will lead to the loss of entire data on your computer hard disk.
Look for the highest order, like sdc, sdd, etc, to identify your SD card. To be absolutely sure, just unplug and re-plug the card a few times and notice the changes.
At the terminal prompt, issue the following command:
PI/OS$ sudo dd if=2012-09-18-wheezy-
raspbian.img of=/dev/sdc bs=2M
This will use dd, the powerful image-creator program of Linux, and transfer the image to the SD card.
When it prompts for ‘su’ password, enter it. (In Windows, there are several graphical image-creator programs like Nero Burner that can be used in a similar fashion.)
The above command may take several minutes. Look for HDD Lamp Busy indication. When the lamp stops blinking, go to the desktop and safely remove the SD card. (Right-click on the SD card and then click on Safely Remove.)
Connecting the display/monitor. Connect the TV screen or monitor to Raspberry Pi board using a composite video cable (AV cable) or HDMI cable, insert the SD card into the SD card slot and switch-on the power supply.
The TV screen/monitor even on the right AV channel may refuse to come up the first time. When that happens, remove the power-supply cord of Raspberry Pi board once (keeping the monitor’s power supply on) and then re-plug it. Now, the monitor should come on live, if the SD card is not removed.
Once the Raspberry Pi starts booting up, it will prompt you for login name and password. The login for Wheezy linux is pi and the password is raspberry. Once entered, Raspberry Pi will open the text screen. Enter Startx command at the prompt to start the beautiful GUI desktop screen (Fig. 2). The first time when I saw the image on my television screen, I was mesmerised.
To make Raspberry Pi open the GUI screen every time it boots up, all you have to do is to make a change in the raspi-config page. Select Sudo Raspi-config and bring the cursor down on Automatic GUI Boot Up. Select → Finish → Reboot. And, it’s done.
Connecting the USB camera and Wi-Fi dongle. Connect the Wi-Fi adapter and webcam to USB slots. You will run short of USB ports now because Raspi-B model has only two USB ports. Find a USB hub with multiple USB ports and grab a hub that has a small, 5V power supply input to it.
(Remember, wireless needs more power. Therefore, if the hub is not powered by a suitable 5V supply, the Raspberry Pi may malfunction.)
Power-on the USB hub; connect the wireless adapter (Wi-Fi dongle), web camera, keyboard and mouse to the USB hub. Go to Raspberry Pi desktop and double-click the wireless module (Wi-Fi Config). Most wireless adapters are easily detected and installed on Raspberry Pi.
Set up a Wi-Fi connection. After double clicking Wi-Fi Config icon, the wpa-gui page will open up. Click Search and a list of available wireless connections will come up. Select a connection and enter the IP address, if required, or leave it as it is for DHCP mode, select the authentication mode (mostly WEP) from the pull-down menu and then enter WEP key in the first blank space. Click Finish and Connect. The symbol will turn to Connected.
(For connecting to a mobile hotspot like cell phones, the WPA2 psk type authentication is used. Select the same from the pull-down menu.)
Checking network connection. To check whether your Wi-Fi is connected to the network or not, you need to go to Terminal and issue ping command as given below.
Enter the following command at Raspberry Pi terminal:
Driver installation. You can issue the following command to search the driver on your Raspberry Pi computer:
PI/OS$sudo apt-cache search realtek
The command will give details of the packages available for that manufacturer. Since your Raspberry Pi is still not connected to the Internet, you need to go to a Linux PC that has an Internet connection and search for realtek drivers online. Go to the manufacturer’s website and download Debian wireless package (e.g., firmware-realtek.deb, firmware-ralink.deb) from their website. Copy this package in the /home/pi folder of the SD card and issue the following command to the Raspberry Pi:
With the firmware installed, setting-up the connection is pretty straight forward now. Go back to the desktop and double-click WiFi Config icon and follow the steps mentioned above under Set-Up a Wi-Fi Connection section.
Setting up the webcam server. ffmpg has some issues on Raspberry Pi as a result of which ffserver does not work easily. Some staunch Raspberry Pi supporter claims to tweak it to work but I find it very complicated and not worth when various other easy choices are available.
Apache2 server with webcam software makes our job very easy but, before that, to ensure the webcam is working, we have to install VLC software. Along with showing webcam images, VLC can play all kinds of media files including *.flv files, which was not possible in the earlier OS.
Installation of VLC Apache2. To install it, issue the following command:
vlc apache2 webcam
Webcam software has no default configuration file to run with, so we have to make one now using Nano editor. Nano is the most beautiful, yet versatile, text editor for Debian Linux. Close the current window and open a terminal to write the following commands:
host = localhost
dir = /var/www
file = webcam.jpg
tmp = imageup.jpg
local = 1
device = /dev/video0
width = 176
height = 144
delay = 5
quality = 75
trigger = 180
Note that, the webcam has a versatile software, with an ability to continuously ftp the captured image to a remote server, where the main webpage may be the host. But, in this case, we have made local = 1, which means that we will host it on Raspberry Pi’s Apache server page (var/www directory) itself.
The delay = 0 means the cam will take pictures non-stop. Since this will put a lot of load on the tiny Raspberry Pi board, a delay of five (means, taking one snap every five seconds) will be more justified for Raspberry Pi.
Checking the working of Apache server. We will now check whether Apache web server is working or not. In the Raspberry Pi computer, open a Midori browser page and enter the address as:
Before that, we have to know the IP address of our Raspberry Pi board (mine is a DHCP by my wireless router).
Issue the following command at the terminal window:
Automatic capturing of images. Open the terminal window and enter the following command:
Change the direction of the camera and refresh the browser on your laptop, and the image will change. Unless the browser window is refreshed, the picture will not change.
Now, we will create an automated page-refresh in Raspberry Pi itself.
Automated page-refresh from Raspberry Pi. Let us now use Nano to create a simple html page in /var/www directory of Raspberry Pi. Note that, /var/www is an Apache document root directory. Whatever we write here will be available, to view, from any PC connected to the network.
<META HTTP-EQUIV=”pragma” CONTENT=
<META HTTP-EQUIV=”refresh” CONTENT=”0”>
<h2> My Raspi Webcam </h2>
<img src=”webcam.jpg” width=”320”
The image will be auto-refreshing every second now. But, this may be a little irritating to the eyes, so we may need to enter a delay factor here. In the webcam, html CONTENT=”0” can be made to CONTENT=”5” for a more soothing refresh once in every five seconds.
You can strip Raspberry Pi board from the mouse, keyboard and monitor, and the webcam will still work non-stop—such is the power of Raspberry Pi and Linux.
Complete automation. Now, how about an automated script? So that, every time you boot or even before login, Raspberry Pi will fire-up the webcam command.
You can get rid of the monitor, keyboard and mouse from Raspberry Pi board. For that, we will put the command in /etc/rc.local file, and every time, on boot up, the webcam will start. Open a shell script file webcam.sh in /home/pi and enter the following command:
webcam /etc/webcam.conf &
Save it and exit.
pi@raspberrypi:/$ sudo chmod +x /home/
pi/webcam.sh // make it executable
Now, in the /etc/rc.local file, enter this line before ‘exit 0’ line like this:
Save it and exit.
Now, strip-off the keyboard, mouse and monitor from Raspberry Pi, and reboot it. Then, go to the remote laptop and open the page http://192.168.1.2/webcam.html. Look for the time stamp at the bottom of the picture. It should change every five seconds. If so, you have just made Raspberry Pi an automated webcam server.
Setting time and date. To reduce the cost of Raspberry Pi board, its manufacturer has not included a real-time clock (RTC) on the board. So, every time on boot-up, the time goes back to an earlier date. However, you can change the date using the following command:
But, how to get your Raspberry Pi board running without a keyboard, mouse and monitor? Simple, through ssh (secure shell) command.
Before stripping the I/O devices, run the raspi-config command (Fig. 3), select ssh option and enable it. Click Finish and reboot it.
Reboot Raspberry Pi board and access it from a remote PC or laptop through Wi-Fi. That is, after successful installation of the Wi-Fi dongle, you can reboot Raspberry Pi computer and run it on headless mode (no keyboard, mouse and monitor). Wi-Fi should be connected on the same network to access Raspberry Pi computer using ssh command. Issue the following command:
Capturing live images using motion software. There is another versatile, video-rendering open source software available, which works well along with Raspberry Pi. Motion software, besides streaming video, can detect sudden motion of an object in the frame area and take snapshots or videos, depending on the threshold limit set in /etc/motion.conf file. To install motion software, follow the steps as given below.
$sudo apt-get upgrade
$sudo apt-get install motion
$sudo nano /etc/motion/motion.conf
daemon off (to switch on—this can be changed near the top of the file). Save it, exit Nano editor and open the next motion file using the following command:
Change start_motion_daemon= no to start_motion_daemon=yes.
Now, we need to start motion server by issuing the following command:
It should be something like http://192.168.1.2:8081. You can enter this IP address on the Web browser in Raspberry Pi or the remote laptop to access the image being captured by the webcam.
You will find your Raspberry Pi webcam churning out streaming pictures.
You can do further settings in motion.conf file as follows:
The threshold value 1500 means that, if there is a minimum disturbance of 1500 pixels in the picture frame of the camera, it will trigger an event. Take a snapshot (or video) and dump it to /tmp/motion directory. However, since 1500 is a small value, you may change it to 6000.
At this point, you may change the webcam port to 5678 as 8081 is very common. Find On Picture Save in motion.conf file and create a pipe in front of it. Write the program you want to start additionally. For examples, I have added my program sendmail1.php (as shown below).
/tmp/motion/img.txt | php -f /var/www/
(Here, php -f /var/www/sendmail1.php is my program that motion kicks into life.)
You will find many parameters in motion.conf file and you can change them to make motion much more interesting.
Somnath Bera is an avid user of open source software. Professionally, he is a thermal power expert and works as an additional general manager at NTPC Limited