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 Adam
    Adam on

    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 or 8081.
    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

    1. Avatar for Gus
      Gus on
      Editor

      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

    2. Avatar for Roman J. Sabatini
      Roman J. Sabatini on

      Type in just “motion” to see infinite monitoring status.

  2. Avatar for dean
    dean on

    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!!:)

  3. Avatar for placidseven
    placidseven on

    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?

  4. Avatar for Bryan
    Bryan on

    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.

  5. Avatar for kedar
    kedar on

    Where those images are stored in Raspberry pi.

  6. Avatar for Paul Cresswell
    Paul Cresswell on

    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.

    1. Avatar for John in Canada
      John in Canada on

      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

  7. Avatar for Wade Robson
    Wade Robson on

    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.

    1. Avatar for Gus
      Gus on
      Editor

      Hi Wade,

      Try turning off the following two options in the /motion.conf file.

      output_pictures off
      ffmpeg_ouput_movies off

  8. Avatar for Miguel
    Miguel on

    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!

  9. Avatar for Chandra
    Chandra on

    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.

    1. Avatar for Kuba
      Kuba on

      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.

  10. Avatar for sanjay
    sanjay on

    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

  11. Avatar for ed
    ed on

    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?

  12. Avatar for Kuba
    Kuba on

    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?

  13. Avatar for Phil
    Phil on

    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?

  14. Avatar for Kiran
    Kiran on

    It worked for some time but stopped working after few seconds with an error Website seems to be unavailable

    Could not connect : Connection Refused

    1. Avatar for Chandra
      Chandra on

      Same issue here, did you find any solution?

      Thanks

    2. Avatar for Kuba
      Kuba on

      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.

  15. Avatar for James
    James on

    Can anybody help with the problem of the webcam requiring the browser to refresh to get the latest image? The image is still

  16. Avatar for Buddy
    Buddy on

    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?

  17. Avatar for Paulo
    Paulo on

    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

  18. Avatar for Alan
    Alan on

    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?

    1. Avatar for Yohnnyjoe
      Yohnnyjoe on

      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.

  19. Avatar for James
    James on

    Oops.. Sorry. I didn’t realise it went through twice.

    1. Avatar for Gus
      Gus on
      Editor

      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.

    2. Avatar for Jim
      Jim on

      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!!!

  20. Avatar for Jim
    Jim on

    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!!!

Leave a Reply

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