Last weekend I sat down and built a surveillance system, including an iPhone webapp, for my apartment using my old iBook, an external iSight, the Webcam Software EvoCam and a set of scripts in AppleScript, PHP and some shell scripts.
The whole thing is pretty custom made but I’ll document it here anyway and with a basic understanding of the technology it should be fairly easy to reproduce with whatever hardware you have available. At the end of the post you’ll find a ZIP file with all the scripts I used in the setup.
The set up
- The iBook has the iSight connected and pointed at the entrance door.
- iBook is using?MAMP to serve PHP scripts and produces some audible confirmation messages.
- The iPhone webapp shows if the system is armed or not, can start or stop EvoCam, lists all the available surveillance videos and offers a link to the live video stream.
Read on for the whole article with a detailed explanation of all components.
The course of action
In addition to the control center there are also two separate webapps for starting and stopping BSS. This allows me to have the three related icons positioned in the bottom left corner of my iPhone home screen. Now whenever I leave the apartment I just need to turn BSS on, with a touch of my finger.
The iBook then launches EvoCam and announces “BSS armed” through Apple’s built in text to speech engine, so I can hear if everything started up properly. Whenever EvoCam detects motion on the entrance door it will record a video (including a 2 second buffer before the motion event occured and 10 seconds afterwards) and save it to a webaccessible folder.
At this point I still haven’t decided on the best form of notification I will send out. Right now there’s a little AppleScript, triggered by EvoCam, that uses Mail.app to send me a predefined Email with the subject “Intruder Alert!”. But this method is a bit of a performance hog, so I’ve considered a number of alternatives. One easy possibility would be to send out an alert via Twitter, but there are also ideas like starting a FaceTime call, having a Push enabled iPhone app that receives the notification or hacking together some kind of way to alarm me via SMS (short message service).
When I get a notification I can choose to see the live stream or the archived video from my iPhone right where I am. The videos are compressed in a format that is playable on iOS and stream nicely over 3G.
When I come home I take out the iPhone again and push the stop button. The system will then announce “BSS disarmed” as soon as EvoCam has quit and I can open the door without triggering an alarm.
There are a number of improvements I can think of that will be incorporated once I have the time to do them.
- Using a cookie to save the list of videos, each time the control center is opened. That way I can append a little “new” badge on each video link that wasn’t there the last time I started the webapp.
- Re-programm the webapp to use only AJAX (probably using jQuery), so I don’t fall out of webapp context, whenever an outside link (video, live stream…) is opened.
- On the “Recording” tab set the Destination path to the folder you want your movies in (/Users/ibook/Sites/bss/ in my case). Then choose “Advanced” and set the filename to “YYYY-MM-DD-HH-MM-SS.mov”
- Select a suitable amount of frames to be prepended to the beginning of the video under “Use Standby Recording”
- Leave “Use Schedule for Recording” unchecked
- Go to the “Items” tab and add a sensor that covers the part of the image you want to monitor for motion.
- Under “Settings” for that sensor ensure that the sensor is always active and check “Perform actions selected in the “Refresh” tab”
- Configure how long the video should continue recording after the sensor triggered.
- Leave all other boxes unchecked.
- On the “Refresh” tab choose “When motion is detected” and select the actions you want to take place.
- If you want to run an AppleScript on an event, choose “Options” from the main menue and select “AppleScript Settings”.
- If you want live video, be sure to turn on EvoCam’s internal webserver on port 8080.
Of course there are a lot more settings and tweaks in EvoCam, so once the system is running, come back here and click through everything that’s available.
The main hub is the index.php file in the BSS root folder. It renders out the HTML for the webapp and ties together all the other components. Download the ZIP file at the end of the post and have a look through all the files, it should be pretty self-explanatory and I’ve added a few comments but you do have to adjust all the respective paths and your security token (more on that below) to fit your setup.
The start_evocam.sh and stop_evocam.sh scripts are called via their respective web frontend (PHP scripts in subfolders). They launch and quit evocam and announce the status via text-to-speech. status.sh uses an AppleScript oneliner to report 0 if EvoCam is stopped or 1 if EvoCam is running. It also has an accompanying PHP caller script in the status subdirectory. If you’re confused by the weird folder set up, let me explain. Every script lives in it’s own subdirectory, so via HTTP I can call all the functions just by appending the name to the base URL. If I need status information I call http://myhome.com/bss/status. The same applies to the start & stop commands.
The bss folder needs to be copied to a webaccessible directory – in my case that is my MAMP web root (in my case: “Sites” in my user folder).
Every PHP file’s first line consists of a check for a token, that is sent via the HTTP call. So to start the system you’d have the URL http://myhome.com/bss/start?t=MYSECURITYTOKEN or the server will give you HTTP Status code 403 (forbidden). Of course this is all sent in clear text and could be easily sniffed, so be sure to implement whatever better security system you need for you environment. But since the URLs only need to be entered once and are then concealed from the screen by iPhone’s webapp fullscreen capability, I thought that should be enough for the moment.
To have access to the security system it is necessary to map port 80 (the main webserver) and port 8080 (EvoCam’s internal webserver for the live stream) via NAT to the internal IP adress of the Mac running the security system. Otherwise you won’t be able to access the system from the outside.
Once all the scripts pathnames and tokens are edited accordingly, you just have to open http://myhome.com/bss?t=MYSECURITYTOKEN in Safari on your iPhone and add it to the home screen by tapping the little plus symbol on the bottom toolbar. Now the BSS control center lives as an icon on your iPhone and will start up in fullscreen with a pretty startup image. For the start and stop buttons you have to open http://myhome.com/bss/start?t=MYSECURITYTOKEN and http://myhome.com/bss/stop?t=MYSECURITYTOKEN and add them to the home screen as well. Each webapp comes with it’s own descriptive icon and title.
Now your surveillance system should be set up and ready to go.
I have a custom website.html file with a black background and no borders that is not included in the download. This file is used by EvoCam to display the live stream on my iPhone.
The AppleScript to send Email is also omitted from the download, as this is easy to figure out and I’m still not sure this will be the final way of delivering alert notifications.
If you have any ideas for additions or improvements I’d be happy to hear them!
Here is the first (and probably last) public release Version of BSS the Brainfusion Surveillance System: