Capturing Images with USB Camera, Wi-Fi and Raspberry Pi

By Somnath Bera


 [stextbox id=”grey”][email protected]:/$ sudo nano /home/pi/
#! /bin/bash
webcam /etc/webcam.conf &[/stextbox]

Save it and exit.

 [stextbox id=”grey”]

[email protected]:/$ sudo chmod +x /home/
pi/ // make it executable


Now, in the /etc/rc.local file, enter this line before ‘exit 0’ line like this:

 [stextbox id=”grey”][email protected]:/$ sudo nano /etc/
exit 0[/stextbox]

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 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:

 [stextbox id=”grey”][email protected]:/$ sudo date –set=
’2014-11-06 23:30’[/stextbox]

But, how to get your Raspberry Pi board running without a keyboard, mouse and monitor? Simple, through ssh (secure shell) command.

Fig. 3: Raspi-config window

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:

 [stextbox id=”grey”][email protected]:/$ ssh -l pi[/stextbox]When asked for password, enter raspberry and you are good to go. Change the date and time using above command and check the webcam (which is already running now) time stamp on page. You will see the current time running now.

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.

 [stextbox id=”grey”]$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install motion[/stextbox]By default, motion remains off. You need to edit motion configuration file. To do so, enter the following command in the terminal:

 [stextbox id=”grey”]

$sudo nano /etc/motion/motion.conf

[/stextbox]Motion configuration file will appear and you can change a few settings. Most importantly, though, we need to change the following:

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:

 [stextbox id=”grey”]$sudo nano /etc/default/motion[/stextbox]

Change start_motion_daemon= no to start_motion_daemon=yes.

Now, we need to start motion server by issuing the following command:

 [stextbox id=”grey”]$sudo service motion start[/stextbox]Wait for a minute and then navigate to your Raspberry Pi’s IP address at 8081 port.

It should be something like 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:

 [stextbox id=”grey”]$sudo nano /etc/motion/motion.conf[/stextbox]Find Threshold under Motion Detection Settings. The default value is 1500.

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).

 [stextbox id=”grey”]on_picture_save echo ‘Alert’ | echo %f >
/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.


  1. I tried the above steps
    Dedian apache works fine on both on my rpi and laptop but when I try
    Opening the webcam server that is
    httpp:// or
    The result it gives is
    404 error url not found
    Plz help as soon as possible

  2. I also attempted to open the webcam server and got the “404 error url not found”. I have gone through many guides and don’t have any positive outcomes. Kindly assist


Please enter your comment!
Please enter your name here