ZigSphere
Technology in Our Hands
Raspberry PI Webcam Over the Internet Using MJPG-Streamer Walkthrough
Print Friendly VersionThese instructions will explain how to setup a mjpg-streamer using the Rasbperry PI. An example of the result of this project can be found here
What you will need:
- A Raspberry PI with Raspian Already Installed
- A USB Webcam or the Raspberry PI Camera Module. (I used the Logitech 120 USB Webcam)
- A USB Extension Cable (Optional)
- A USB Powered HUB (Preferred)
- A Network Connection
Pre-installation steps
$ sudo apt-get update
$ sudo apt-get upgrade
Installing mjpg-streamer dependencies
$ sudo apt-get install libjpeg8-dev imagemagick libv4l-dev
Manual adjustment of videodev.h
The videodev.h file has been replaced with videodev2.h. In this case, a symbolic link must be created.
$ sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
Downloading and Compiling mjpg-streamer
Since the mjpg-streamer isn't located in a repository, apt-get cannot be used to install it. In this case, mjpg-streamer must be fetched from another location. Subversion was the method of choice, since it it much easier.
$ sudo apt-get install subversion
$ cd ~
$ svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
$ cd mjpg-streamer
Many plugins are included with the mjpg-streamer. These instructions will explain how to use three of them. First, let's explain the three types of plugins there are:
- input_uvc.so (for USB Webcams): copies JPGs from a single input (the Webcam in this case) to one or more output plugins. This is also good for low CPU usage, but at the same time having a high framerate.
- input_file.so (for camera module): similar to the USB camera method, but this method copies from a local directory.
- output_http.so: streams the files to a Webserver.
In this case, one of the methods should be selected below
If you are using a USB webcam:
$ make mjpg_streamer input_uvc.so output_http.so
If you are using the Raspberry PI Camera Module:
$ make mjpg_streamer input_file.so output_http.so
Copy the mjpg-streamer to another location
This step is optional, since the mjpg-streamer could be ran from the directory it was compiled in. To makes things easier, let's move it to another location.
$ sudo cp mjpg_streamer /usr/local/bin
If you have a USB webcam, run:
$ sudo cp output_http.so input_uvc.so /usr/local/lib/
If you have a camera module, run:
$ sudo cp output_http.so input_file.so /usr/local/lib/
Now let's move the web interface to the appropriate location.
$ sudo cp -R www /usr/local/www
Export paths
Since we moved the files to /usr/local/lib/ in the step above, if mjpg-streamer was attempted to be run, the system would't know where to look to run it. So, let's tell the system to look in another directory to run the mjpg-streamer.
$ export LD_LIBRARY_PATH=/usr/local/lib/
Now, let's reload the bash profile.
$ source ~/.bashrc
Starting the mjpg-streamer
$ mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www"
Here are the flags that are used in this command:
- -i : uses input_uvc.so (the USB Webcam) as input
- -o : output_http.so for the output (sending the images to a Web server
- -w : the directory, which has the HTML, CSS, and JS files: /usr/local/www
View the Webcam Live-stream From a Browser
Now you should be able to see the web interface for the mjpg-streamer. Make sure there is a network connection on the Raspberry PI.
- Locally: Navigate to:
http://localhost:8080 - From another device: Navigate to
http://"ip address of pi":8080
Run MJPG-Streamer as a Daemon (Background Service)
If you would like to have the mjpg-streamer started up automatically when the PI is booted up, a simple script can be created to do so.
$ sudo vi /etc/init.d/livestream.sh
Copy and paste the following into the file:
#!/bin/bash
mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www -c username:password"
Note: The "-c username:password" can be taken out of the script above if you want the public to see your camera. Otherwise, keep "-c username:password" in the script so that a username and password must be entered to view the camera.
Save the script and make it executable
$ sudo chmod 755 /etc/init.d/livestream.sh
Make the script executable during boot
$ sudo update-rc.d livestream.sh defaults
Accessing the PI over the internet
Now that the camera can be seen locally, it would also be interesting to show the world your camera. An outdoors camera, for instance.
What you will need
- Forward the PI's IP address in your router and with UDP port 8080
- A Dynamic DNS Membership that can be used on your router (Optional)
If you do obtain a Dynamic DNS membership, you can enter the credentials of your membership on your router (most routers support this). After doing so, you should be able to navigate to http://you_domain.com:8080. If you do not have a Dynamic DNS subscription, you can just use your public IP address.