Raspberry Pi and Motion Webcam

Raspberry Pi Motion Detecting and Live Web Cam

The Raspberry makes is a fantastic device that allows you to stream live video and record motion detection. As long you have a webcam that is compatible with the Raspberry Pi, you have the basic hardware to accomplish such a task.

In order to get started with live streaming and motion detection, you need to install the Linux package titled 'motion'. Motion will handle the process of streaming and recording video. Normally, out of the box, it just works.

Installing Motion on Linux

 apt-get install motion 

Configuring Motion

After motion is installed, you will need to open a couple of files and make some simple edits. The two main files are /etc/motion/motion.conf and /etc/default/motion. Below are some code blocks that will need to be changed in order to broadcast streaming video or record motion detection.


 daemon on webcam_localhost off control_localhost off 


This file, located in the /etc/default folder requires a single change.



After the initial options are set, you may want to customize your setup. Below, are a few examples of customization; like setting the framerate equal to that of the webcam.

framerate 30  // default 1
quality 90 // default 70

Out of the box, it will record motion. But, it is sensitive to a single frame of motion. In some instances, flickering light could make it start recording a brief sequence. In order to combat this, you can increase the amount of frames that need to be contain motion.

 minimum_motion_frames 5 

Now that the cam actually runs and everything works, you can find the stored motion files in the directory defined by the target_dir option that is set in the motion.conf file. By default, the directory is tmp/motion.

Storing Motion Files On a USB Stick

If you run a Raspberry Pi on an SD card(in all likelihood), you may want to mount a usb stick on it so it records all motion to the usb stick. By doing this, you will cut down on writes to the sd card and keep files nice and organized.

To store images on a mounted USB stick,

1) get the USER ID

 id -u pi 

2) Add the user pi to fstab in order to mount for that user.

 /dev/sda1      /home/pi/mount/storage   vfat  uid=1000,noauto,user,exec   0 

3) Format a usb stick fat32

4) Mount it to a desired folder

5) Unmount it

6) Format it with mkfs.ext4. This format is critical to allow a user other than root to be able to have specified permissions with mounted media.

 mkfs.ext4 /dev/sda1 

7) Mount it again

8) Change folder permissions to 777

 chmod 777 /home/pi/myfolder 

9) Change owner to pi

 chown pi:pi /home/pi/myfolder 

When you can write to the mounted folders and they have the proper permissions, you can store the jpg images and swf files there.

Website Usage

Your Raspberry Pi will need a complete LAMP setup; or at least an installed web server like Apache. Once you have this, you can enable port forwarding in your router so your ip(or even a website) can be seen on the web via port 80. With port forwarding you can add other other forwarded ports to the router like 8080 and 8081, and port 22 for SSH, if desired.

By enabling an ssh port, you will be able to remote control all Linux boxes on your local network because once you can connect to one of them through the ssh port, you can easily connect to the others since they are all on the same local network.

Once you make the pages available to the outside world, you can create a web page that can be seen on the web with an src tag which embeds the streamed video. If you allow a local ip like to be port forwarded with port 80, you make have a file like links to another inetrnal ip like

All in all, you will use two Raspberry Pis on a local network that will be setup so that any other website can integrate the live streams into a webpage.

<img src="http://local_ip_with_motion_service:8081/" width="320" height="240"/>

Then, you can use any other online webpage and add an iframe to the website url hosted at home.

<iframe style="width:343px; height:262px;" src="http://isp_ipaddress/test.html"></iframe>