Tag: raspberry pi

Capturing SAT24 satellite images to create an animation

During the solar eclipse on March 20th 2015 it was nothing but clouds over here in The Netherlands.
So besides following the eclipse on TV I also watched the satellite images on the SAT24 site to track the Moon’s shadow over the continent.
I got the idea to create a small bash script that downloads the satellite images every X minutes in a separate directory.
Those images can be stitched together to create a nice movie.

So, here’s the code:

INFRARED=false
TMPDIR=/home/pi/sat24/tmp
IMAGEDIR=/home/pi/sat24/images
CURRENTIMAGE=$(date +"%Y%m%d%H%M")

wget -O $TMPDIR/temp.jpg "http://www.sat24.com/image2.ashx?region=eu&ir=$INFRARED"

MD5NEW=`/usr/bin/md5sum $TMPDIR/temp.jpg | cut -d ' ' -f 1`
MD5OLD=`/usr/bin/md5sum $TMPDIR/prev.jpg | cut -d ' ' -f 1`

if [ "$MD5NEW" != "$MD5OLD" ]; then
{
cp $TMPDIR/temp.jpg $TMPDIR/prev.jpg
cp $TMPDIR/temp.jpg $IMAGEDIR/$CURRENTIMAGE.jpg
}
fi

It’s pretty straight forward.
The image file name is the current date and time and a new image is downloaded using wget.

The only thing that needs an explanation is the md5sum stuff.
Sometimes it happens that the script downloads a image from a few minutes ago and not the current one.
That means that most of the time it’s an image you already have.
I guess this has something to do on the Sat24 side: web servers not in sync or load balancers that don’t balance load.
With md5sum you create a MD5 hash which is ‘unique’.
If the new image has the same MD5 has as the previous image, this new image can be discarded because you’ve already downloaded it.

To make this script run automatically, simply add it to the crontab (I’m using the cron of user pi on my Raspberry Pi here):

<code>*/4 * * * * /home/pi/sat24/sat24.sh</code>

I use intervals of 4 minutes but you can tune that if you like.
Oh, and do not for get to turn on the execute bit of your script.

Okay, you’ve been running this script for a couple of days and you’ve collected hundreds of images.
Now it’s time to stitch them together and make an animation!

Thanks to a great command line tool called avconv (previously known as ffmpeg), you can create a movie file using separate images:

cat images/*.jpg | avconv -f image2pipe -c:v mjpeg -i - -r 25 -map 0 -s 1280x780 -filter:v "setpts=4.0*PTS" out.mp4

Err…yes! It’s that simple!
Just cat the files and pipe them into avconv.
The -s 1280×780 parameter creates a 720p HD video file.
The -filter:v “setpts=4.0*PTS” parameter slows the playback rate down because 0therwise the animation goes a bit too fast. You can fiddle with the number to your taste.
The video created is called out.mp4 which you can rename if you want, of course.

And this is the result:

Some double images might be downloaded once in a while, which you can delete by yourself by wading through the images directory.
Or you could write a script that checks and deletes duplicates for you.

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.