Build a Raspberry Pi Webcam Server in Minutes

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.

Raspberry Pi Webcam Server

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

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 1
  • framerate 100 Changing this option will allow for 100 frames to be captured per second allowing for smoother video, default is 50
  • width 640 #This line changes the width of the image displayed, default is 640
  • height 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.

raspberry pi USB webcam in action

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.

Clips and Ribbon Cable

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.

Webcam Server Browser

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

Router Port Forwarding

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.

300 Comments

  1. Avatar for Cliff
    Cliff on

    What do I need to access this off my home network? It works fine as long as both are connected to home wireless. However, if I try to access via my phone on mobile network it times out every time. Any thoughts?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Cliff,

      You will need to portforward the port used by the webcam server on your Raspberry Pi. In the case of this tutorial that port should be 8081.

      You will also need to make sure that you connect using your actual IP address and not the local IP address.

      Cheers,
      Emmet

    2. Avatar for Cliff
      Cliff on

      Will disabling localhost throughout the config file yeild better results for remote viewing? See below from another forum.

      forgot to say the motion.conf file seems to have changed since this article has been posted the first time so llok for every occurence of localhost and turm them to off

      # Restrict stream connections to localhost only (default: on)
      stream_localhost off
      # Restrict control connections to localhost only (default: on)
      webcontrol_localhost off

    3. Avatar for Emmet
      Emmet on
      Editor

      Hi Cliff,

      That should definitely help with gaining remote access.

      I will have to look into updating this tutorial over the next few weeks.

      Cheers,
      Emmet

  2. Avatar for Pi3NOOB
    Pi3NOOB on

    Hello!

    I try to configure Pi3 for web camera by using your Guide. I have troubles since “sudo nano /etc/motion/motion.conf” – I don’t know how to work it. I try to write and save
    daemon on
    stream_localhost off,
    but I didn’t find the necessary strings, I put F1, but it looks like the man.
    Please help! May be there is expanding upon.

    1. Avatar for Gus
      Gus on
      Editor

      Hi,

      Assuming you’re using the nano editor you can search for text using ctrl+w then trying your search phrase, then enter. You can do this multiple times until you find your word.

      Once you have updated the file, press ctrl+x, then y and finally enter. The file should now be updated and saved.

      If they don’t exist, you can add them, and the program will pick them up.

      Hope that helps.

  3. Avatar for Jim
    Jim on

    This is working great for me! I work from my home and it’s not easy for me to know when the mail has arrived. But with this set up, I have a little window open on my desktop and I can see when the mail carrier shows up. Because he takes a very specific route, I can set up the camera to monitor that and send me a notification when the mail is here.

    It saves me checking half a dozen times between 3:00 and 5:00 (the range of times he might arrive).

  4. Avatar for Muthu Ram Prabhu
    Muthu Ram Prabhu on

    Hi,

    Thanks for this post. It helped a lot. I followed the procedure and everything was working fine.
    After a couple of days, the Pi would not reboot and gave a blank screen. The reason was due to low storage. I had to flash the pi again and set up everything from scratch. Again after a few days, the issue happened.

    Is it because the video is logged to a file? how do I avoid this?

    Thanks

    1. Avatar for Gus
      Gus on
      Editor

      Hi,

      Yes, it is logged. I recommend switching both the picture_output and movie_output options from on to off. These options should be in the motion.conf file.

      I hope that helps.

  5. Avatar for Geoff Wheeler
    Geoff Wheeler on

    Thanks for providing this information, it worked first time for me! Other coding did not work.

    I might have missed this, but next step for me, how do I add scrolling text to this video stream?

    1. Avatar for Gus
      Gus on
      Editor

      Hi Geoff,

      As for as I am aware scrolling text is currently not supported by the Motion software package.

  6. Avatar for Philacious
    Philacious on

    Awesome tutorial! Worked on the first try. Thanks for the great instructions.

    Next to look into setting up recording to network storage!

  7. Avatar for Gene Vogt
    Gene Vogt on

    New to Linux, new to Raspberry Pi, old-hand at MS DOS & Windows. Got a Pi 3, installed Raspbian using NOOBS, started cutting and pasting the commands in the article into the Terminal window, seemed to be going well for a long time. First hint of a possible problem was with the “sudo apt-get remove libavcodec-extra-56 libavformat56 libavresample2 libavutil54” command when the system was unable to locate four packages; libavcodec-extra-56, libavformat56, libavresample2, libavutil54. Proceeded from there in the same manner, got occasional errors. When done, motion service was not found. What did I do wrong? (I captured the complete trace if that would help.) I’ve got a USB camera I want to be able to monitor on-demand and having a web server port I can tap into seems ideal.

    1. Avatar for Gus
      Gus on
      Editor

      Hey Gene,

      Errors occuring from sudo apt-get remove libavcodec-extra-56 libavformat56 libavresample2 libavutil54 about packages not existing is not a bad thing, as this command is trying to remove them anyway.

      It would be helpful to know what these others errors you ran into were? If you intend on sending the complete trace i recommend you utilize a service such as gist.github.com and linking from there rather then pasting the whole thing here.

      Cheers

  8. Avatar for Jörg Günther
    Jörg Günther on

    Hello Gus

    I have stretch and made with you wrote but the USB Cam which is connected to an USB-switch (with own and good net aggregate) shows only a blank grey screen with time and date. What could have went wrong ? Do you have an idea?

    Thanks from Sweden
    Jörg

    1. Avatar for Gus
      Gus on
      Editor

      Sounds like it could be an issue with the webcam drivers or something similar.

      Does it have the same issue if you connect it directly to the Raspberry Pi?

    2. Avatar for Jörg Michael Günther
      Jörg Michael Günther on

      Hej Gus
      Thank you for answer. I must test it tomorrow 😉
      WBR
      Jörg

  9. Avatar for Teress
    Teress on

    How is the progress with multiple USB cams setup? Is it possible? Any tutorial?
    Using your tutorial I have one USB cam working flawlesly. THANKS A LOT.

  10. Avatar for Ed
    Ed on

    What is this mmal folder that everyone has? I went through the tutorial and get an empty gray screen when I look in my browser, but I don’t even have this folder that I am seeing discussed in the comments

    1. Avatar for Erez Krimsky
      Erez Krimsky on

      ….and I had a typo. I typed “bcm2835-v4l2” as “bcm2835-v412.” Thanks for the tutorial!

  11. Avatar for Gil
    Gil on

    well, its sept 2017 – using raspbian 9 – once i started thinking, saw where i was screwing up. this works like a charm. i’m using a ps3 motion camera – its a little choppy – may have to work on the refresh rates to see what happen.

  12. Avatar for Job
    Job on

    Hi sir. Your work is awesome!
    And I want to do it in 2 usb webcam. How can I access to view it simultaneously in my web browser.

  13. Avatar for Rick Yoder
    Rick Yoder on

    Hey there!

    Just tried this out on a Pi 2 model B and the video is a bit choppy still.

    Is the bottleneck the Pi or my LAN in this case? I’m only doing 480p at 40fps.

    1. Avatar for Gus
      Gus on
      Editor

      Most likely the Pi, I have to run mine on a Pi 3 to get smooth video playback.

  14. Avatar for Erjan
    Erjan on

    My USB webcam has also a microphone.
    Is there a way to blend in sound?

  15. Avatar for Radoslav
    Radoslav on

    How can I run 2 or more cams in one time? Please tell me in detail.

  16. Avatar for Nigel
    Nigel on

    3.8.2017
    Hi Gus,
    I see that this article is from 2015. The latest Jessie 2017 seems to have some problems with motion, so I have heard.

    I would love to see an updated version and seventh heaven would be adding control for pan/tilt servos.

    1. Avatar for Gus
      Gus on
      Editor

      Hey Nigel,

      I have tested and updated this tutorial since its initial release and have made several adjustments to it a couple of months ago for Raspbian Jessie. It should work perfectly fine on Raspbian Jessie.

      Cheers

  17. Avatar for Muz
    Muz on

    Works great on Chrome and FireFox, but I can’t seem to figure out how to display live video in MS Edge or MS Explorer?

  18. Avatar for Bonzadog
    Bonzadog on

    27.7.2017
    mmm,I had submitted a comment but it appears to be lost.

    WIn 10 Firefox Browser, RPI 3 + 16GB + latest Jessie OS , Microsoft HD Web Camera plugged directly in the RPI USB.

    I performed steps 1-12 and double-checked the conf files.
    After entering ip-address:8081 I get
    1) A “Not connected ” message
    OR
    2) a video with a slow frame rate of say 1-3 seconds although in .conf framerate is set to 100.
    Worse is that the video exists for 2-20 seconds then I get a “non connected “message .
    Is it motion, my WebCam, perhaps tweeting another conf file?

    Someone please assist. I cannot use this product as it is.
    Thank you
    BD
    I

  19. Avatar for Javi
    Javi on

    Hi Gus. Very interesting and detailed tutorial, but i cant make it work with RPi2 🙁
    Maybe it wont work at all with this model?
    thanks 🙁
    Javi (Spain)

    1. Avatar for Gus
      Gus on
      Editor

      Hi Javi,

      It should work just fine, I tested it a few weeks ago with the RPi2. What kind of issues/errors are you facing?

  20. Avatar for Markw
    Markw on

    Thank you. This was a well written tutorial. Everything came up as advertised. Next is to learn more about MotionEye.

Leave a Reply

Your email address will not be published. Required fields are marked *