Using the Raspberry Pi for camera surveillance using Motion and cloud storage

February, 16th 2014
A fully updated post using BoxFS2 with oauth2 authentication.
The previous version of this post used an old version of BoxFS which is now obsolete because Box.com changed their API.

If you are looking for a cheap camera surveillance setup, the Raspberry Pi is a great solution.
It is small, easy to install and, most important, has low energy usage.
I own Model B (see the Raspberry Pi Wikipedia page for detail about the models) which uses 3.5 Watts.

There are several open source motion detection applications for Linux such as ZoneMinder and Motion.
Both programs are available through the Rasbian repository.
ZoneMinder looks like a real security control center with all those nice cam screens, but it’s too bloated and too CPU heavy for the Raspberry Pi.
Motion is more lightweight and doesn’t stress the CPU too much when processing 320×200 camera data.
With 2 IP camera’s my average load is less than 0.50 on my RasPi, which also serves as an caching DNS server.

For storage of the AVI video files which contain the captured motion frames, I use a free Box.com account.
Box.com provides the same service as the popular Dropbox.
The reason for choosing Box.com is because Dropbox has no open source client. The Dropbox Linux client is closed source and has, at the moment, no support for ARM devices like the Raspberry Pi.
Furthermore the free Box.com account gives you 10 GB of free storage. Way enough to store home camera data for a couple of months.

BoxFS2
is the weapon of choice to mount your Box.com account as a drive partition on your Raspberry Pi.
Some basic Linux knowledge is preferred.

Setting up BoxFS2
First, read the README file on the BoxFS2 site carefully for the needed dependencies 🙂

Install the needed libraries from the command prompt:

sudo apt-get install libxml2-dev libfuse-dev libcurl4-gnutls-dev

Besides some libraries from the Raspbian repository, you’ll also need libapp and libjson.
Get the latest commits from boxfs2, libapp and libjson:

git clone https://github.com/drotiro/boxfs2.git
git clone https://github.com/drotiro/libapp.git
git clone https://github.com/vincenthz/libjson.git

Compile libapp first, then libjson and boxfs2 last using make and sudo make install.
After installing make sure you run the command ldconfig to update links to the libraries for the operating system.
Forgetting this might give you some error messages about not found libraries.

The BoxFS2 binary will be installed in /usr/local/bin.

Creating a BoxFS2 config file
With the command boxfs-init you can create a config file for BoxFS2.
The configuration file will be placed in the directory .boxfs2.

Open an editor and the configuration file would look something like this:

# Conf file for boxfs
# edit to fit your needs.

# Put oauth2 tokens here:
token_file = /home/pi/.boxfs/token

# Set a valid mount point
mountpoint = /home/pi/box.com
verbose = no
largefiles = no

# Configure the cache
cache_dir = /home/pi/.boxfs/cache
expire_time = 1440

# Configure your uid and gid below:
uid = 1000
gid = 1000
fperm = 644
dperm = 755

The mountpoint must be provided in either way. This can be an empty directory called /motion for example.

Also check the user id (uid) and the group id (gid) of the user you are using.
You can check this in the passwd file which is located in /etc.
In my case I’m using user pi which has user id 1000 and group id 1000.

To let Boxfs2 run as a non-root user you have to add your user to the fuse group:

sudo gpasswd -a fuse

If you won’t do this, the following error message will be shown:
fuse: failed to open /dev/fuse: Permission denied

Also the execute bit of the fusermount binary should be set:

sudo chmod +x /bin/fusermount

Forgetting this will cause mounting to fail with this error message:
fuse: failed to exec fusermount: Permission denied

Mounting your Box.com account using BoxFS
To start the BoxFS2 client, simply run this command from the home directory of your Box.com user:

boxfs -f .boxfs/boxfs.cfg

The first time you run boxfs, you will need to complete the authentication (oauth2) process and grant access to your box.com account. It’s easy, just follow the instructions on the terminal and on your browser.

When the mount is successful, set the right permissions with the command chmod -r 755 <mountpoint>.

To check if everything is doing what it should be doing, go to your BoxFS mount point and try to create or copy some files.
For debugging I recommend to be logged in to your Box.com account with your browser to see if things are actually written in to the cloud storage.

Installing and configuring Motion

To install Motion just run this command:

sudo apt-get install motion

Be sure that your camera’s and/or webcams output a 320×200 (or 240) image and Motion is configured with the same resolution.
I’ve tried a resolution like 640×400 pixels, but the CPU load went sky high on the RasPi, so I’ve settled for a lower resolution.
The images generated by my camera’s are quite clear and useful, so I’m happy.
I have no experience over clocking the Pi in combination with Motion, but if you do, please share your findings in the comments.
Same goes for solving the above resolution problem 🙂

