Friday, May 13, 2016

Low cost Raspberry Pi Webcam Live stream With motion

If you’re after a more security like system then check out the Raspberry Pi security camera tutorial it features fully-fledged web streaming, motion detection, recording and multi camera functionality.
Raspberry Pi Webcam Server
  • Facebook
  • Twitter
  • Google+
  • Digg
  • Pinterest
  • Evernote
  • StumbleUpon
  • reddit

Equipment

Required

Optional

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.
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 it then please subscribe to me!

Installing Motion with a USB Camera

We will be using a package called motion if you want to learn more about it you can check out their website: Motion.
  1. We will be using the terminal so open up terminal on the Pi or via SSH.
  2. Let’s check for any updates for Raspbian and its packages.
sudo apt-get update
sudo apt-get upgrade
  1. First we will need to download and install motion, to do this enter the following command:
    sudo apt-get install motion
  2. Now we need to make some edits to the configuration file (motion.conf)
sudo nano /etc/motion/motion.conf
  1. Find the following lines and change them to the following.
    • daemon on
    • webcam_localhost off
    • Optional (Don’t include the text in brackets)
    • webcam_maxrate 100 (This will allow for real-time streaming but requires more bandwidth)
    • framerate 100 (This will allow for 100 frames to be captured per second allowing for smooth video)
    • width 640 (This changes the width of the image displayed)
    • height 480 (This changes the height of the image displayed)
  2. Now we need to setup up the daemon, first we need to edit the motion file.
sudo nano /etc/default/motion
  1. Find the following line and change it to the following:
    start_motion_daemon=yes
  2. Now make sure the camera is connected and run the following line
sudo service motion start
  1. If you need to stop the service simply run the following command
sudo service motion stop
  1. Now to test it out! We can check out the Raspberry Pi Web Cam Stream at the ip address of our Pi so in your browser go to the following address:
192.168.1.103:8081
There we have it a fully working Raspberry Pi Webcam server that you can place where ever you would like (Given it is within Wifi range) and be able to view via the web browser. If you want allow external access to it check out my instructions towards the bottom of this tutorial.

Installing Motion with a Raspberry Pi Camera

Motion by default doesn’t support the Raspberry Pi Camera so we will need to do a different setup for it to work. However we will still get a really good Raspberry Pi Webcam server by doing this.
Note: This method doesn’t work on the Raspberry Pi 2 & 3. There are also issues with the latest version of Raspbian Jessie.(I may have a new method coming in the near future that will be compatible) Please head over to the security camera page for a working tutorial.

Installing the Hardware

In order to install the Raspberry Pi camera we will need go to the ribbon cable slot on the Pi and with two fingers on each side of the connector pull up. This should now open the connector, now insert the ribbon cable with the metal leads facing away from the Ethernet Port.
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
  • Facebook
  • Twitter
  • Google+
  • Digg
  • Pinterest
  • Evernote
  • StumbleUpon
  • reddit

Installing the Software

Now to get the Raspberry Pi Webcam stream up and going we will need to install the required software. This process is different to the above method.
  1. Firstly we need to ensure the camera is switched within raspi config.
    Enter the following command and then enable the camera.
    sudo raspi-config
  2. Next let’s update Raspbian and its packages
sudo apt-get update
sudo apt-get upgrade
  1. Let’s also check and update the camera firmware if required.
sudo apt-get install rpi-update
sudo rpi-update
sudo reboot
  1. Now time to install motion to do this enter
    sudo apt-get install motion
    sudo apt-get install libjpeg62
  1. Now lets create a directory in your home called mmal
cd ~/
mkdir mmal
cd mmal
  1. Now  we need to download dozencrow’s motion program using:
    wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz
  2. Now we will need extract the file
    tar -zxvf motion-mmal.tar.gz
  1. Now we need to edit the config file to do this enter the following line:
sudo nano motion-mmalcam.conf
  1. Now we will need to make several changes to the config file the following lines should be changed so they appear like the this:
    • width 640
    • height 480
    • target_dir /home/pi/mmal/m-video
    • output_pictures off
    • logfile /home/pi/mmal/motion.log
    • stream_maxrate 100
    • framerate 100
  2. Now you can test it out by using the following command:
./motion -n -c motion-mmalcam.conf
  1. Now quit the process by typing ctrl+c
  2. This isn’t very good if you ever want to work on the Pi without disrupting the motion camera. To get around this we will write some scripts that can be called to easily stop and start the process. You can download these scripts here or you can simply keep following this tutorial.
  3. First lets create a file called startmotion by typing the following command:
sudo nano startmotion
  1. Now let’s type the following into the folder (A text editor (Nano) should be open)
#!/bin/sh
nohup /home/pi/mmal/motion -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
  1. Now let’s make the stop script by typing the following:
    sudo nano stopmotion
  2. Now in the editor enter these two lines
#!/bin/sh
killall motion
  1. Now we need to make both of  the scripts executable by running the following commands:
sudo chmod 755 startmotion
sudo chmod 755 stopmotion
  1. Now if you type either ./startmotion or ./stopmotion either start or stop the script and not disable the terminal so you can do other work if needed. These commands will only work if you’re inside the folder. If you want to call these outside you will need to use the following:/home/pi/mmal/startmotion or /home/pi/mmal/stopmotion
  2. You should now be able to access the webcam stream by going the Pi’s address on port 8081.
eg: 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
  • Facebook
  • Twitter
  • Google+
  • Digg
  • Pinterest
  • Evernote
  • StumbleUpon
  • reddit

Setting up outside External Access

In order to provide external access to your Raspberry Pi Webcam Server we will need to change some settings in the router. However all routers are designed differently so you will probably need to look up your own brand.
This is 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 service so 8081
    • Protocol: All
    • Status: Enabled
  5. These settings will route all traffic destined for port 48461 to the webcam server (192.168.1.103)
  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 Fowarding
  • Facebook
  • Twitter
  • Google+
  • Digg
  • Pinterest
  • Evernote
  • StumbleUpon
  • reddit
If they are unable to connect you can try the following:
  • Check your router settings and confirm they are correct.
  • Check your IP hasn’t changed (Some IPs will provide you with a dynamic IP rather than a static IP) You can setup something called dynamicdns to counter this you can find out more information here.
  • Restart the router.

No comments:

Post a Comment