Note: Since we published this post the Twilio Ruby Library has been updated. For the most up-to-date code for sending Twliio SMS with Ruby, visit here.
One of the striking things about the Twilio API when I first played around with it was that it made my phone ring. That might sound simple, but it was remarkable for me. I’d spent my whole life as a developer playing in a virtual world of sorts. You’d write code and it would display something on your screen, or store something, or even send data over the Internet onto someone else’s computer, but at the end of the day everything lived inside of a box. And then I made my phone ring.
Twilio developers make phones ring all day, so we ran a hardware contest in the hopes of recreating that feeling for our developers. I was a little hesitant about this contest because I know a lot of developers don’t have the hardware on hand, but the winner, Andrés, made something really cool with stuff he had lying around—an intrusion detection system.
Here’s what Andrés had to say about it:
I discovered Twilio through a CloudSpokes challenge. The description was “integrate Twilio with something physical.” Since I don’t own any unusual hardware interfaces, such as an Arduino, at first I dismissed the challenge. But it stayed in my head. After a few days, I had the idea of interacting with a webcam, and the idea of building an alarm system followed. It felt like a fun project, so I started figuring out the details. Besides, the prize was really tempting!
I had made a few time-lapse video projects, and used some of that knowledge to build a small prototype. I knew I could capture frames from my low-res webcam using VLC Media Player.
To perform the detection, I did some basic image processing, using ImageMagick. The detection procedure is as follows:
- Key frames are singled out from the webcam frames at a given interval.
- Key frames are compared for differences. This is done by subtracting one frame to the other. (Figure 1)
- A threshold is applied to the result of the subtraction, to filter noise and small changes. (Figure 2)
- If the resulting image is completely black, no changes are assumed.
- If the resulting image contains a white section (which indicates a considerable difference between the key frames), the alarm is triggered.
When the alarm is triggered, all frames between the key frames are saved. Following frames are also captured for a defined period of time (i.e. 3 seconds). With all the gathered frames, a video is encoded (using mencoder), and then uploaded to a server.
Only one thing is missing: letting the user know that something has been detected! Here’s where Twilio comes in. Once the video has been uploaded, an SMS with a link to the video is sent to the user.
Using a ruby wrapper of the Twilio REST API, this was the easy part:
ruby send.rb alert-1.mp4
The only downside was that Twilio doesn’t work for international numbers yet, and I’m based in Argentina. A friend living in the US helped me test the system.
Another feature that could be added to the project is to use SMS (Using Twilio API, of course :) ) to turn the alarm on/off. Twilio integration was really easy and straightforward. It was a fun project and I really enjoyed building it.