Raspberry Pi As Low-cost HD Surveillance Camera

We now need to install it using the command line (log in to your raspberry as user “pi”):

sudo apt-get install motion 

Some packages will be installed in the installation process; just type “y” to proceed with the installation.
As the current version of motion does not (yet) support the Raspberry camera module, we need to download and install a special build with support for this camera module.

cd /tmp 
sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev
wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz  

And now you need to unpack the downloaded file to the /tmp directory:

tar zxvf motion-mmal.tar.gz  

After this unzipping, you now need to update your installed motion with the downloaded build:

sudo mv motion /usr/bin/motion
sudo mv motion-mmalcam.conf /etc/motion.conf  

You also need to enable the motion daemon so that motion will always run:

sudo nano /etc/default/motion

and change the line to:

start_motion_daemon=yes

We’re pretty sure that the official build of motion will shortly also support the Raspberry camera module as well.
A very important command to edit the motion configuration file is

sudo nano /etc/motion.conf  

Note: in the standard motion installation, the motion.conf is in /etc/motion/, but in the special motion-mmal build from dropbox-url (see above) it’s in /etc/. If you follow this tutorial with all steps, this is no problem at all.
Be sure to have the file permissions correct: when you install motion via ssh while being logged in as user “pi”, you need to make sure to give the user “motion” the permissions to run motion as service after reboot:

sudo chmod 664 /etc/motion.conf
sudo chmod 755 /usr/bin/motion
sudo touch /tmp/motion.log
sudo chmod 775 /tmp/motion.log  

We’ve made some changes to the motion.conf file to fit our needs. Our current motion.conf file can be downloaded
Some of the main changes are:
Make sure that motion is always running as a daemon in the background:

daemon on 

A very good (and free open-source) motion detection/surveillance software with many configuration options is motion We now need to install it using the command line (log in to your raspberry as user “pi”):Some packages will be installed in the installation process; just type “y” to proceed with the installation.As the current version of motion does not (yet) support the Raspberry camera module, we need to download and install a special build with support for this camera module.And now you need to unpack the downloaded file to the /tmp directory:After this unzipping, you now need to update your installed motion with the downloaded build:You also need to enable the motion daemon so that motion will always run:and change the line to:We’re pretty sure that the official build of motion will shortly also support the Raspberry camera module as well.A very important command to edit the motion configuration file isNote: in the standard motion installation, the motion.conf is in /etc/motion/, but in the special motion-mmal build from dropbox-url (see above) it’s in /etc/. If you follow this tutorial with all steps, this is no problem at all.Be sure to have the file permissions correct: when you install motion via ssh while being logged in as user “pi”, you need to make sure to give the user “motion” the permissions to run motion as service after reboot:We’ve made some changes to the motion.conf file to fit our needs. Our current motion.conf file can be downloaded here . Just download, unzip and copy the containing motion.conf to /etc/motion.conf if you would like to use the exact config options we describe below.Some of the main changes are:Make sure that motion is always running as a daemon in the background:

We want to store the logfile in /tmp instead (otherwise autostart user won’t be able to acces it in /home/pi/ folder):

logfile /tmp/motion.log

As we want to use a high quality surveillance video, we’ve set the resolution to 1280×720:

width 1280
height 720 

We don’t need real-time video, 2 pictures per second are totally ok for our needs:

framerate 2 

This is a very handy feature of the motion software: record some (2 in our configuration) frames before and after the motion in the image was detected:

pre_capture 2
post_capture 2 

We don’t want endless movies. Instead, we want to have max. 10 minutes slices of the motion videos. This config option was renamed from max_movie_time to max_mpeg_time in motion. If you use the motion-mmal build, this one will work. If you get an error ‘Unknown config option “max_mpeg_time”‘ either change this one to max_movie_time or make sure to really use the motion-mmal build as shown above.

max_mpeg_time 600 

As some media players like VLC are unable to play the recorded movies, we’ve changed the codec to msmpeg4. Then, the movies play correctly in all players:

ffmpeg_video_codec msmpeg4  

Enable access to the live stream from anywhere. Otherwise only localhost (= the Raspberry device) would be allowed to access the live stream:

stream_localhost off 

If you want to protect the live stream with a username and password, you should enable this:

stream_auth_method 2  
stream_authentication SOMEUSERNAME:SOMEPASSWORD 

All configuration parameters are explained in detail in the
After your changes to the motion.conf, reboot the Raspberry:

sudo reboot  

After the reboot, the red light of the camera module should be turned on, which shows that motion currently is using the camera to detect any movement.

As we want to use a high quality surveillance video, we’ve set the resolution to 1280×720:We don’t need real-time video, 2 pictures per second are totally ok for our needs:This is a very handy feature of the motion software: record some (2 in our configuration) frames before and after the motion in the image was detected:We don’t want endless movies. Instead, we want to have max. 10 minutes slices of the motion videos. This config option was renamed from max_movie_time to max_mpeg_time in motion. If you use the motion-mmal build, this one will work. If you get an error ‘Unknown config option “max_mpeg_time”‘ either change this one to max_movie_time or make sure to really use the motion-mmal build as shown above.As some media players like VLC are unable to play the recorded movies, we’ve changed the codec to msmpeg4. Then, the movies play correctly in all players:Enable access to the live stream from anywhere. Otherwise only localhost (= the Raspberry device) would be allowed to access the live stream:If you want to protect the live stream with a username and password, you should enable this:All configuration parameters are explained in detail in the motion config documentation After your changes to the motion.conf, reboot the Raspberry:After the reboot, the red light of the camera module should be turned on, which shows that motion currently is using the camera to detect any movement.