So this is a project I’ve been picking away at for some time now, adding new bits along the way. I was looking for a way to keep an eye on my flat while spending a lot of time on the road with work.
While hunting around online I found a reference to a small application called Motion which will monitor a web cam and can trigger alerts when the image changes. Along with the alerts it can capture the images or video of what has changed and has filters to detect the gradual changes as it gets light or dark during the day.
My motion config file is in the Resources section at the end.
Originally I had to build Motion from src, but it was recently added to the extras rpm repository I use with Fedora 11. Once installed it’s relatively easy to configure, initially I just had to update the config file (/etc/motion/motion.conf) point it at my web cam (/dev/video0) and a directory to save the images in.
Motion has a number of useful features, once running you can update all of the settings via web interface and it will also stream the video from the camera on a different port which is good for testing. There is also a REST style interface which can be used to turn the motion detection on and off, more on that later.
After that I set up Motion to email the images to a spare gmail account to ensure there was a off site copy of the images just in case the computer with the camera was stolen. The following script was attached to the “on_picture_save” event as follows:
... on_picture_save /home/user/bin/intruder.sh %f %H:%M:%S %d-%m-%Y ...
Where %f is the path to the image and %H:%M%S %d-%m-%Y is a time and date stamp.
#!/bin/sh uuenview -a -m firstname.lastname@example.org $1 << EOF Subject: Possible Intruder $2 $3 EOF
This sends a mail with the time and date in the subject and the image in the body.
So that was not a bad solution, I normally run Thunderbird or have the gmail manager plugin for Firefox running so I get updates about new emails relatively quickly. But then I ended up at a customer who would not let me hook my laptop up to internet while in the office so I started to look for a different notification mechanism to add on as well.
The next perfect solution seamed to be SMS messages. I had a spare mobile phone and pay-as-you-go sim cards are free. So back to searching the net for some software to send SMS messages. I found 2 different Linux packages that would do this. The first is called Gnokii which is part of a package that started out as a way to back up and interact with Nokia phones. Gnokki’s smsd works from two simple mysql tables, one for inbound and one for outbound messages. It polls the outbox table at a configuable interval (I’ve set mine to 5 seconds) and sends any unprocessed messages.
The second SMS package for Linux is called Kannel. Kannel is a lot more than just a program to send SMS messages, it is also a full WAP stack. It is very powerful but requires 2 processes and webserver to be able to send and receive messages.
So to start with I chose Gnokii as the set up was a lot simpler. You basically just need to get a serial connection to your phone, this can be either via a USB cable or you can set up a connection via Bluetooth using the rfcomm command.
I am using a Sony w880i as my phone and it comes with a USB cable which doubles as a charger which makes things easier. When I plugged it into my box it showed up as 2 Serial ports (/dev/ttyACM0 and /dev/ttyACM1) they both seam to behave the same.
I added a new script to the “on_movie_end” Motion trigger
... on_movie_end /home/user/bin/sendSMS.sh %H:%M:%S %d-%m-%Y ...
#!/bin/sh /usr/bin/mysql -u smsgw -p xxxxxx smsgw << SQL insert into outbox (number,text)values ('+4477xxxxxxxx','Possible Intruder - $1 $2'); quit SQL
There is a sample Gnokii config file in the Resources section
So that is a good start, we’ve got a system that will detect motion and raise the alarm. But at the moment it’s turned on all the time, which is no good. This is where Motion REST style control API comes in. If I had a way to automatically tell when I’m at home I can turn off the motion detection. I discovered a blog post by another guy at Hursley called Gareth Jones
Who seamed to have already solved this problem. Gareth had put together some Python that scans for Bluetooth devices with in range of the computer and sends alerts as devices arrive or leave. Since most people carry a Bluetooth device arround with them all the time in the shape of a mobile phone this seamed like a good idea.
At the time I was looking at this bit I didn’t have time to go digging in Python, so I hacked up a quick shell script verion that would just track one phone and then use the Motion REST API to pause the detection when ever I’m at home and turn it on again when I leave.
The detection is done using the l2ping command from the Bluez package and giving it the Bluetooth mac address of the phone I want to track.
#!/bin/sh MAC=00:AA:BB:CC:DD:EE while true; do if [[ `sudo l2ping -c 1 $MAC 2> /dev/null | grep -c "44 bytes from"` == "1" ]] then echo "home" if [[ `cat /home/user/home` != "YES" ]] then echo YES > /home/user/home fi lwp-request -C user:password http://127.0.0.1:8080/0/detection/pause > /dev/null sleep 90 else echo "away" if [[ `cat /home/user/home` != "NO" ]] then echo NO > /home/user/home fi lwp-request -C user:password http://127.0.0.1:8080/0/detection/start > /dev/null sleep 15 fi done
MAC is the bluetooth address of my phone, you can discover this by running the following command “hcitool scan” which will produce a list of all the bluetooth devices your machine can currently see and their names.
So this is where I left the solution for quite a while because I couldn’t find a project that would allow me to send MMS messages from Linux without having an arrangement with a cell phone
- Motion – http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome
- Motion – conf file
- Gnokii – http://www.gnokii.org/
- Gnokii – conf file
- Kannel – http://kannel.org/