Thursday, March 28, 2024

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

By Somnath Bera

Save the file with ctrl+o command. Exit Nano using ctrl+x command. Close it and go back to the terminal window.

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:

[stextbox id=”grey”]http://localhost[/stextbox]Apache will open a page, indicating the server is up-and-running (even though there is no content page available in the server). So far, so good. Now, we have to see whether the same page is available over wireless or not.

- Advertisement -

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:

[stextbox id=”grey”]

- Advertisement -

pi@raspberrypi:/$ifconfig

[/stextbox]This will give details of the IP addresses of the wired as well as the wireless network of Raspberry Pi board. Look out for WLAN configuration—WLAN IP address of my Raspberry Pi board as assigned by my router is 192.168.1.2. So, I enter the following address in the address bar of the browser on my laptop:

[stextbox id=”grey”]http://192.168.1.2/[/stextbox]The same page opens up in my laptop. This confirms that Apache is up-and-running on Raspberry Pi board.

Automatic capturing of images

Open the terminal window and enter the following command:

[stextbox id=”grey”]pi@raspberrypi:/$sudo webcam /etc/
webcam.conf &[/stextbox]The last ‘&’ command means the process will execute in the background. Now, go back to a remote laptop and enter the following address in the browser address bar (To stop the webcam, use ctrl+c command or find out process ID number and then kill it by $sudo kill–9xxxx, where xxxx is the process number.):

[stextbox id=”grey”]http://192.168.1.2/webcam.jpg[/stextbox]A still-image facing the camera will now appear on the top-left corner of your laptop. This confirms that the webcam is working. You may try to open the same page in Midori browser on Raspberry Pi, but it may take several minutes to open, as most of its resources are busy handling webcam images. Do not worry; the page will open on the remote laptop quite comfortably.

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.

 [stextbox id=”grey”]pi@raspberrypi:/$ sudo nano /var/www/
webcam.html
<html>
<head>
<META HTTP-EQUIV=”pragma” CONTENT=
”no-cache”>
<META HTTP-EQUIV=”refresh” CONTENT=”0”>
</head>
<body>
<center>
<h2> My Raspi Webcam </h2>
<img src=”webcam.jpg” width=”320”
height=”240”>
</center>
</body>
</html>[/stextbox]Save using ctrl+o and exit using ctrl+x keys on the keyboard. Now, go to the remote laptop and open the page http://192.168.1.2/webcam.html

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:

3 COMMENTS

  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://192.168.0.103/webcam.jpg or
    httpp://192.168.0.103/webcam.jpg
    The result it gives is
    404 error url not found
    Plz help as soon as possible

SHARE YOUR THOUGHTS & COMMENTS

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators