Eric Blue’s Home Automation Hacks with Belkin, Twilio and Siri

March 24, 2014
Written by

belkin_twilio_siri

eric_biz_bw_close
Eric Blue started experimenting with home automation almost on accident. He just wanted to automate inconvenient aspects of apartment life.

When Eric’s friends came to visit him and pressed his apartment buzzer, he would get a call. But, if when he was watching a movie or playing a video game he’d often miss the call. So, Eric used Twilio to forward calls from his apartment buzzer to display on his TV, so he never missed a call, even if he was in the middle of a heated battle on Xbox.

Once he finished the TV hack, Eric wanted to do more. Using a Belkin Wemo, Twilio SMS and Siri, Eric developed a way to turn the lights in his apartment on and off via voice command and text. Take a look at Eric’s tutorial below originally published on his blog here

Learn more about Eric and his independent software development projects by visiting his blog here

Last year around this time on an ambitious evening I had an idea to quickly mashup some APIs, and make a cool and useful home automation hack.  This example uses the Belkin Wemo power switches, my Perl API (WebService::Belkin::WeMo) for controlling them,  Twilio, and the built in features of voice control with Siri on the iPhone.  It sounds complicated, but with relatively little code you can automatically turn your lights on and off in your home by either sending a text message, or even using voice commands (ex: “Computer turn all lights on!”)

Overview Diagram

wemo_twilio_siri_hack

Belkin WeMo Perl API

Last year when I first got my Belkin WeMo switches I was disappointed to see there was no API out there yet.  And, given my go to language for quick ‘hacks’ is Perl I decided to reverse engineer how it works.  Luckily it’s based on UPNP (Universal Plug n’ Play) and didn’t take too much work to figure out how to search for switches and sensors, and send commands to toggle electrical outlets on and off.  The code has been available on GitHub for some time, and I just recently uploaded to CPAN to make distribution and inclusion easier.

Step by Step Instructions

[API and SMS server]

  1. Find a computer located on the same network as your Belkin Devices.  Ensure your firewall/router can open up port 9000 (default) and map to your computer.
  2. Check out the Perl Belkin Wemo API from CPAN (v1.0 as of this writing) for the latest stable version, or Github for the latest developments.
  3. Scan your local network to find all Belkin Wemo Switches and save into your /etc/belkin.db file (see examples/scan.pl under the Perl Wemo API dist)
  4. Create a simple test script to turn a switch on and off to make sure things are working OK
  5. Download the SMS server example at https://github.com/ericblue/Belkin-WeMo-Twilio-Server.  The commands, your cell number, and the names of your switches should be changed.
  6. You can launch the server (launch.sh) or in debug mode.  Going to http://your_internal_ip:9000/ should show a simple text welcome message.

[Twilio Setup]

  1. Go to Twilio.com and setup an account
  2. Register a new number and define the messaging request url as ‘http://your_public_ip:9000/sms/ and POST.  Port 9000 is the default port, and should be mapped in your firewall from your public IP to internal IP of your SMS server.
  3. Send a text/SMS message to this computer ‘get devices’ and you should get a list of all Belkin Switches on your Network

[iPhone/Siri for Voice Control]

  1. See this article on setting up Siri for voice control – http://www.dummies.com/how-to/content/how-to-use-siri-for-texting.html
  2. Add your Twilio SMS server as a contact.  I named mine ‘Computer’ (think Star Trek) , but you can name it whatever you want
  3. You’ll need to add a nickname or relationship/label as ‘Computer’ so when you hold down the button for Siri you can say ‘text Computer ’

my $valid_commands = {’1′ => ‘test’,’2′ => ‘get devices’,’3′ => ‘get commands’,’4′ => ‘turn all lights on’,’5′ => ‘turn all lights off’,’6′ => ‘turn upstairs lights on’,’7′ => ‘turn upstairs lights off’,’8′ => ‘turn downstairs lights on’,’9′ => ‘turn downstairs lights off’};

If you look at the sms_server.pl file you can see it’s a VERY simple and crude processor for some basic commands and translates words such as ‘upstairs’ and ‘downstairs’ to specific devices. This was created very quickly, so to customize you could modify to suit your needs or add something more sophisticated like a rule processor or XML-based config for mapping commands and devices.

That’s it! I hope you enjoyed this relatively simple hack and have fun!