Code to Interface Your Home to Twitter

Home Connected to Twitter

Last week we interviewed Andy Stanford-Clark on his smart home integration with Twitter and now we have a document published by computer science student Justin Wickett giving his open source script to do the same.  Read on for the code that gets your home communicating with you via SMS text message to your mobile plus all the other advantages Twitter offers.

“I only had to write a few lines of code to pull all of these technologies together so that they would work with each other. The following code is a very rough implementation that I originally used to test the feasibility of this idea. I plan on incorporating Bluetooth support as well as confirmation notifications once I get back to Duke University. Right now, I am using my cell phone to send Twitter public updates that are broadcasted out to all of my friends. This method is not secure, and spams your followers with updates about your electrical network’s condition. I recommend creating a private account for testing purposes, or better yet using Twitter’s direct messaging functionality.

Finally, this code relies on the Summize.com search engine, which parses and indexes every public message sent to Twitter. I could not poll Twitter.com because of rate limiting issues. My code polls Summize’s REST API (which is simply a Web URL) every second checking to see if there has been an update. This polling method is not efficient and taxes Summize’s servers. I recommend subscribing to and parsing Twitter’s Pub Sub Jabber feed (see http://groups.google.com/group/twitter-development-talk/web/jabber-pubsub).  Below is my quick and dirty Python code that can be easily ported over to other languages:

#/usr/bin/python
#Copyright 2008 Justin Wickett
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import feedparser, os, time #The ‘feedparser’ library can be installed from http://www.feedparser.org/
#TODO: Replace the username as well as the INSTEON address with the appropriate values
#TODO: Also make sure that the path is correctly set in the system command to icmd’s path on your local machine
lastTweet = 0 #Used to keep track of the last Tweet received to make Summize queries less expensive
username = “xxxxxxxx” #Twitter username who is sending the commands to control the electrical network
insteonAddress = “xx.xx.xx” #INSTEON address of the device you want to turn on and off

while(1):
feedUrl = “http://summize.com/search.atom?q=from%3A”+username+”&since_id=”+str(lastTweet) #Polling Summize
feed = feedparser.parse(feedUrl)
if len(feed[‘entries’]) > 0 and feed[‘entries’][0].link.split(‘/’)[-1] > lastTweet:
if cmp(feed[‘entries’][0][‘title’], “Bedroom lights on”) == 0: #Check for the “ON” command
os.system(“icmd “+insteonAddress+” ON 255”) #Turn the lights controlled by my switch on
if cmp(feed[‘entries’][0][‘title’], “Bedroom lights off”) == 0: #Check for the “OFF” command
os.system(“icmd “+insteonAddress+” OFF 255”) #Turn the lights controlled by my switch off
lastTweet = feed[‘entries’][0].link.split(‘/’)[-1] #Save the last Tweet so we aren’t stepping over ourselves
time.sleep(1) #Sleep one second, and execute code again

Justin Wickett’s Site http://people.duke.edu/~jyw2/  : Follow Automated Home on Twitter

1 Comment on "Code to Interface Your Home to Twitter"

  1. amazing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.