Introduction: Twi_X10 - the Synergy of Twitter and X10

The goal of this Instructable is to show you how to automate your home lighting (or appliances) with X10, setup your computer to control the devices, and then get Twitter to do the control. Experience with Python, the Linux command line, and Twitter's dev api will help make this Instructable easier to understand.

This Instructable is part of the "Adafruit Make It Tweet Challenge", so if it's before June 30, 2011. please vote!

Yes, it's that X-10. The company with the super annoying, flashy banner ads that used to be everywhere selling creepy, stalker-worthy, video cameras also sells home automation devices, and they're cheap!

Note: X-10 is the company, and X10 is the protocol. I'm going to try to keep it straight.

Step 1: Required Hardware

This Instructable will use your computer to poll Twitter for direct messages, then forward them to an X10 device (CM11A), which will send the commands along the power wiring, to control your lights (LM465).

You will need:
- Computer running Linux with an always on internet connection
- Serial port (a USB to serial adapter is fine - Meritline )
- CM11A (Ebay )
- LM465 (Ebay )

These instructions will allow for multiple devices to be controlled. When all is said and done, it should be pretty straight forward how to control more than one device.

Note: I have no affiliation with X-10, Meritline, Ebay, or any Ebay sellers.

Step 2: Install the LM465 and a Lamp

In order to install the LM465, we need to decide two things. First, what house code will you use? Second, what do you want the unit code of the lamp to be? For the purposes of this instructable, we're going to use house code B and unit code 7. This house and unit code combination will from now on be known as B7. You can use any house and unit code you'd like. Here's a better explanation of the house vs unit code thing (x10.com ).

After choosing the house and unit code:
- Set the code on the LM465.
- Plug the lamp into the LM465.
- Plug the LM465 into the wall.



Hooray! We're well on our way to controlling that pretty little lamp with Twitter!!!

Step 3: Install the CM11A

This part is almost as hard as the last step.

- Plug the included cable into the CM11A.
- Plug the CM11A into the wall.
- Plug the other end of the included cable into your computer.

Step 4: Install Heyu

Heyu is the Linux command line application that we will use to interface with the CM11A. Unfortunately, detailed instructions on how to install Heyu are out of the scope of this Instructable. Basic steps for Ubuntu are listed below.

Basic steps are:
- Download the latest version of Heyu (here )
- Decompress the file (tar zxvf heyu-VERSION.tgz)
- Change to the directory where it was decompressed (cd heyu-VERSION/)
- Run the configure application (./Configure)
- Run make (make)
- Run make install as the super user (sudo make install)

Now, if you run "heyu on B7" at the command line the light should turn on. If it doesn't, you may need to edit the heyu config file. The heyu FAQ page is a good place to start.

 HEYU is a trademark of Daniel B. Suthers.

Step 5: Get a Twitter Account for Your Computer

You will need two Twitter accounts to make this work. One should be your normal account (if you have one). The second will be for your computer. You will be sending messages (formerly known as direct messages) to your computer's account, and it will be posting responses to tell you that it did something. In order to achieve this, your computer's account will need to follow you. This is required because you can only send (direct) messages to accounts that follow you. You can follow your computer, but it is not required. You can make either (or both) account(s) protected. If you protect your computer's account, you'll need to follow it in order to see the responses it posts. 

- Go to Twitter.
- Register an account for your computer.
- Make that account follow you.
- Optionally, follow that account.

Step 6: Get a Key and Secret From Twitter

As of August 30, 2010, Twitter no longer accepts Basic (unencrypted username and password) authentication. Overall, this is a good thing. The problem is that means little applications such as this one got a bit more complicated. In order to use this application, you'll need to register an application at dev.twitter.com  (this is a client application, not a browser application, and will require "Read, Write, & Direct Messages" access). You'll also need to install python-oauth2.

Using the key and secret you got from Twitter, you can use the Twitter Three-legged OAuth Example  to generate your OAuth token and OAuth secret.

Those four hashes,  the OAuth Token, OAuth Token Secret (from the Three-legged example), Consumer Key, and Consumer Secret (from Twitter), separated by line feeds, go into a file named "Twi_X10.conf".

"Twi_X10.conf" should look like this:

WWWWW_OAUTH_TOKEN_WWWWW
XXXXX_OAUTH_SECRET_XXXXX
YYYYY_CONSUMER_KEY_YYYYY
ZZZZZ_CONSUMER_SECRET_ZZZZZ

Note: Please excuse me if I've messed up some of the OAuth terminology.
Also note: If you want to read up a little bit more on Twitter's Basic Auth to OAuth transition, read here .
Also also note: The "Read, Write, & Direct Messages" access bit is a new requirement. If you got your keys before June 30, and hadn't set that permission, the script may have stopped working. You will need to make sure the permission is set properly in the application settings, and get a new key/secret combination.

Step 7: Download My Python Script and Add Users

Download the attached python script. Put it in the same location as "Twi_X10.conf" from the previous step.

Make another file, named "Twi_X10_Users.conf" (in the same directory), and put the Twitter handles of the people you want to be able to control the X10 devices (separated by line feeds).

"Twi_X10_Users.conf" should look like the following:
YOUR_TWITTER_HANDLE
YOUR_ROOMMATE'S_TWITTER_HANDLE
YOUR_MOM'S_TWITTER_HANDLE

You may be thinking to yourself, "Why is this necessary? No one can send a message to my computer's account unless my computer is following them." You'd be right. This is duplicating that restriction. I added it in originally, and it's still there. We're moving on.

Step 8: Run the Python Script

Add execute permissions to the Python script
"chmod +x Twi_X10.py"

Run the Python script
"./Twi_X10.py"

Now, tweet the following (replacing COMPUTERTWITTERACCOUNT with the account you created in step 5):

"d COMPUTERTWITTERACCOUNT x10&on B7"
Wait a second or two, the light should go on, and your computer should reply with a tweet that says:
"Sun Jun 26 12:00:00 2011: on B7 from YOU, status: OK"

 "d COMPUTERTWITTERACCOUNT x10&off B7"
 will turn the light off, and you'll get a response along the lines of the following:
"Sun Jun 26 12:00:00 2011: off B7 from YOU, status: OK"

Congratulations, you can tweet that lamp on and off!!!

The "x10&" needs to be the first thing in the message. After that, anything that you can put on the heyu command line is fair game. The application simply replaces the "x10&" with "heyu" and runs that command at the command line.

Step 9: Expanding Your Setup

You can now repeat step 2 with more X10 devices to expand your setup. This should work with any standard X10 lamp or appliance modules (or any other X10 home automation device). You can also use aliases for your lights by setting them up in the heyu config. Read up on heyu and leave comments about what fun stuff you've made your house do.

Also, vote for me in the Make It Tweet Challenge!