Raspberry Pi Webcam Robot – Best Video Streaming Tutorial

Raspberry pi webcam robot DIY Hacking

Raspberry pi webcam robot

It is always cool to add a camera to your remote controlled car or robot, so that you can see where its heading exactly and probably use a wifi enabled smartphone or tablet to view the video as well.Its very simple now to do this using a raspberry pi and a USB webcam.Real time streaming can be done with minimum delay too. Here, steps are given on how to view the feed from your webcam on the monitor connected to the raspberry pi as well as on another device in the same local network. This tutorial will show you how you can add a camera and video broadcasting system and make a raspberry pi webcam robot . Have fun with this DIY Hacking tutorial!

Raspberry pi webcam robot DIY Hacking

USB webcam

What are the stuff required to do this project?

Hardware:

  1. Raspberry Pi model B with memory card preloaded with an OS.
  2. WiFi dongle : Edimax EW 7811UN.
  3. A USB webcam.

Software (Programming languages and OS  involved): 

  1. HTML.
  2. Linux/Rasbian.

How does it work?

The working of the raspberry pi webcam robot is explained as follows. Here, the USB webcam connected to the raspberry pi usually associates with the pi at /dev/video0.A streaming service called mjpg streamer after installation is then used to broadcast the video on the raspberry pi’s local IP address and port number. The resolution of the video and frame rate can be set using linux commands while running the mjpg streamer.The video feed can then be viewed on any local network connected device by opening an HTML file in a browser that invokes the video from raspberry pi’s IPaddress:portno.The file required for this is provided in this tutorial as well. Also, in order to test the webcam locally on the pi, a service called ffmpeg is used. This allows you to view the webcam feed on the monitor connected to the raspberry pi. A block diagram showing the basic working:

Raspberry pi webcam robot DIY Hacking

Block diagram for the raspberry pi webcam robot

Step 1: Connecting the USB webcam and checking it

First of all, you need to check if your webcam is getting detected in the raspberry pi and whether its feed can be viewed. For this, first run the command “lsusb” in the terminal of the raspberry pi. This shows you a list of all the USB connected devices on the pi. Find out, from the list if your webcam’s name or software is displayed (sometimes driver name is displayed eg: Microdia for Iball webcams). Next, we need to check whether you can view the feed from the webcam on the pi. For this use “cd /dev” to go to /dev directory. Next, use “ls” to list its contents, check if “video0” is present.

Raspberry pi webcam robot DIY Hacking

Raspberry pi with USB webcam and wifi dongle

Now, you have to use a service called ffmpeg to view the feed from the webcam on the monitor connected to the pi.Use “sudo apt-get update” to update lists. To install ffmpeg service, use these commands:

sudo apt-get install ffmpeg
ffplay -f video4linux2 -framerate 15 -video_size 320x240 /dev/video0

The first command installs ffmpeg. The second commands starts up the video having a frame rate of 15fps using the video4linux2 mode , having a resolution of 320×240 and using the device from /dev/video0. Once you run it, you will see the webcam feed on the monitor.

Step 2: Setting up the video streaming service for the webcam

Here, the video from the raspberry pi is broadcasted on the local network. Any device on this network can view the video from the webcam.To do this, you need to first install the mjpg streamer. Use these commands to install it:

sudo apt-get install libv4l-dev
sudo apt-get install libjpeg8-dev
sudo apt-get install subversion
sudo apt-get install imagemagick

libv4l-dev and libjpeg8-dev serve as dependencies, imagemagick is used for installation whereas subversion is used for installing open source softwares. In order to download and compile the code use these commands :

svn co https://svn.code.sf.net/p/mjpg-streamer/code/
cd /home/pi/code/mjpg-streamer/
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr install

Next, to run this service and to begin streaming use the following command:

mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0 -y -r 640x480 -f 10" -o "/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer"

  • -i input plugin parameters
  • -d represents the video device
  • -y enables YUV format, disables MJPEG
  • -r specifies the resolution
  • -f is the frame rate
  • -o output plugin parameters
  • -p specifies the port number
  • -w specifies output webserving directory

Now your raspberry pi has started broadcasting the video, to view this video download this HTML file : video.html. The contents of it are like this raspberry pi webcam robot (the box in the page will be replaced by your video stream) , but you need to edit the file before using it:

<html> <body> <h1>DIY Hacking - Webcam Robot</h1> <img src="http:192.168.1.9:8090/?action=stream" width="600"> </body> </html>

Here, edit the IPaddress “192.168.1.9” with the IP address of your raspberry pi. You can find it out by using the command “ifconfig” on your pi. Finally use your browser to open this html file and view the video. For devices, like Ipad, etc use any file manager apps to open the HTML file.

Step 3: Building the Raspberry Pi Webcam Robot

If you wish to build your own robot then continue reading. Follow the instructions at : Smartphone controlled robot, to build a smartphone controlled robot and then come back to this tutorial.This is the robot from the tutorial :

Raspberry pi webcam robot DIY Hacking

Smartphone controlled Robot

Now you need to add the raspberry pi and the webcam to this and use a wifi dongle to make it wireless. I have used one half of a plastic box to create a platform for the raspberry pi and webcam. Use hotglue to stick them together. And that’s it, you have made a video streaming, smartphone controlled robot. You can use SSH to initiate the program remotely on the raspberry pi without having the need to hook it up to a monitor.

