Setting up Wireless motion-detect cam UPDATED


I have received many requests for help with this project since I first wrote it over a year ago. So I decided to take another look at it. As it said at the beginning of the original article, the main input came from a posting by dozencrows, in the RaspberryPi Forums. He has in fact done further work on the original article, and a much better implementation can be downloaded from this post here. The actual download link for the modified motion file and associated config file is here. I have tried this out today on the very latest raspbian distro. As in the original article you should install motion and libjpeg62. Then download dozencrows modified motion and config file. For safety I suggest renaming HIS version of the motion file motion-mmal and using the set up and run command from my original article. That way you don’t run the installed motion binary by mistake.

./motion-mmal -n -c motion-mmalcam.conf

I tried this with his config file as downloaded, but you can of course modify it to meet your own requirements.

I also checked this out with my Synology NAS running DSM 5 with surveillance station 6.3 

Set up the camera as per the picture in the original article, and you can then record using that program. I hope that this update will make it easier for readers to get this going.
The original article follows below.

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)

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

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

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

66 thoughts on “Setting up Wireless motion-detect cam UPDATED

  1. Very handy writeup, thank you! I set my configuration to 640×480 resolution and 3 fps. That works, and the AVI generated by motion-mmal as you describe can be played by VirtualDub 1.9.11 on Windows XP, but it always gives me the following warnings:

    [!] AVI: Index not found or damaged — reconstructing via file scan.
    [!] AVI: Keyframe flag reconstruction was not specified in open options and
    the video stream is not a known keyframe-only type. Seeking in the video
    stream may be extremely slow.

  2. Thanks for this tutorial. It’s been a huge help.

    However, I had issues with the stopmotion script. Is there any reason why one can’t simply use:

    killall motion-mmal

    It seems to work well for me.

  3. Thank you very much for this fantastic approach.
    I have a problem with the movies which are stored in /home/pi/m-video/.
    Is it possible to deactivate this feature and that only the streaming-function is working? Is there a parameter in motion-mmalcam.conf which can disable saving movies?

    • I’ll try and check for you. I think there may be something on this on the thread on MMAL on the Raspberry Pi Forum. Even if you can’t stop them, you could add a script to delete the oldest if space is an issue.

  4. Is there a restriction on the 640×480 size here? The pi cam can do full HD.

    I’m experiencing some stability issues with motion (with USB camera) but can get 720p quality currently… May be I should reduce to this lower resolution too.

    • Hi Mike
      The originator of the MMAL thread on …

      dozencrows wrote:
      A note on image resolution…

      Currently, only certain resolutions work. My personal testing has shown the following work:


      Hope this helps

  5. Thanks for the tutorial, it works really well, except it doesn’t use my raspberry pi camera board, instead it uses my usb camera. Is there a way I can select to use the camera board instead?

  6. Hi
    Thanks for the tutorial. I was having a bit of trouble setting this up before but starting from scratch using your tutorial has made it easier.
    FYI It looks like (so far at any rate) setting ffmpeg_output_movies to “off” in motion-mmalcam.conf will stop generation of avi files in the m-video directory.

  7. Hi, i’ve been having loads of problems recently with motion, i managed to set it up with a webcam a while back and have it working, but last monday started again with this project and nothing is working, webcam doesnt stream… I had several model of webcam and the board cam which is why im here trying with this as i tought it might be better to troubleshoot with this. it is generating videos on m-video folder but its not streaming, when i go IP:8081 what could I try to find out whats going wrong?


  8. By me the ./stopmotion command dont work…the error: awk: 1: unexpected character 0xe2
    Then i see in nano the quotes are not right. When you copy/paste from wordpress to nano is not the same.

    WordPress seems to be messing up the single and double quotes, you need to remove them and retype them in.

    • Well it does say type in the article not copy and paste. I didn’t put it in a code block where it would have copied ok.
      There is always the option of using the download for the files.
      I’ll see if I can alter it so it will copy and paste ok.

      • put this script in a code block so you CAN copy and paste it ok. Previously wordpress altered the quotes to smart ones which dont’ work.
        Thanks for the comment John

  9. Wonderful manual (in particular if I compare it to the almost stupid stuff that is copy-and-pasted elsewhere). Still, I get a problem. My log says

    [-1242889376] [NTC] [ALL] [Nov 09 22:14:28] motion_startup: Using log type (ALL) log level (WRN)
    [1] [ALR] [VID] [Nov 09 22:14:28] mmalcam_start: MMAL Camera thread starting… for camera ( of 1024 x 576 at 2 fps
    [1] [ALR] [VID] [Nov 09 22:14:28] mmalcam_start: MMAL Camera using video capture
    [0] [ERR] [ALL] [Nov 09 22:14:58] main: Thread 1 – Watchdog timeout, trying to do a graceful restart
    [0] [ERR] [ALL] [Nov 09 22:15:10] main: Thread 1 – Watchdog timeout, did NOT restart graceful,killing it!
    [0] [ERR] [ALL] [Nov 09 22:15:58] main: Thread 1 – Watchdog timeout, did NOT restart graceful,killing it!
    [0] [ALR] [VID] [Nov 09 22:15:58] MMAL Camera cleanup
    [1] [ALR] [VID] [Nov 09 22:15:59] mmalcam_start: MMAL Camera thread starting… for camera ( of 1024 x 576 at 2 fps
    [1] [ALR] [VID] [Nov 09 22:15:59] mmalcam_start: MMAL Camera using video capture
    [0] [ERR] [ALL] [Nov 09 22:16:28] main: Thread 1 – Watchdog timeout, trying to do a graceful restart…

    Any idea what is going on here?

  10. Hi! Thank you a lot for this very simple tutorial. It’s so simple that I can’t get motion working…

    I d’ont use yet the scripts to start and stop the stream. So I type this :
    ./motion-mmal -n -c motion-mmalcam.conf

    And it stay stuck here :
    [-1242705056] [NTC] [ALL] conf_load: Processing thread 0 – config file motion-mmalcam.conf
    [-1242705056] [NTC] [ALL] motion_startup: Motion mmaltest Started
    [-1242705056] [NTC] [ALL] motion_startup: Logging to file (/home/pi/MMAL/motion.log)

    Safari can’t access to the server when typing http://(RPIaddress):8081

    The motion-mmalcam is very close to yours.

    Did I missed something?

    Here is the result of raspistill -v -o temp.jpg :

    raspistill Camera App v1.3.5

    Width 2592, Height 1944, quality 85, filename temp.jpg
    Time delay 5000, Raw no
    Thumbnail enabled Yes, width 64, height 48, quality 35
    Link to latest frame enabled no
    Full resolution preview No
    Capture method : Single capture

    Preview Yes, Full screen Yes
    Preview window 0,0,1024,768
    Opacity 255
    Sharpness 0, Contrast 0, Brightness 50
    Saturation 0, ISO 0, Video Stabilisation No, Exposure compensation 0
    Exposure Mode ‘auto’, AWB Mode ‘auto’, Image Effect ‘none’
    Metering Mode ‘average’, Colour Effect Enabled No with U = 128, V = 128
    Rotation 0, hflip No, vflip No
    ROI x 0.000000, y 0.000000, w 1.000000 h 1.000000

    Thank you for your help.

  11. This tutorial is the best so far, short and understandable. One question. If I error and send
    ./startmotion again after motion is already running, ./stopmotion will not work. I tried a several times to stop, but could not. Had to do shutdown.

  12. when I try to build motion with config file I get error
    ioctl (VIDEOCGCAP): Invalid argument in motion

    Can you please help me figure what am I doing wrong ?

    • Not sure you should be trying to build motion…This article is written around a cuastomised downloadable binary. However, you should follow the renaming I describe to make sure you are using the correct binary, as you also install the standard motion to get the supporting files. The config file will only work with the customised motion binary that you download as described.

  13. Installed, start and stop files work but I haven’t got in the stream on Google+ Safari, VNC viewer or RDP, After a few minutes of it running it knocked out my lan connection so I couldn’t connect even with Putty which I had running at the time, the camera light is still on and the single red power light on the board. I pulled out my cat5 and re–inserted but it didn’t re-connect so I pulled the power and rebooted, now going for a re-run, booted ok cd mmal ./startmotion and it’s running again. Rfb protocol error on VNC, i’m now trying VLC via loads of reading on Google.

  14. Hello, thanks for the tutorial it worked first time :-)

    Is there any chance you would know how to get the PI to automatically start motion on boot. This would help for power outages and reboots etc

      • Hello RBN2013,
        Unfortunately it doesn’t appear to be so easy. I have added
        (sleep10;su – pi -c”/home/pi/mmal/startmotion”)&
        to my rc.local and when I log in and “top” motion-mmal is running however there is no light on the camera board and no webpage displaying the output. If I then ./stopmotion and ./startmotion everything works.

        I note that the startup text says
        [warn] Not starting motion daemon, disabled via /etc/default/motion /// (warning)
        However I believe this is generated by the motion package and not MMAL motion package. If I edit this file to say yes plain old motion starts.

        Do I need to add the user system to any groups or anything like that and if so how?


      • It looks as if the mmal package is not starting. You shouldn’t have to change any groups. I changed the name of the modified package to motion-mmal and the conf file specifically to avoid confusion between the installed motion package and the modified version and used ./motion-mmal -n -c motion-mmalcam.conf to start things. It looks as if the correct one is not starting.

      • Thanks rbn2013,
        I did as you suggested and used ./motion-mmal -n -c motion-mmalcam.conf in my rc.local This piped the error messages to the command prompt (making it unusable so I had to SSH in, but it was very good feedback for debugging).

        I am not sure if it was a problem but I discovered that I had “Start in Deamon” as “on” in both the mmal conf file and the standard one (I turned them back off).

        Furthermore I discovered that I had to give the explicit location of the conf file in the RC.Local and not just the file name. If motion can not oped the specified conf file it will use the default one.

        So if you don’t tinker with anything you should just be able to add the following line to the RC.local for it to start automatically.
        (sleep 4;su – pi -c “/home/pi/mmal/motion-mmal -n -c /home/pi/mmal/motion-mmalcam.conf”)&

        Thanks again

  15. Thanks for the clear explanation of how to set this up. I have it all working however it is very slow with about a ten second delay between the actual motion and that being streamed. Any idea how I could fix this?

  16. Hi,
    I tried this and was able to finally get motion up and working and that was great. But after a few image captures by motion detection the images cease to be captured. . maybe a few minutes and no more images or videos are saved. There is plenty of disk space, the motion process is still running and the people pi noir cam LED continues to remain lit. I don’t want to introduce another process to restart motion. Can you offer any advice?

    • Hi there
      I wrote this project last July so I’m afraid I am a bit rusty about it now. I suggest you ask the question on the original Raspberry Pi forum article linked at the beginning of the article.

  17. Hi, this app detects motion as a picture change. So if it is dark and someone enters the camera field he/she won’t be detected. There is no real intruder detection. So it is completely useless as a security camera since burglars wouldn’t normally switch on the light when they break in. Would be nice to add the capability of using a GPIO input as another triggering method using a real infrared intruder detector.

  18. Hi,

    This worked really well. Thanks for the guidance.
    Everything I have read about motion suggests it has lots of capabilities even control of PTZ cameras. However, I’d like to connect the Pi into an alarm system so that when motion is detected one of the GPIO lines is toggled but I don’t know where to begin with this.


  19. I tried this on 20/1/14, but the .tar file is not available on Dropbox. Is there an alternative, or has it been moved please.

  20. Back again – I installed the software and modified the .conf file as specified. The camera red light was already on, having tried to check the view previously from Firefox, so I re-booted it – still on. When I try the program, I get an error message:
    mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
    Segmentation fault
    If you can give me any idea what I have done wrong, I would appreciate it.

    • I’m a bit rusty on this Nigel. Haven’t looked at it for months. I’ll see if I have time to check it out again. BTW there have just been advances in the camera software. Have a look at this. May be of interest. I did some slow mo movies of water drops falling yesterday. Very nice.

  21. Hey,

    I’m having problems with:

    tar -zxvf motion-mmal.tar.gz

    I get the error:

    gzip: stdin: not in gzip format
    tar: child returned status 1
    tar: Error is not recoverable: exiting now

    • Hmm the file appears to be downloading in a not gzip format. I got the same error. It looks like it is still in a html format. What you can do is to download it on a pc or Mac. Just go to the url there. On a mac you can double click the downloaded gz file and it extracts for you. You could copy it across to the Rpi using scp. Let me know if you want more help on this.

  22. First, thank you very much for this clear tutorial. I got everything working but im having another kind of question. I’m kind of new with camera’s and the settings of an camera. Are these settings overruling the default pi camera settings or can i still change the camera settings and these will have influence on this app? I’m having trouble with the images at nighttime, is there an special mode for night?

  23. Hi, great tutorial. Motion works ok – one thing is missing for me how to make it send email wohen motion is detected. I did setup postfix for that and it’s 100% working but when I edit on_picture_save (or something like that) line and add %f to attache last taken picture nothing happens – any idea for that? It’s raspberry model B and the camera module.


  24. To monitor each and every motion outside the home using the wireless camera and making he necessary coding for the motion in it for the automatic motion detection.

  25. Works well. I added a script to the on_picture_save and on_movie_end events that copies the files to Amazon S3 and deletes them from the PI once they’ve been uploaded. It uses s3cmd which is the easily available and well documented s3 command line script.

    In the .conf file:

    on_picture_save /home/pi/mmal/
    on_movie_end /home/pi/mmal/



    cd /home/pi/motion-capture

    var=( $(ls -t) )

    dir=`date +”%y/%m/%d/%H”`

    /usr/bin/s3cmd put $name s3://piBucket/$dir/$name

    rm $name

  26. This page was a great help for me. Thank you all for this.

    A efficient {start|stop} script to lauch the motion-mmal software automatically when you pi boots is given below. It needs to be placed into /etc/init.d folder.
    It assumes that the motion-mmal software and config file are placed in /home/pi/mmal
    and that you followed the excellent guidelines at the top of this page:

    sudo pico /etc/init.d/motioncontrol

    and enter the following code: (starting with #! and ending with the line after exit 0 )

    #! /bin/sh
    # /etc/init.d/motioncontrol

    # Provides: motioncontrol
    # Required-Start: $remote_fs $syslog
    # Required-Stop: $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Simple script to start motion-mmal program at boot
    # Description: A simple script to start the motion-mmal at boot.

    # If you want a command to always run, put it here

    # Carry out specific functions when asked to by the system
    case “$1″ in
    echo “Starting motion-mmal”
    # run application you want to start
    nohup /home/pi/mmal/motion-mmal -n -c /home/pi/mmal/motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &
    echo "Stopping motion-mmal"
    # kill application you want to stop
    killall motion-mmal
    echo "Usage: /etc/init.d/motioncontrol {start|stop}"
    exit 1

    exit 0

    Save this file, make it executable (chmod u+x /etc/init.d/motioncontrol )
    Make it part of the normal startup process:

    sudo update-rc.d motioncontrol defaults

    Like this the motion-mmal will launch when your pi starts…
    Starting and stopping can be done using
    sudo /etc/init.d/motioncontrol start
    sudo /etc/init.d/motioncontrol stop

    If you don't want the red LED to light up when motion-mmal is working, add the following line to

    sudo pico /boot/config.txt

    and add

    at the end of the file. Save and exit. Reboot you pi.

    Have fun!

    • In case anyone is having trouble using TheLuckyOne’s script, check your quotes. The website substitutes different quote characters sometimes which messes you up when you copy/paste. Script works great by the way. Thanks!

    • The script is working fine for me as well. Thanks ! Great stuff. Just pay attention when copying the quotes indeed into pico editor. I also struggled with the (chmod u+x /etc/init.d/motioncontrol). It should be sudo chmod u+x /etc/init.d/motioncontrol. I know I’m a rookie. But I learned from it :)

  27. Hi,
    I have problem, camera make bad movies. Look log:

    [-1242782048] [NTC] [ALL] [Aug 11 17:52:26] motion_startup: Using log type (ALL) log level (WRN)
    [1] [ALR] [VID] [Aug 11 17:52:26] mmalcam_start: MMAL Camera thread starting… for camera ( of 640 x 480 at 2 fps
    [1] [ALR] [VID] [Aug 11 17:52:26] mmalcam_start: MMAL Camera using video capture
    [0] [ERR] [VID] [Aug 11 17:53:14] Received unexpected camera control callback event, 0x0003e220
    [0] [ERR] [ALL] [Aug 11 17:53:34] main: Thread 1 – Watchdog timeout, trying to do a graceful restart
    [0] [ERR] [ALL] [Aug 11 17:54:34] main: Thread 1 – Watchdog timeout, did NOT restart graceful,killing it!
    [0] [ALR] [VID] [Aug 11 17:54:34] MMAL Camera cleanup

    Example movie here:

    Error 0x0003e220? I dont know, google dont know …
    I have not any idea for solution this problem :-(
    Thank You for feedback.

  28. Hi rbn2013, Great tutorial on mmal usage and configuration. It really got me started using the RBPI camera module and motion detection. I also have a synology which I configured according your guidelines. It took me a while to understand connecting the RBPI on the SN there’s 2 kind of motion detection. One done by the motion script on the RBPI and storing AVI files in m-video. The other motion detection is done by the SN surveillance station which has noting todo with the RBPI motion script. These are 2 different things so it seems.

    All is working fine except the timeline recordings. My SN seems to be recording avi files but I can’t play them in the SN surveillance station. I get like a little icon like the file is damaged. When I go to the actual directory on my SN and I download the avi file I have like a 1 frame still video of several seconds. I’m trying to figure out why.

    Could you explain in the SN camera config where the /stream.mjpeg is coming from and what it’s referring to ?

    Thanks Peter

    • Hi Peter
      Glad you enjoyed the article. To be honest it is over a year since I looked at this. I think I got the /stream.mjpeg by experiment, and by looking at the list of user reported image source path settings in the synology surveillance camera help file, and trying out various combinations. If I have time I will try and fire things up again, but these days I am heavily into using Sonic-Pi on the RPi (version 2 released yesterday!) as you will see from other entries in my blog.
      I wouldn’t be surprised if things have changed a bit since I set up the code. New DSM for Synology for a start, and new models for Pi and later OS, so there may well be bits that need to be tweaked again.

  29. Hi rbn2013, nice howto, but how to start it automaticly? I want to start it every time my raspberry starts, im not sure for the right way.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s