This Raspberry Pi Webcam Server tutorial will take you through on how to have your very own Webcam that is visible on a webpage.
If you’re after a more security like system then check out the Raspberry Pi security camera tutorial it features fully-fledged web streaming, motion detection, recording and multi camera functionality.
This tutorial is marked intermediate it as it can be difficult to get working especially if you have hardware that differs to the versions used in this tutorial. If you find that you can’t get this to work then the security camera solution linked above is much easier to get going.
If you’re more of a visual person and would prefer to watch how to set this up then you check out my video below. If you like the video then please subscribe to me so you’re kept up to date on all the latest videos!
Below are some of the pieces of equipment that you will need to be able to complete this tutorial.
How to Setup a Raspberry Pi Webcam Server
Firstly we will need to install Raspbian onto the Raspberry Pi. If you haven’t already done this then check out my awesome guide on installing NOOBs Raspberry Pi. It will take you through all the steps that you will need to do to get up and running.
Installing Motion with a USB Camera
We will be using a package called motion if you want to learn more about it you can check out their website at Motion.
- We will be using the terminal so open up terminal on the Pi or via SSH.
- Let’s check for any updates for Raspbian and its packages.
sudo apt-get update sudo apt-get upgrade
- First we will need to download and install motion, to do this enter the following command:
sudo apt-get install motion
- Now we need to make some edits to the configuration file (motion.conf)
sudo nano /etc/motion/motion.conf
- Find the following lines and change them to the following.
- daemon on
- webcam_localhost off
- Optional (Don’t include the text in brackets)
- webcam_maxrate 100 (This will allow for real-time streaming but requires more bandwidth)
- framerate 100 (This will allow for 100 frames to be captured per second allowing for smooth video)
- width 640 (This changes the width of the image displayed)
- height 480 (This changes the height of the image displayed)
- Now we need to setup up the daemon, first we need to edit the motion file.
sudo nano /etc/default/motion
- Find the following line and change it to the following:
- Now make sure the camera is connected and run the following line
sudo service motion start
- If you need to stop the service simply run the following command
sudo service motion stop
- Now to test it out! We can check out the Raspberry Pi Web Cam Stream at the ip address of our Pi so in your browser go to the following address:
There we have it a fully working Raspberry Pi Webcam server that you can place wherever you would like (Given it is within Wifi range) and be able to view via the web browser. If you want allow external access to it check out my instructions towards the bottom of this tutorial.
Installing Motion with a Raspberry Pi Camera
Motion by default doesn’t support the Raspberry Pi Camera so we will need to do a different setup for it to work. However we will still get a really good Raspberry Pi Webcam server by doing this.
Note: This method doesn’t work on the Raspberry Pi 2 & 3. There are also issues with the latest version of Raspbian Jessie.(I may have a new method coming in the near future that will be compatible) Please head over to the security camera page for a working tutorial.
Installing the Hardware
In order to install the Raspberry Pi camera we will need go to the ribbon cable slot on the Pi and with two fingers on each side of the connector pull up. This should now open the connector, now insert the ribbon cable with the metal leads facing away from the Ethernet Port.
Once you have lined it up gently press the connector back down and the cable should now be locked in one place.
Installing the Software
Now to get the Raspberry Pi Webcam stream up and going we will need to install the required software. This process is different to the above method.
- Firstly we need to ensure the camera is switched within raspi config.
Enter the following command and then enable the camera.
- Next let’s update Raspbian and its packages
sudo apt-get update sudo apt-get upgrade
- Let’s also check and update the camera firmware if required.
sudo apt-get install rpi-update sudo rpi-update sudo reboot
- Now time to install motion to do this enter
sudo apt-get install motion sudo apt-get install libjpeg62
- Now lets create a directory in your home called mmal
cd ~/ mkdir mmal cd mmal
- Now we need to download dozencrow’s motion program using:
- Now we will need extract the file
tar -zxvf motion-mmal.tar.gz
- Now we need to edit the config file to do this enter the following line:
sudo nano motion-mmalcam.conf
- Now we will need to make several changes to the config file the following lines should be changed so they appear like the this:
- width 640
- height 480
- target_dir /home/pi/mmal/m-video
- output_pictures off
- logfile /home/pi/mmal/motion.log
- stream_maxrate 100
- framerate 100
- Now you can test it out by using the following command:
./motion -n -c motion-mmalcam.conf
- Now quit the process by typing ctrl+c
- This isn’t very good if you ever want to work on the Pi without disrupting the motion camera. To get around this we will write some scripts that can be called to easily stop and start the process. You can download these scripts here or you can simply keep following this tutorial.
- First lets create a file called startmotion by typing the following command:
sudo nano startmotion
- Now let’s type the following into the folder (A text editor (Nano) should be open)
#!/bin/sh nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
- Now let’s make the stop script by typing the following:
sudo nano stopmotion
- Now in the editor enter these two lines
#!/bin/sh killall motion
- Now we need to make both of the scripts executable by running the following commands:
sudo chmod 755 startmotion sudo chmod 755 stopmotion
- Now if you type either
./stopmotioneither start or stop the script and not disable the terminal so you can do other work if needed. These commands will only work if you’re inside the folder. If you want to call these outside you will need to use the following:
- You should now be able to access the webcam stream by going the Pi’s address on port 8081.
You should now have a fully working Raspberry Pi camera server that is accessible within your local network. If you want to allow external access to the camera then please follow my instructions below.
Setting up outside External Access
In order to provide external access to your Raspberry Pi Webcam Server we will need to change some settings in the router. However all routers are designed differently so you will probably need to look up your own brand.
This is what I did on mine in order to get it to work. My router is an AC1750 TP-Link Router.
- Go to the Router admin page (This will typically be 192.168.1.1 or 192.168.254)
- Enter the username and password. Default typically is admin & admin.
- Once in go to forwarding->Virtual Server and then click on add new
- In here enter:
- Service port: In this case 48461
- IP Address: 192.168.1.103 (Address of your Pi)
- Internal Port: We want this to be the same as the service so 8081
- Protocol: All
- Status: Enabled
- These settings will route all traffic destined for port 48461 to the webcam server (192.168.1.103)
- You should now be able to connect to the Raspberry Pi webcam stream outside your network. You may need to restart the router for changes to take effect.
If they are unable to connect you can try the following:
- Check your router settings and confirm they are correct.
- Check your IP hasn’t changed (Some IPs will provide you with a dynamic IP rather than a static IP) You can setup something called dynamicdns to counter this you can find out more information here.
- Restart the router.
I hope you have enjoyed this tutorial on how to build a Raspberry Pi Webcam server if you want more then please check out all my Raspberry Pi projects I currently have completed! There will many more coming in the future.