Raspberry pi webcam robot DIY Hacking

Assembling the raspberry pi webcam robot

The robot can be maneuvered using a smartphone application on an android phone. The corresponding feed from the USB webcam of the raspberry pi can then be streamed using an Ipad, pc or any other device connected to the local wifi network.

Raspberry pi webcam robot DIY Hacking

Parts of the raspberry pi webcam robot

Use this DIY Hacking tutorial to make surveillance systems , video streaming robots or even drones. Real time streaming can be done at minimum delay using the mjpg streamer and is considered as one of the best streaming services. Please note that , it is not required to have an arduino along with pi for building this robot. You can build it by just using the raspberry pi, I have done this because I had already built a robot using arduino. Now watch this video showing the robot streaming the feed from the webcam :

Recent Posts
Showing 30 comments
  • shashank
    Reply

    Hey,
    Can you make a tutorial of line following robot using only raspberry pi

  • Anton
    Reply

    Now it won’t install ffmpeg. Is there a new program that will work?
    I get this when I try to install it:

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package ffmpeg is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘ffmpeg’ has no installation candidate

    • akshay pavithran
      Reply

      it wont work on rasbian jessei i.e, u need to compile it u r self on rasbian jessie or install rasbian wheezy

  • Beta
    Reply

    This project rocks … just the streaming part is “not state of the art”…
    If you want real free H264 streaming with 24 FPS and more and not 1 FPS try out the open source software Datarhei Restreamer on GitHub. https://datarhei.github.io/restreamer . With this tool you be your own streaming provider.
    Have fun DIY!

  • pacific
    Reply

    hi Arvind,
    This one is nice tutorial. I am also trying to do same using openCv python. But failed to make line follow or grid follow algoritham can u please send the link of your code or on github.

  • pranay
    Reply

    hey arvind
    when im installing ffmpeg
    “pi@stark:~ $ sudo apt-get install ffmpeg
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package ffmpeg is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘ffmpeg’ has no installation candidate”
    please help…

  • hans
    Reply

    Hi.

    it’s a nice tutorial

    i was wndering if this method could be used wit hthe raspberry pi camera.

    thanks.
    hans

  • Hemant
    Reply

    Hi Arvind,
    Thanks a lot for this post.
    I think I do have exactly the same Web Camera (iBall C8.0 Face2Face) but I am facing issues while capturing images on Raspbian due to lack of drivers.
    MJPEG capture or stream is not working.
    Could you please send me the link from where I can download the linux drivers for this web cam?

    Regards,
    Hemant

    • Arvind Sanjeev
      Reply

      Hi Hemant, can you state the exact errors you get? I did not download any drivers for this webcam, just plug and play.

      • Sourav
        Reply

        Hey,
        I have the exact usb webcam, but its not getting detected in lsusb. It is just showing

        ID 1e4e:0100

  • CHOKKALINGAM
    Reply

    Hai Sanjeev,
    Good evening actually i need the “ride smart” app setup and the documentation soon can u? pls mail me

  • ehtisham
    Reply

    Hi there when downloading from the svn site it says that it has been checked out

    • Arvind Sanjeev
      Reply

      Hey, that’s normal. Did you execute the remaining steps like on the tutorial?

  • Kamal
    Reply

    how to connect pi to a wifi network?
    how can i get the video output of pi through wifi?

    • Arvind Sanjeev
      Reply

      This tutorial, tells you how to do just that. To connect pi to wifi, you can use the GUI interface in the OS through a wifi dongle.

      • vinodh
        Reply

        Hi.
        when I use mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -y -r 640×480 -f 10″ -o “/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer.
        I am getting error as mjpg_streamer: unrecoginzed option ‘d’. please advice. thanks

        • Arvind Sanjeev
          Reply

          Do sudo apt-get update and sudo rpi-update and try again.

          • bala
            Reply

            pi@raspberrypi ~/code/mjpg-streamer $ mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -y -r 640×480 -f 10” -o “/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer”
            MJPG Streamer Version: svn rev: 3:172
            i: Using V4L2 device.: /dev/video0
            i: Desired Resolution: 640 x 480
            i: Frames Per Second.: 10
            i: Format…………: YUV
            i: JPEG Quality……: 80
            libv4l2: error setting pixformat: Device or resource busy
            Unable to set format: 1448695129 res: 640×480
            Init v4L2 failed !! exit fatal
            i: init_VideoIn failed

            am getting the error like this. What’s the problem?

            • Arvind Sanjeev
              Reply

              Try restarting the pi and repeat or kill any existing process using ps -a and kill -9

  • sana
    Reply

    me and my friends are working on quad-copter project just that streaming part remains actually we have used java on pi and on anther hand there is android to control that quad. all i done through java socket programming. i would like to know how its possible to send that video streaming directly on android device rather then browser? we have used arduino along with it if u have any information that how can i receive that stream from pi or arduino to android device then kindly guide me.

  • Muhammad Umar
    Reply

    I need the programming of ardiuno of this robot .. plz

pingbacks / trackbacks
  • […] Anyone who’s watched any kind of sci-fi show will have at one stage or another thought that it would be pretty cool to have their own robot, and this DIY hack offers you the chance of creating one. Using the Raspberry Pi single board computer, the hack offers the opportunity to produce a robot complete with USB webcam. […]

Leave a Comment

− 1 = 3

Start typing and press Enter to search