WHAT'S NEW?
Loading...

Beginners Guide How To Setup a USB Webcam to your Raspberry Pi 2 in Minutes

Most of you surely have a USB webcam that you used in the past and then thrown away in some corner because it has become old or because you changed your PC. Nowadays, most of the computer is supplied as a standard webcam and no longer needs to buy and to be connected externally. What to do with this old hardware? If you have a device Raspberry Pi, you can reuse your old webcam to build something useful again.


Turn on your Raspberry Pi, open a terminal window, connect the webcam into the USB port and enter the following command that will list all the USB devices that are being connected.

lsusb

you should see your camera listed as a usb device, for example

Bus 001 Device 006: ID 0c45:602c Microdia Clas Ohlson TWC-30XOP Webcam

if webcam is not in the list then you are not lucky, your webcam is not compatible with Raspberry Pi. If your Webcam is compatible, you can try to take some pictures to verify its functionality by installing fswebcam software.

sudo apt-get install fswebcam

Now run this command to capture a picture from the USB Webcam and save it as testimage.jpg

fswebcam testimage.jpg

You can run fswebcam with many switches, type the following command to have more options

man fswebcam

That is all! Your webcam setup is complete, but if you want experiment more with your webcam continue to read. Now we will install another very useful software to stream the video signal via web interface. The program name is "Motion" and you can read all the documentation here.

apt-get install motion

Now edit two configuration files

sudo nano /etc/default/motion

Change the following value from NO to YES

start_motion_daemon=no --> start_motion_daemon=yes

Press Ctrl+O to save and type Ctrl+X to exit.

sudo nano /etc/motion/motion.conf

This is quite a large conf file but the points you need to edit are:
daemon on
stream_localhost off
webcontrol_localhost off

############################################################
# Daemon
############################################################
# Start in daemon (background) mode and release terminal (default: off)
daemon on
############################################################

############################################################
# Live Stream Server
############################################################
# Restrict stream connections to localhost only (default: on)
stream_localhost off
##############################################################

############################################################
# HTTP Based Control
############################################################
# Restrict control connections to localhost only (default: on)
webcontrol_localhost off
#############################################################

Press Ctrl+O to save and type Ctrl+X to exit.

Then start the program by typing

sudo service motion start

sudo motion

To see if all is runnging type

sudo netstat -tulpn | grep LISTEN

Ports are showing as listening:
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN

Now get the local IP address of the Raspberry PI with command

ifconfig

On the second line of the resulting text, you find the IP address after "inet addr:" for example 192.168.1.6

Now visit the address http://YourIpAddress:8081 in your browser and you should get the Webcam stream. In my example i do http://192.168.1.6:8081 

You will see your Webcam stream into the browser from every pc in your LAN! 

Do you want to make available the display of your webcam directly on the Internet? Follow this tutorial.

Did you have trouble configuring the program files "motion"? Watch the video to get some help.

3 comments: Leave Your Comments

  1. Great tutorial.
    Any tips on how to speed up the response time of the webcam video streaming (for example, by decreasing video quality). I tried overclocking my B+, that didn't seem to help.
    Objective is to use webcam for navigation of robotic car.

    ReplyDelete
  2. Thanks so much for this tutorial. I am new to the PI and just starting to programme again. Last did math modelling in Fortran 4 so it was some time ago. Had great fun with some simple programme turning LEDS on an off and came across your tutorial. Followed the instructions and it worked. Thanks a lot

    ReplyDelete