Accueil > Logiciel, Matériel, Tutoriel > Setting up Wireless motion-detect cam

Setting up Wireless motion-detect cam

28/01/2014 Categories: Logiciel, Matériel, Tutoriel Tags: ,
Print Friendly, PDF & Email

This project is based on a post by dozencrows here which explored the use of a Raspberry Pi camera board as a motion detecting video cam. It is based on the motion package in Debian. He has recompiled this with a slightly different configuration, making it convenient to work with the Raspberry Pi cam board. I take no credit for the code, but hope that details of how I have se it up on my system, and how I have connected it to a Synology NAS with built in Surveillance Station will be of interest to others. The original thread has multiple posts on it, and it can be confusing to get to grips with the code as a result.

What you need:

A Raspberry Pi, with PSU, camera board and WiFi adaptor (It would work with an ethernet connection, but it makes it less convenient for placing the camera is a suitable location. An SD card containing an uptodate copy of the wheezy-raspian distribution. If you have a blank card you can put the distribution on it together with configuring the wifi dongle by following the first section of my article here. You should also remember to check that the camera code support is enabled by typing  sudo raspi-config and setting option 5
For convenience I have ssh enabled on my card so that I can carry out the work on it via a link to a terminal on my main computer.

My wireless lan port has ip address, so I connect to the pi using
ssh pi@ from a terminal window on my mac (you can install and use  putty on Windows)

Now to get going:

First make sure (if you haven’t already) that your raspian is up to date.

sudo apt-get update
 sudo apt-get upgrade

it is also a good idea to make sure your firmware is up to date

sudo apt-get install rpi-update

and run it

sudo rpi-update

a reboot is required when this is finished

sudo shutdown -r now

(you will have to reconnect via ssh when the Rpi has restarted if you are using a remote terminal)

Lire aussi:  Tutorial: Using VMWare ESXi and PFsense as a network firewall/router

Now install the motion program

sudo apt-get install motion

We don’t actually use this version, but it does insure that all the necessary dependencies are installed as well

sudo apt-get install libjpeg62

create and enter a directory called mmal in the user pi home directory using

cd ~/
 mkdir mmal
 cd mmal

download dozencrow’s version of the motion program using


and extract the contents with

tar -zxvf motion-mmal.tar.gz

you now modify the configuration file, by opening it with the editor nano

nano motion-mmalcam.conf

nano hints
To insert a blank line, move to the beginning of a line and press return
To delete a line position the cursor on the line and press ctrl+k
use the backspace arrow to delete characters
you can search for a string by typing ctrl+w and inserting the string you wish to find.
When you have finished amending the file use ctrl+o to write out the amended file, selecting the existing filename to overwrite by pressing return. Then select ctrl+x to exit the editor.

you are going to alter the lines for

 text _left

search for them in turn using ctrl+w and set them as follows

width 640
 height 480
 target_dir /home/pi/m-video
 output_pictures off
 text_left Pi-cam %t
 logfile  /home/pi/mmal/motion.log

then write the changes out using ctrl+o push return and exit the editor with ctrl+x

So that there is no confusion as to which motion binary is running I rename the one in the mmal directory using mv motion motion-mmal

Assuming your camera board is correctly installed, you are now in a position to test it out
In your terminal window, from the mmal directory type
./motion-mmal -n -c motion-mmalcam.conf
where upon the camera board red led should light, and if you start a browser on another computer on your local network (Internet Explorer doesn’t work but Chrome, Firefox and Safari are ok) you should see live pictures at
You quit the process by typing ctrl+c

Lire aussi:  How to live stream video from webcam on Linux

The start stop process is hardly ideal, so I have written a couple of scripts to start and stop the process.

type nano startmotion
In the editor window type

 nohup ~/mmal/motion-mmal -n -c motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &

type ctrl+o then push the return key to write the text, followed by ctrl+x to exit the editor
chmod 755 startmotion to make it executable

type nano stopmotion
In the editor window type

ps -ef | grep motion-mmal | awk '{print $2}' | xargs kill

note: I put the script above in a code block as wordpress changes the quotes to smart ones otherwise which don’t work if you copy and paste

type ctrl+o then push the return key to write the text, followed by ctrl+x to exit the editor
chmod 755 stopmotion to make it executable

now you can type ./startmotion to start the camera and ./stopmotion to stop it
The videos created are stored in the m-video in the mmal home directory and the process is logged in the motion.log file
The command used in the start motion script was suggested by adayforgotten in the original topic thread. As you have seen, if you type the ./motion-mmal command to start the camera directly you cannot type subsequent commands into the terminal, and if you type ctrl+c or exit the terminal then the process terminates. Wrapping the command in nohup……& makes it run in the background and be independent of the terminal closing, which would normally stop dependent processes  The >/dev/null redirects the standard output to /dev/null, throwing it away. The “2>&1″ part redirects the “standard error” output of the command to the standard output. the “</dev/null” unattaches the console keyboard and uses “</dev/null” instead. Thus the script can start the process without producing any screen output.
The stopmotion script uses ps-ef to give a formatted list the process id of running processes, pipes the output to grep to find the line for the process motion-mmal pipes that to awk ‘{print $2}’ which extracts the second field or process ID and finally pipes to xargs which feeds in turn any matching ID to kill which terminates them. It is instructive to build up this command line manually trying each step in turn. Thus
ps -ef
ps -ef |grep motion-mmal
ps -ef | grep motion-mmal | awk ‘{print $2}’
etc to see the output of each stage in turn (you should have the process running first of course!)

Lire aussi:  Disjoncteur

Connecting the the Synology NAS surveillance module
If you have such a NAS you can log in to it with a web browser and start the supplied surveillance station. The trick is in how to setup the camera configuration.
The photo below shows the settings which have to be entered


Although the frame rate is rather slow, the system works and in the time line of the Surveillance program you can inspect video where there has been motion detected.
The surveillance program has a full help file.

Quck install files
I have put the start and stop scripts plus my amended motion-mmalcam.conf file in a zip file which you can download here into your Pi home directory using

followed by unzip
quick install instructions are included in a README file

Source: rbnrpi

Les commentaires sont fermés.