This Raspberry Pi webcam server tutorial will take you through on how to have your very own Webcam that is visible on a web page.
If you’re after more of a security like system, then check out the Raspberry Pi security camera tutorial as it features fully-fledged web streaming, motion detection, recording and multi-camera functionality.
This tutorial is marked intermediate as it can be challenging 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.
Equipment
Below are some of the pieces of equipment that you will need to be able to complete this tutorial.
Recommended
- Raspberry Pi (Latest version recommended for best performance)
- Micro SD Card
- Ethernet Cable or Wi-Fi
- Raspberry Pi Camera or USB Webcam
Optional
Video on Setting up the Webcam
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 with all the latest videos.
Unfortunately, the video is now out of date and uses an older method then what’s explained in the text version of this tutorial. I highly recommend that you follow the written tutorial, while I work to update the video.
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.
In this tutorial, we will be using a package called Motion if you want to learn more about it you can check out their website at Motion.
Since the latest version of Raspbian is missing dependencies that Motion requires for it to work correctly, we will need to install a different precompiled version.
The package maintainer (Currently: Mr Dave | Previously: Calin Crisan) of Motioneye provides a package that has everything we need, and it works just fine with the Raspberry Pi.
1. We will be using the terminal, so open the terminal on the Pi or connect to it via SSH.
2. To begin, first, update the Raspberry Pi, so you’re running on the latest version.
sudo apt update
sudo apt upgrade
Depending on the version of Raspbian you’re using you will need to do some different steps. For this tutorial, we will be installing Motion for Raspbian Buster. If you’re not already on it then I highly recommend that you upgrade.
3. First, install the following packages. This command will work both on the full and lite version of Raspbian Buster.
sudo apt install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev
4. Next, download the Motion deb file for Raspbian Buster from GitHub using the wget command and install it using the dpkg command.
sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.5.1/$(lsb_release -cs)_motion_4.5.1-1_$(dpkg --print-architecture
).deb
sudo dpkg -i $(lsb_release -cs)_motion_4.5.1-1_$(dpkg --print-architecture).deb
That’s all you need to do before moving on to configuring Motion so that it will run on your Pi.
Configuring Motion
1. Now we need to make some edits to the configuration file, motion.conf.
sudo nano /etc/motion/motion.conf
2. Find the following lines and ensure that they are set to the following values.
daemon off
stream_localhost off
Note: Change the following two lines from on to off if you’re having issues with the stream freezing whenever motion occurs.
picture_output off
movie_output off
Optional (Don’t include the text after the #)
stream_maxrate 100
# This change will allow for real-time streaming but requires more bandwidth & resources. Needs to be added to the config file, default is 1framerate 100
# Changing this option will allow for 100 frames to be captured per second allowing for smoother video, default is 50width 640
#This line changes the width of the image displayed, default is 640height 480
#This option changes the height of the image displayed, default is 480
Altering these options can drastically affect the performance. You may need to tinker to get the best results.
3. Now, we need to enable the Motion service by running the command below.
Using this command will make Motion start up when your Raspberry Pi powers on.
sudo systemctl enable motion
4. Once you’re done, simply save and exit by pressing CTRL + X then Y.
5. Now make sure the camera is connected and run the following line:
sudo systemctl start motion
6. If you need to stop the service, simply run the following command:
sudo systemctl stop motion
7. Now you should be able to check out the Webcam Stream at the IP address of our Pi so in your browser go to the following address.
192.168.1.103:8081
8. If the web page isn’t loading, try restarting the service.
sudo systemctl restart motion
9. If you’re using a Raspberry Pi camera, then you will need to do a few extra steps that are mentioned below.
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 the stream via the web browser.
If you want to allow external access to the camera, then check out my instructions towards the bottom of this tutorial.
Extra Steps for the Raspberry Pi Camera
If you want to use the Raspberry Pi camera module, then you will need to do a few additional steps to set it up.
Installing the Hardware
1. First, go to the CSI (Camera Serial Interface) ribbon cable slot on the Pi and with two fingers on each side of the connector pull up. This technique should now open the connector, now insert the ribbon cable with the metal leads facing away from the Ethernet Port.
2. Once you have lined it up gently press the connector back down and the cable should now be locked in one place.
Configuring the Software
To get the Raspberry Pi camera to work with Motion, we will need to do a few extra steps.
1. First make sure the camera is switched on within raspi config, enter the following command and then enable the camera (Found in interfacing options). You will need to restart once you have done this.
sudo raspi-config
2. Enter the following line to had the camera to modules. Make sure the camera is connected correctly before running this line otherwise it will thrown an error.
sudo modprobe bcm2835-v4l2
3. Alternatively, the following process should also work. First, open up the modules file by entering the following line.
sudo nano /etc/modules
4. Enter the following line at the bottom of the file if it doesn’t already exist.
bcm2835-v4l2
Once done, save and exit by pressing CTRL + X then Y.
5. Now reboot the Pi, and the stream should now work.
sudo reboot
6. You should now be able to access the Raspberry Pi webcam stream by going to the Pi’s IP address on port 8081.
192.168.1.103: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.
Saving Motion Footage and Photos
By default, you will likely run into permission problems for saving the photos and video triggered by motion. I will go through the steps below on how to set this up to work correctly.
1. Firstly, let’s make a new directory for our photos and videos.
sudo mkdir /motion
2. Next, we need to change the group of the motion folder so motion can write into it. After that, we give the group; read, write and execute permissions using the chmod command.
sudo chgrp motion /motion
sudo chmod g+rwx /motion
3. We now need to go back into the motion configuration file and change the target_dir
to our new directory. First, open up the file with the nano text editor.
sudo nano /etc/motion/motion.conf
4. Update the target_dir line, so it looks like the one below.
Make sure you remove the ;
from the start of the line.
target_dir /motion
5. Next, restart the motion service, so the configuration is loaded correctly.
sudo systemctl restart motion
6. Now, whenever the camera detects motion, it should be saved into the folder we specified above. You can check this by running the following command.
ls /motion
Be sure to check out our Linux permissions guide for more information on setting permissions correctly.
Setting Up External Access
In order to enable external access to the Raspberry Pi webcam server, we will need to change some settings on the router. However, all routers are designed differently so you may need to look up instructions for your brand of router.
Please note, opening ports to the internet comes with a security risk.
If you need a more in-depth guide, then be sure to take a look at my guide on how to set up Raspberry Pi port forwarding and dynamic DNS.
The following steps are what I did on mine in order to get it to work. My router is an AC1750 TP-Link Router.
1. Go to the router admin page (This will typically be 192.168.1.1
or 192.168.254
)
2. Enter the username and password. Default typically is admin & admin.
3. Once in go to forwarding->Virtual Server and then click on add new.
4. 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 webcam server so make it 8081
- Protocol: All
- Status: Enabled
5. These settings will route all traffic destined for port 48461 to the webcam server located at the IP address and port you provided. For example: 192.168.1.103:8081
6. 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 you’re unable to connect outside your local network then you can try the following.
- Check your router settings and confirm they are correct.
- Check your IP hasn’t changed. Some internet service providers will provide you with a dynamic IP rather than a static IP) You can set up something called dynamic DNS to counter this you can find out more information via the link mentioned above.
- Restart the router.
I hope that 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 be much more coming in the future.
Dear All
I installed motion with on my 16Gb memory card on a raspberry pi2. I followed the instructions exactly.
I got an error while loading shared libs
pi@raspberrypi ~/mmal $ ./motion -n -c motion-mmalcam.conf
./motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory
What could be the problem?
Best regards,
Ruud
The Netherlands
Hi,
I’m trying to stream the video of my raspberry pi B/raspberry pi camera (Rev. 1.3) with motion-mmal.
I did according the instructions, but most of the time there is a grey field with “unable to open video device”… Is there anything I can do to get the videostream?
The camera itself works fine (when runnint raspivid or raspistill)
Thanks
Hello!
I have an issue. When there is too much motion in the video, my stream goes off and I have to restart it ( sudo service motion stop & sudo service motion start)
Hi Gus,thanks for this post, very useful. One question however, to download the video stream, do you reckon ffmpeg? Or is there anything available out of the box? Thanks
When I sudo nano the motion.conf it is empty any ideas?
How can you start the USB Webcam automatically at Boot?
Thanks
HI I used your guide for mmal.
I used a another guide for control/monitor my Garage door and added your guide for video.
Problem is the get the mmal video I use the ip:8081 as a source in html. When attempt to get my web page that has port forwarding done, only my control/monitor stuff work and video is missing. Is there a way to pipe the mmal video to the port 80 webpage without getting the video from 8081 mini-server.
Take a look at this web page to see what I did. http://www.instructables.com/id/Raspberry-Pi-Garage-Door-Opener-with-streaming-vid/
Hi Gus! Like two others in this comment section I also get a grey box and the text “Unable to open video device”.Any idea what’s going on? I tried to do your tutorial twice, but with the same result. My cam blinks like its on just cannot get a video to show…
My camera is Pi NoIR Camera Board 5mp 1080P W/filter Night Vision Module X000QGCF5R
Running on a Pi2 B
Please Help!
Hey Jon,
There is an issue with the package I used in the Raspberry pi camera part of the tutorial. It only affects the Raspberry Pi 2.
If you check out this tutorial you shouldn’t get the issue: http://pimylifeup.com/raspberry-pi-security-camera
Hi Gus,
I need to use a general cam with USB and cable as I need to (re)position my cam from time to time to monitor a basin with my pet turtles. Any ideas or suggestions for me? Do I need a different cam or driver?
Thanks.
this did work for my RPi2…
http://pimylifeup.com/raspberry-pi-security-camera
There is a 3 second lag before the monitor updates motion, but that doesn’t appear to be a problem. I don’t see motion exclusion areas in MotionPie like in Zoneminder. but, I will keep learning this system and Linux. A big tip of the hat to all your help. Thanks.
Hey guy, found out that apparently not all the cams work (properly) on the Pi. I had a cheap cam which didn’t show the feed properly. When I attached a Microsoft HD-5000 LifeCam it worked, but as Marco mentions with a few seconds delay. I still have to figure out how to get sound though. Anyone any suggestions?
Hi Gus,
Am looking into using my Rpi2 as a server for 4x WiFi IP cameras.
Motion looks the way to go (vs zoneminder etc), do you know how to set motion up with IP cams, on the same network?
Can you tell me how to port forward to BT-HomeHub-5
How do I add a username and password for the video feed?
-Thanks
Is there a way to get audio with the video?
Thanks
I get everything to work but cannot the website to come up.
I also have another webserver running for my garage door.
Hey Paul,
Is the websever running on the same Pi as the webcam server ?
If so it probably be beneficial to change the port numbers.
Just change the references to the ports to something else such as 9090 and 9091. (Previously 8080 and 8081)
You can find these values in the config files.
sudo nano /etc/default/motion
for the webcam and
sudo nano motion-mmalcam.conf
for the raspberry pi camera.
Can this show the video on an iPad ? I can’t see on chrome
Hi Franklin,
Chrome no longer supports mjpeg streams (Annoying).
To fix the issue you can create a html file with a image tag linking to the stream:
For example
<.img src="http://:8081">
(remove the full dot at the start of the tag)
Once done just simply open the HTML file in chrome it should work.
Let me know how you go!
Thank you
hi Gus, it flashes and disappear . But it’s getting there.
Thanks again
Franklin
Hey. Great tutorial! Everything works smooth and sweet! Thanks!
Got a question though: how about some security? I’ve got a static address, opened the 8081 port for Raspberry Pi, so I am accessing my home webcam (with my kitties) from Internet (at work). Is it possible to enable a login/password request in Motion or in any way set up a security to prevent unauthorized connect?
That’s a good question, are you using a webcam or the pi camera? They use different packages. (Depending on which I’ll look into)
Thanks for answer.
I’m using a usual webcam, all settings are as described in this tutorial. I connect to the webcam thru Internet using dyndns, if that matters. Latest version of Raspbian, ethernet-connected to the home router.
I’m trying to get this going with a USB webcam with a pi 2 and have two observations:
1.
sudo motion service stop
does not stop my service, and even after running that line and a reboot, the service starts up automatically.2. On a desktop browser, I see nothing at the pi’s IP address. If I go to it on my iPad with safari I get the same result, but if I scroll with my finger I get flashes of the video feed. I suspect that the pi is sending the video feed just fine and there’s something wrong with my router settings, but I could be wrong.
I have more than ipad, one of them did so, so i installed VLC from appstore which was more specialized in receiving video streams.
Well presented project … was exactly what I was looking for; so I gave it a go.
After carefully following the directions, I had my Pi (B+) up and running double quick with the USB webcam. Problem came after 10 minutes when the cam shut down.
Worked again after re-boot but then shut down again.
Doesn’t seem to want to stay working for more than 5 to ten minutes at a time. 🙁
Have I missed something elemental here ????
Hi Gus! Like two other in this comment section I also get a grey box and the text “Unable to open video device”.Any idea what’s going on? I tried to do your tutorial twice, but with the same result.
Regards / Jimmy
Hey Jimmy,
This sounds like there is an issue between the software and the camera. Most likely an issue with the webcam driver you’re using. Can you please let me know what kind of webcam you’re using ?
Hi, I am also having same problem.
When I typed
http://IP:8081
gray screen comes out and shows ‘unable to open video device’
I am using Raspberry camera.
Thank you
I also have the unable to open video device. Occasionally there is a short moment I see feed, but it goes back to grey again. How can we check/update the driver? Or which cam would work best?
I installed the motion program, then connected a Logitech webcam and got the “Unable to open video device” message. Then I connected an old Creative VF0040 cam and all was OK.
Hi there,
I was wondering if you had to do anything extra to get your VF0040 working with your Pi. I’m getting a similar error(“Unable to open video device”) when using VF0040 with MotionEye on a RPi0 W.
thanks.
No, it worked right out of the (junk) box.
first type “sudo service motion stop”, then type “./motion -n -c motion-mmalcam.conf”
Hi
I have Raspberry Pi 2 model B and Pi NoIR camera. When i put in mmal the ./startmotion command the red led in camera starts blinking, then i put my browser the ip-address and port8081 and i see only black screen? Where is the picture?
Hi gus,
Stellar tutorial! Getting an issue on my b+.
Its the same segmentation fault that others have mentioned above. Any thoughts on a fix?