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:

Raspberry Pi (Latest version recommended for best performance)

Micro SD Card or SD Card (If you’re using an old version of the Pi)

Ethernet Cord or WiFi dongle

Raspberry Pi Camera or USB Webcam

Optional:

USB Keyboard

USB Mouse

HDMI Cable

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-get update
sudo apt-get 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 the GitHub and install it using the dpkg command.

sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.2.2/pi_buster_motion_4.2.2-1_armhf.deb
sudo dpkg -i pi_buster_motion_4.2.2-1_armhf.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 update them to the following.

  • daemon on
  • 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 setup up the daemon. First, we need to edit the motion file.

sudo nano /etc/default/motion

4. Find the following line and change it to the following:

start_motion_daemon=yes

5. Once you’re done, simply save and exit by pressing ctrl + x then y.

6. Now make sure the camera is connected and run the following line:

sudo service motion start

7. If you need to stop the service, simply run the following command:

sudo service motion stop

8. 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

9. If the web page isn’t loading, try restarting the service.

sudo service motion restart

10. 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. Once done, save & exit by pressing ctrl + x then y.

bcm2835-v4l2

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.

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.

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 service motion restart

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 outside 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.
Router Port Forwarding

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.

298 Comments

  1. James on

    Hi,
    Has your written steps changed since creating the video?

    I’ve followed the written steps on an rpi 3 with rpi camera and am getting a grey box with “unable to open video device”

    Thanks

    1. Gus on

      Written steps have changed and should work with the Pi camera or a webcam (depending on drivers etc). If you followed the video then you may run into issues getting the Pi camera to work.

    2. James on

      Hi, thanks for your comments.

      I started fresh with a new load of raspbian and it works this time. I must not have done something right. Thanks again.

      Now I need to make it more secure.

  2. Quentin on

    I got everything working, the only question i have is if its possible to add this stream to the synology Surveillance station? if yes then how?

  3. PE4000 on

    First try with my Logitech C920 HD cam on RPi 3 with powered usb-hub, video capture size 1280×720 or more…
    Viewing on W10/64 up-to-date computer / Acer 8″ tablet W10/32
    Firefox 54: it works perfect.
    Microsoft Edge: it don’t want to view, only download the stream.
    Pale Moon: also perfect viewing.

    Thanks for this great tutorial.

  4. William Quintas de Melo on

    Hi!

    Can I configure the motion to access the webcam with a link like “ip:/cam1” where ip is like “146.106.91.100” instead using the port “:8081“?

    Thanks!

  5. Metai on

    Thank you sir it works perfectly, it helped me a lot. 😀

  6. Tesso on

    Great post, just a question, how do i setup multiple webcams?

  7. sunil on

    can I send the motion detection notification with image or jpg files to the email..

  8. Apostolos on

    Hello,

    thanks for all this great details!
    When you access externally through dynDNS how can you password protect the page?

    Thanks a lot
    Apostolos

  9. Charles on

    This is great! I’ve been struggling for a while with a random bag of endless problems getting a streaming feed from a Pi. Followed loads of other ‘solutions’ elsewhere, none worked.

    Started with a fresh install, this works a treat. Thanks!

  10. theosus on

    trying this today, and using the command

    sudo apt-get install curl libssl-dev libcurl4-openssl-dev libjpeg-dev libx264-142 libavcodec56 libavformat56 libmysqlclient18 libswscale3 libpq5

    returns an error saying the package is missing or obsoleted. Any help?

  11. Ben on

    Hey,

    does anyone know how to stream this through an ardunio 4G sheild?

    I have a raspberry pi expansion kit which lets me plug in ardunio shield, however im not sure where to start with getting this working.

    I have seen it done before, however there was no open-source code provided for it!!

    Thanks in advance!!

  12. Klaus on

    Hi Gus,
    many thanks for this really nice and clear post, that’s great!

    I tried it, and on my raspberry B+ it’s working at once-
    but with a few problems:

    I tried it with 3 different usb cameras, which are all working under different windows versions, from XP up to windows 10.

    Here I found different problems:
    – Logitech and Sansun webcam: I cannot set the image size. Independent from the given width and height values I always get only a very small video size. 176×144 with Logitech, 123×159 with Sansun.
    – Hama camera with autofocus: The video size is ok, but the autofocus cannot fix, it’s always searching the fix point, even if there is no move on the video.

    Have you any ideas, how I can fix this problems? Or at least one of them? That would be great 🙂

  13. LaurieR on

    If I set “ffmpeg_video_codec mp4” it works fine and I capture H.264 mp4 files ok.
    But if I change to “ffmpeg_video_codec hevc” , which the motion.conf comment says should create H.265-encoded files, nothing is captured.

    I wonder – does Step 3 removes the H.265 codec and Step 5 doesn’t reinstall it? If so, how do I install an H.265 encoder for ffmpeg?

  14. Lorenzo on

    Hi when I go to the rpi ip address on 192.168.1.7:8081 it shows me a grey screen with the message “unable to open video device sice the date when i tried to connect”. I use a rpi0w with a picamera v2 and I’ve made the steps to activate the picamera. Can anyone helps me?
    Thankyou wery much, it’s a super guide!!

    1. Pradeep on

      I also got the same error with the Pi camera on Pi 3 but the issue in my case was that I typed bcm2835-v4l2 as bcm2835-v412 which is pure typing error. After correction, my camera is showing a small red light and video is accessible on browser. What I would advise is copy pasting the commands from start. Hope it helps!

  15. JJ on

    I get the forward as the internal listening port 8081 but where do wet change this. Also I am trying to adapt to show up to 8 cameras and I think motion eye would be the go-to for that; but what port does that listen on? I’ve watched a few tutorials but they were focused on watching and access internally. I want to get access to the camera screen from my phone for example.

  16. Bill on

    Is it possible to just get a NOOBs Pi image that is precompiled and flashed to a card for the Pi? It seems like that could help avoid the many possible mistakes that I make trying to fight through the settings, dependencies, etc.

  17. Joe on

    I’ve noticed the onboard RPI v2 camera, generates a LOT of heat within the RPI when its being used/accessed.

    I see 165’F on the PI temperature sensor.

    I may go with an external camera to keep this from happening.

  18. Paul on

    Please can you update the method.
    Step 4 no longer exists and makes this impossible to do.

    1. Gus on

      It downloads for me just fine.

  19. SG on

    The motion detection .conf file gives you the option to set a “username:password” for stream access.

    Is there a way to ban the ip (maybe through fail2ban?) that gives the wrong credentials?

    I thinking of 2 ways but don’t know how to go further:
    a. Is there any log file when someone accesses port 8081? or
    b. Can we stream motion detection video through our website instead of port 8081?

    Thanks!

  20. Laurie R on

    This works perfectly with the “EasyCap” USBTV007 composite-to-USB converter. I didn’t have to do anything other than follow the above instructions (then add permissions on the capture folder as others have mentioned). The latest Jessie / Pixel version of Raspbian includes the necessary USBTV007 drivers.

    This is perfect for a nestbox cam where you need a long cable run between the camera and the Pi.

Leave a Reply

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