This tutorial will take you through how to set up a webcam server on the Raspberry Pi. You can have your very own Webcam visible on a web page.
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. This software will allow us to stream footage from a webcam connected to the Raspberry Pi. You will be able to view the stream via a web page in your favorite browser.
If you’re after more of a security-like system, 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 it working, especially if you have hardware that differs to the versions used in this tutorial. If you can’t get this to work, 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 ( Amazon )
- Micro SD Card ( Amazon )
- Ethernet Cable ( Amazon ) or Wi-Fi ( Amazon )
- Raspberry Pi Camera ( Amazon ) or USB Webcam ( Amazon )
Optional
Setting up the Webcam Server on the Raspberry Pi
Firstly, we will need to install Raspberry Pi OS onto the Raspberry Pi. If you haven’t already done this, check out our guide on installing Raspberry Pi OS. It will take you through all the steps that you will need to do to get up and running.
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 OS, so you’re running on the latest packages.
sudo apt update
sudo apt upgrade
3. Next, install the following packages. This command will work on the full and lite versions of Raspberry Pi OS.
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 libcamera-v4l2
4. Next, download the Motion deb file for Raspberry Pi OS from GitHub using the wget command and install it using the dpkg command. You can view all the releases for Motion over at their Github.
Run the two lines below to download and install Motion.
sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.6.0/$(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb
sudo dpkg -i $(lsb_release -cs)_motion_4.6.0-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 Raspberry Pi.
Configuring Motion
1. We need to edit the configuration file, motion.conf. To do this, open the file in the nano text editor.
sudo nano /etc/motion/motion.conf
2. Find the following lines and ensure they are set to the values below.
daemon off
stream_localhost off
Note: Change the following two lines from on to off if you have 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 and resources. It 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. Once done, save and exit by pressing CTRL + X then Y.
4. We need to enable the Motion service by running the command below.
This command will make Motion start when your Raspberry Pi powers on.
sudo systemctl enable motion
5. Ensure the camera is connected and run the following line.
sudo systemctl start motion
If you need to stop the service, run the following command.
sudo systemctl stop motion
6. You should be able to check out the webcam stream at the IP address of the Raspberry Pi. To get the IP address, use the hostname command.
hostname -I
7. In your browser, go to the IP address of your Raspberry Pi, followed by the port 8081.
192.168.1.121:8081
If you are using a Raspberry Pi camera, you will need to do a few extra steps, mentioned further down the page. You will see a grey box with an error if you do not do these steps.
8. If the web page isn’t loading, try restarting the service.
sudo systemctl restart motion
9. You should now have a fully working Raspberry Pi Webcam server that you can place wherever you would like (Given it is within Wi-Fi range) and be able to view the stream via the web browser.
If you want to allow external access to the camera, check out my instructions at the bottom of this tutorial.
Additional Steps for Using a Raspberry Pi Camera
If you want to use the Raspberry Pi camera module, you will need to do a few additional steps to set it up.
A Raspberry Pi 5 uses a different port, so you may need to acquire a ribbon cable (Amazon) designed to fit into the new ports.
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. This port is labeled CAM/DISP 0 on the Raspberry Pi 5.
2. Insert the ribbon cable with the metal leads facing away from the Ethernet Port. If you are using a Raspberry Pi 5, the metal leads must face toward the ethernet port.
3. Once you have lined it up, gently press the connector back down, and the cable will lock in place.
4. Since the Raspberry Pi camera relies on a library called libcamera we will need to edit the service file. To do this, enter the following line.
sudo nano /lib/systemd/system/motion.service
5. In this file, find the line that contains the following text.
ExecStart=/usr/bin/motion
Update the line to the following text.
ExecStart=/usr/bin/libcamerify /usr/bin/motion
Once done, exit the file by pressing CRTL + X and then Y.
6. Since we just changed the source service file, we will need to reload the systemctl daemon. You can do this by running the following command.
sudo systemctl daemon-reload
7. Lastly, restart the motion service to ensure our changes have taken effect.
sudo systemctl restart motion
8. 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.121:8081
You should now have a fully working Raspberry Pi camera server accessible within your local network. If you want to allow external access to the camera, please follow my instructions below.
Saving Motion Footage and Photos
By default, you will likely have permission problems when saving the photos and video triggered by motion. I 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 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. Ensure you remove the ;
from the start of the line.
target_dir /motion
5. Next, restart the motion service to load the configuration correctly.
sudo systemctl restart motion
6. When the camera detects motion, it will be saved into the specified folder. You can check this by running the following command.
ls /motion
Check out our Linux permissions guide for more information on setting permissions correctly.
Setting Up External Access
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 router brand.
Please note that opening ports to the internet comes with a security risk. Please use strong security measures to prevent unauthorized access to your local network.
If you need a more in-depth guide, be sure to look at my guide on how to set up Raspberry Pi port forwarding and dynamic DNS.
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 dynamic DNS to counter this.
- Ensure that the port mapping matches correctly with the motion port. For example, the internal port must be set to 8081.
- Restart the router.
Conclusion
I hope you enjoyed this tutorial on how to build a Raspberry Pi webcam server. You should now be able to view your camera stream using a web browser.
If you want more projects, please check out all our Raspberry Pi projects. We are always working on more, so be sure to check back in the future or stay up to date by signing up to our newsletter.
Please feel free to use the comment section below to provide feedback for this tutorial.
Hi Thanks for the tutorial,
I did this one more the 3 times and I still don’t get any stream in my internal network when i go to
192.168.1.4:8080
or8081
.Wanna throw the raspberry pi out of the window Lol,
I’m using raspberry jessie and did all the updates and upgrades, testing the camera with normal basics are going please Advice.
Thanks
Is there an error on the webpage or is it just timing out altogether ?
You can check to see motion is running by using the following line.
sudo service motion status
Type in just “motion” to see infinite monitoring status.
thanks for the great article….newbie here:) I got it working with my lifecam webcam but it only stays on for a few seconds. I did not do the router changes yet. any thoughts on what i should do next?
Thanks for your help!!:)
If you follow these instructions coupled w/ your DNS instructions, you create a webcam with unrestricted access? Anyone can see your webcam…Is there a way to secure this aside from setting up a web server to access the camera?
Hi, I have an RPi 3 and the RPi 8M pixel camera.I can’t connect to the video stream from the camera. I’m stuck at instruction 14: “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:” I tried the IP address in the tutorial, and the one I use to SSH to my RPi 10.0.1.13, but neither address will bring up the video stream in a browser on my Mac or on the RPi. Can you help? Thanks.
Where those images are stored in Raspberry pi.
Hi, Great tutorial but im struggling with finding the webcam_localhost off line in the conf file.
I followed all other instructions and get 192.168.1.107 refused to connect.
any help gratefully received.
Paul!
I hunted (more than) my fair share of MotionEye (v20161125) bugs
I have found !answers! to “where” (any file is…) with this method:
(1) Login using puTTY…
(2) command “cd / ” (not the double-quotations)
(3) command “find . -name ‘mot*.conf’ ”
and then study the results listed;
(my fresh setup)
./data/etc/motion.conf
./data/etc/motioneye.conf
./etc/motioneye.conf
(4) a quick look at “nano ./data/etc/motion.conf” and
observe the second active line for
“webcontrol_localhost on” ( by default)
is this it? do you see the file(s) as I do with ‘find’?
my setup works fine from browser (within same IPsegment subnet)
to motioneye (static IP) or stream 8081 to other lappies, or
RTSP to the (Linux based) security cam recorder (at 4fps)
with enough rez to see License Plates / faces in my driveway
I do recall swapping microSD, t-shooting 2 days
and all that time , could not find nano /etc/motion/motion.conf
so the above recursive FIND -name command with wildcard
did the job, and continues to find files that have
wandered directories under newer versions
and that never makes it into the tutorials …[insert rant here]
/* your note;
webcam_localhost=OFF
setting should be just for HDMI and “local” with respect to
the RPi hosting the motioneye [?]
I do not get how it would effect the http control page access.
(setting=on) does not impede my browser streaming (:8081)
nor http: control (:80) streaming here. [?]
There must be something else… IMHO */
try it as bot ON and OFF and let me know if your control page access is regained
My guess?
You have to set your browser to allow cookies and (updated) java execution to the control web page.
early on
I had 3 examples running;
I discovered on all 3 systems that the microSD cards are the #1 cause of freezes, crashes and so forth/ wastes of time.
None of the uSDcards are equal
even among same brand/type , but CLass 10 seem to work 100%.
I tried to repurpose older 2GB from Garmin GPS upgrades-
a time wasting mistake, causing http crashing/freezes/SSH issues.
Overclock requires really new fast uSDcards.
Now that I have one working uSDcard 8GB CL10 as a spare,
I know I can test similar RPi B v1.2’s with v1.3 5MP cams from ePay with 100% confidence.
John
This is the first set of instructions on any project I’ve ever got working on the first pass, so A+ for that. The server streams perfectly until somethings big walks in front of the camera (like a person) then it appears to stop. So I must have some kind of motion detection setting wrong, but I can’t figure out where it is in the conf file. Can you point me in the right direction? I’m using a B+ with a USB camera.
Hi Wade,
Try turning off the following two options in the
/motion.conf
file.output_pictures off
ffmpeg_ouput_movies off
Hi!
I have followed the tutorial and I managed to establish a streaming from my Pi.
The only thing is that the screen is black! Despite that I can see the date and hours..
I have a old Creative Labs Wecam and a Pi 2 model B,
Any hint?
Thanks!
Hi,
First of all thank you very much for this tutorial, it helped me a lot to stream video from my raspberry pi3. I am using USB webcam and installed ‘motion’ today, I don’t see webcam_maxrate and webcam_localhost under /etc/motion/motion.conf file. It seems to me that in recent version of ‘motion’ these 2 are replaced with ‘stream_localhost’ and ‘stream_maxrate’ respectively. I modified the motion.conf file and started motion.
The problem is, I could see the video stream for 10 to 20 seconds from another machine in same network and it disconnects. Don’t know why.
Appreciate your help.
Thanks.
Hi, check out my solution I’ve replied to you:
Hi, I found that there is probably a problem with or during saving (or trying to save) movie after motion detection. I’ve changed the event_gap from “60” to “-1”. Try this, I hope it will help.
And I’ve disabled movie and photo saving after motion detection.
Can this work with external camera on WIfi network ? I want to record video on SD card on Raspberry PI 2 and camera is Wifi Foscam
Great tutorial. Sadly though I encountered a problem.
At first on :8081 I got the video dimensions but the message “unable to open video device”. Thought maybe it is a power issue as the usb cam was fed from the PI. So I added a powered hub
That gave me indeed an image, only briefly. then it disappears, telling me again ‘unable to open video device’, then I briefly get a picture again, then ‘unable to open video device again etc etc.
Any solution for that?
Hi, great tutorial but my video stream stops after few seconds (e.g. I have blue light on my webcam when working and after few second this light turns off). Any idea why is that?
Hi. Nice tutorial. I don’t know if it’s anything to do with Motion being moved to new dev’s but when I follow your instructions I can’t find webcam_localhost in the config file. I’ve found stream_localhost and set that to ‘off’ (which stops the stream being restricted to localhost) but I can’t connect via a browser. Any ideas?
It worked for some time but stopped working after few seconds with an error Website seems to be unavailable
Could not connect : Connection Refused
Same issue here, did you find any solution?
Thanks
Hi, I found that there is probably a problem with or during saving (or trying to save) movie after motion detection. I’ve changed the event_gap from “60” to “-1”. Try this, I hope it will help.
Can anybody help with the problem of the webcam requiring the browser to refresh to get the latest image? The image is still
I followed the guide completely using a Raspberry Pi 3 and a USB webcam. When I connect to it I can watch the feed for about 5-10 sec. then I lose connection and I notice my webcam’s light goes out. I tried running the server without connecting and the same thing happened. Am I missing something?
Hello, First of all thanks for this great tutorial. I´m trying this with a Raspberry pi 3 with a HP HD 2300 and it works but only for 20 seconds after that it stops the streaming. Can anyone help me.
Regards
I am not a Linux expert, but I believe I followed the first set of directions properly. When starting the service I am getting an error message “chown: invalid user: `motion:motion'”, which apparently is a file permissions issue. Any idea why this might be happening?
Did you change your username from pi? I had that issue and fixed it with adding user to proper group. I’m on mobile but I think I left a reply here awhile back that explains how to.
Oops.. Sorry. I didn’t realise it went through twice.
That’s ok, it can be confusing! spam checks, manual approval etc. often makes it look like the original comment never made it through or was removed.
This isn’t normal behaviour and not sure of a work around. It may be a bad driver for that webcam.
What model is it? I’ll look into it when I have some spare time.
Gus,
Your response inspired me to try a bit newer webcam. The original was an old USB 1 logitec… The newer one is a 1080p (cheep from china.) PROBLEM SOLVED. It now finds the camera on reboot.
THANKS AGAIN!!!
Gus,
When I reboot the pi motion can’t find the webcam. Once I unplug the USB camera and then plug it back in (re-plug? 🙂 ) motion finds it and all is well again.
Is that normal behavior? Is there a work around? It becomes problematic when I’m off sight.
THANKS!!!