Further configuring Motion is something you have to do on your own.
The on line manual is quite clear about the numerous settings which can be done.
Mailing an image snapshot or a simple message when motion is detected is just one of the many possibilities.
Oh, and don’t forget to set the path to your BoxFS2 mount point to store your videos 😉

When you’ve got an mobile device, you can use the Box.com app to download an AVI file and watch it using a movie player which supports the file format.
Great when you’re on holiday for example, and want to know what’s going on at home.
And all of this without tampering  your firewall, keeping your home network closed and secure.

I hope this post helps you to set up your own budget camera surveillance system.
For less than 180 euro (that’s 2 decent IP camera’s and a RasPi) you’re all set to go.

30 reacties

  1. Hi Astroland! Thank’s very much….it’s all i need!
    it’s only a few months that I use linux and raspberry. But already fascinated …
    I mounted the webcam on debiam wheezy and all is ok ….on the local memory.
    Please, ….could you make a step by step tutorial for setup boxfs. I do not know where to begin: i don’t know how compile libeapp…and create config file.
    Thanks

  2. Hi!

    Thank you for this tutorial, it was very helpful to me, but the my main project is not exactly this. And this is why I’m asking you:

    How can you access the mounted boxfs directory on vnc without root permissions on GUI?

    Thanks: Robert

    1. Hello Robert

      Why don’t you connect using ssh?
      I don’t understand why you should connect a directory using a remote desktop utility.

  3. Thanks, this looks like what I need. I’m planning on using an older android phone (wifi only) as a IP camera. I tried this setup with zoneminder but performance on the pi was very poor. I’m goning to try a few more tweaks in before I give up and try motion instead. But this is good info and I didn’t think of using boxFS.

  4. Hi astroland, thanks for the tutorial. A question: why if I contact the webcam at http://ipwebcam:8081 I don’t see the but receive a poup with “do you want to save this file, or find a program online to open it?
    Thanks in advance

    1. Hi Antonio,
      The Motion webserver outputs an MJPEG (Motion JPEG) video stream.
      Looking at the message you get, you are using Internet Explorer which does not support MJPEG. Therefor it asks to use a program which can display a MJPEG stream.
      Firefox, Chrome, Opera and Safari support MJPEG without installing any extra programs.
      On the Motion website you can find some other solutions to display a MJPEG stream if you want to stick with Internet Explorer: http://www.lavrsen.dk/foswiki/bin/view/Motion/WebcamServer

  5. Thanks for the article, Astroland. I’ve been trying to set Motion up on my Pi for a few weeks now, and have got it to work initially but it invariably terminates after about 5-10 minutes. I’ve tried low resolutions, have moved the camera (Logitech C270 – bought especially because it said supported on the R-Pi compatibility list) to a powered USB hub.

    The best I’ve managed so far is a tiny 176×144 image size (I was hoping to use at least 640×480, and the camera says it supports 1280×960 at 30fps with MJPEG/MJPG ‘pixel format’) and it stayed up for 30 minutes before crashing out with some watchdog timeout error and a non-graceful restart:


    Apr 1 21:51:28 raspberrypi motion: [1] Started stream webcam server in port 8081
    Apr 1 22:20:17 raspberrypi motion: [0] Thread 1 - Watchdog timeout, trying to do a graceful restart
    Apr 1 22:20:17 raspberrypi motion: [0] httpd Closing
    Apr 1 22:20:17 raspberrypi motion: [0] httpd thread exit
    Apr 1 22:21:17 raspberrypi motion: [0] Thread 1 - Watchdog timeout, did NOT restart graceful,killing it!
    Apr 1 22:21:17 raspberrypi motion: [0] Calling vid_close() from motion_cleanup
    Apr 1 22:21:17 raspberrypi motion: [0] Closing video device /dev/video0

    Any ideas how I can get this working continuously? I’m using ‘Linux raspberrypi 3.6.11+ #397 PREEMPT’ if that’s any use, and I’ve done update/upgrade?

    1. This seems to be a specific Logitech problem in combination with the Pi running Motion.
      I did some googling and found on the Raspberry Pi forum users with a similar problem you’re having.
      If you scroll all the way down in that thread, you can read some solutions/workarounds given by forum users ahynes1 and johnsonfamily38.
      You have to fiddle around with the palette MJPEG is using.
      Don’t know if it works but it is worth a try.

      1. Hi Astroland – thanks for your reply.

        My camera won’t work with motion if I pick MJPEG as the palette so I’m using YUYV at the moment. To hack it to work at the moment I’m using 320×240 resolution with about 2 frames a second if I’m lucky.

        I’ve written a script (based on this: http://unix.stackexchange.com/questions/12075/best-way-to-follow-a-log-and-execute-a-command-when-some-text-appears-in-the-log) that monitors /var/log/user.log and when it sees the watchdog timer giving up it restarts the motion service. This seems to keep things working (although I’ll have blind spots during restarts, of course), and it seems to be restarting between every 10–20 minutes on my Pi. Not ideal but at least it sort of works. Apparently the USB people at Raspberry Pi are working on the USB drivers so maybe some improvements will come along (although at the moment, rpi-update bricks my SD cards each time and the file rename trick doesn’t seem to fix it so I end up having to re-image the SD card each time I try it, so I can’t try any of the latest updates.)

        I was assuming/hoping all this would basically just work by now on Linux so it’s all rather disappointing when these cameras work flawlessly at 30fps without crashes on Windows… 8-(

  6. Hi Astroland, Thanks for the article. I have compiled and installed boxfs on my pi. And I am able to mount my directory to box using
    boxfs -u name@mail.com -U 1000 -G 1000 -F 644 -D 755 -v /home/motion.

    The problem is that I am not able to write to /home/motion using my “pi” user after it is mounted. And I can only create a new file in /home/motion as root.
    drwxr-xr-x 2 motion motion 0 Jan 1 1970 motion
    Any ideas? Many thanks.

    1. Hi Ben

      I see your /home/motion directory has the owner motion and the group motion.
      Your boxfs command line uses user id 1000 and group id 1000. These belong to the user pi.
      Try to find the correct user id and group id for the user motion in /etc/passwd and use these in the boxfs commandline.
      Hope this helps.

      1. Hello Astroland,
        Thanks for the reply. The problem is that only the root user is only to write to the directory after boxfs mounted. Thanks.

        1. Hi Ben

          Yes, I understand your problem.
          Have you tried the possible solution in my previous reply?
          In your boxfs commandline you use the uid and gid of user pi while you want to write as user motion.
          That’s giving you the problem.
          User root can read/write (almost) everywhere on a filesystem, so that’s understandable in your situation.

          1. Hi Astroland,
            Thanks and I have tried that and it is writing now. But unfortunately, I have a new issue – SD card I/o error. Cheers.

  7. Applying UID and GID of user motion to boxfs mounting options not helping.
    User motion is not even able to browse mount point.
    Compiled rev 95 from SVN.

  8. Yes, I did that. Notice time stamp on directory is wrong:

    as root:
    drwxr-xr-x 2 root root 4096 Aug 19 14:38 mnt
    drwxr-xr-x 2 motion motion 8 Dec 31 1969 motion
    drwxr-xr-x 5 root root 4096 Oct 4 22:32 opt

    as user motion ( had to give him /bin/sh instead of /bin/false in order to test ):
    drwxr-xr-x 2 root root 4096 Aug 19 14:38 mnt
    d????????? ? ? ? ? ? motion
    drwxr-xr-x 5 root root 4096 Oct 4 22:32 opt

  9. FWIW, I’m having the Exact same issues as Matolok. I’ve also tried (and failed) all solutions offered here. The only thing I notice is that when BoxFS is loaded, it’s root-only access.

    I did a sloppy ‘fix’ of just running a cronjob to move files as they appear from where motion outputs to the boxfs directory which is working well enough. I’m a fan of K.I.S.S. though, so it’s slightly irritating.

    Thanks for the blog entry, it has helped me.

    1. I will do a revision of this blog entry soon(ish).
      In the mean time BoxFS has been updated to BoxFS2 which uses a new API and makes BoxFS (version 1) useless any day now.
      As an alternative to BoxFS, you can also try davfs2. With davfs2 you also mount to your box.com account using box.com’s DAV interface at https://dav.box.com/dav/.
      davfs2 is in the apt repository, so you can install it with apt-get install davfs2.
      A proper explaination will be in the mentioned revision but you can also do some research before I post that revised article 😉

  10. any luck with Pi Noir ? I would like to stack drift-scan images with this imager.
    Word has it one could use the raw format, and go beyond 10 seconds of imaging. Not sure if the over 10sec. treshold could be used but I do not plan high magnifications anyway.

  11. I am a noobie. In your tutorial you say that I should compile libapp then libjson and boxfs2 last So my question is do I run the command make immediately after I close each library? That is how do I make sure I compile the libraries in the order that you suggested. Second question – Will the software work with a raspberry pi camera board? Third question in which folder should they be compiled or is that automatically done by the make command? Thank you for your help.

  12. Hello,

    I know it’s been a good while since you wrote this blog but I have a problem which is simple one one hand, but, on the other, can’t find any step-by-step instructions to remedy it.

    I have successfully set up motion and made all the necessary tweaks to run it in daemon (edited etc/default/motion & etc/motion/motion.conf) but I still can’t get it to start in daemon mode. That said, with every retsart of the Pi. I have to manually start the script by “sudo service motion start”. I know it is an issue with the permissions that motion needs to start in daemon but as a complete newbie in the PiWorld, I would appreciate if you could guide me through to the 2-3 necessary commands to fix it.

    Many thanks

Sluit aan bij het gesprek

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *