Python LogoA couple of weeks ago, I began a series of articles showing you how to do home automation easily and relatively cheap. In those articles (Part 1, Part 2, Part 3), I introduced you to the X10 home automation products such as the screw-in lamp module, appliance module, wall switch, and controlled wall outlet. I also showed you how to use your GE Simon XT home security system with your X10 home automation network. In part 4 of the home automation articles, I introduced you to the Insteon Hub which can be used to control all of your X10 modules using your cellphone, tablet, and computer. Today, as promised, I will now show you some code that you can use to control your devices from your own software. This code can easily be ported to Java, C#, or any other language of your choice. But, for simplicity purposes I will be providing you the code written in Python. Plus, this code can be used with the other automation applications I have been showing lately and will be showing in the near future. So, let’s begin.

The first thing you will need in order to control your X10 devices from your own applications is the Insteon Hub. If you have not read Part 4 of my home automation series, you should do that now as the code in this article will only work with that device. As explained in that article, make sure you download and install the “Insteon for hub” app onto your cellphone or tablet as we will be using that for a quick setup before jumping into the Python code. Launch the app on your device and connect to your hub. Next, click on the “Settings” icon in the lower-right corner and click on “Edit Settings”.

Insteon Hub App Edit Settings

On the Edit screen, click on the “House” link. On the next screen, scroll down and make a note of your “Local IP” and “Port”. You will be needing both of these later on in the Python code. Next, scroll to the very bottom of the settings page and make a note of your “Hub Username” and “Hub Password”. I think they default to “admin” and “admin”, but I’m not 100% for certain. Regardless of what they are, it’s best that you change these to something different than the defaults. Just make sure you remember what you set them to as you’ll be needing these in the Python code as well.

Now you are ready for some Python code. Before I jump into the code, I believe it is worth mentioning that there are already a few Python packages out there that can be installed that provide you with the ability to communicate with your Insteon Hub. Since this article is about controlling X10 devices, I found it was fairly simple to write my own code as opposed to using something someone else already wrote. This decision helped me learn more in depth how to talk to the Insteon Hub and control my X10 devices in code. It also allowed me to run the code on any device, including my Raspberry Pi’s which I am using as the “brain” of my home automation system, without having to worry about installing any 3rd party packages. Instead, I am using only the packages that come pre-loaded with Python.

Before you can use the code below, you will first need to change a few things. At the top of the code, you will need to change the username, password, ip_addr, and port to match those for your Insteon Hub as explained above. Once you have those set to match your environment, the next things you will need to change are the house code, unit code, and command code that you will be passing to your Insteon Hub. Those 3 values are set & passed to the “send_command” function at the very bottom of the code. In the example in the code, you will see that I chose to test with house code = A, unit code = 1, and command code = On. You can change these to whatever matches your X10 network as long as those devices have also been registered with your Insteon Hub. You’ll also need to make sure that the computer you are running the code from can “see” your Insteon Hub. You can do that by pinging the IP address you noted on your mobile device earlier. Here’s the code.

import urllib2, time, base64

username = 'admin'
password = 'admin'
auth = base64.b64encode(username + ':' + password)
headers = {'Authorization':'Basic ' + auth}

ip_addr = ''
port = '25105'

house_codes = {
    'A' : '6',
    'B' : 'E',
    'C' : '2',
    'D' : 'A',
    'E' : '1',
    'F' : '9',
    'G' : '5',
    'H' : 'D',
    'I' : '7',
    'J' : 'F',
    'K' : '3',
    'L' : 'B',
    'M' : '0',
    'N' : '8',
    'O' : '4',
    'P' : 'C'

unit_codes = {
    '1' : '600',
    '2' : 'E00',
    '3' : '200',
    '4' : 'A00',
    '5' : '100',
    '6' : '900',
    '7' : '500',
    '8' : 'D00',
    '9' : '700',
    '10' : 'F00',
    '11' : '300',
    '12' : 'B00',
    '13' : '000',
    '14' : '800',
    '15' : '400',
    '16' : 'C00'

command_codes = {
    'On' : '280',
    'Off' : '380',
    'Bright' : '580',
    'Dim' : '480',
    'All On' : '180',
    'All Off' : '680'

def send_command(house_code, unit_code, command_code):
    house_code = house_codes[house_code]
    unit_code = unit_codes[unit_code]
    command_code = command_codes[command_code]
    req = urllib2.Request('http://' + ip_addr + ':' + port + '/3?0263' + house_code + unit_code + '=I=3', None, headers)
    res = urllib2.urlopen(req)
    req = urllib2.Request('http://' + ip_addr + ':' + port + '/3?0263' + house_code + command_code + '=I=3', None, headers)
    res = urllib2.urlopen(req)

send_command('A', '1', 'On')

Once you have everything configured, go ahead and run the application to test it. If everything worked accordingly, the device with the house & unit codes you passed to the “send_command” function should now turn on. If it did, change the command code from “On” to “Off” and re-run the app at which point your device should now turn off. You can also play around with the other commands such as “Bright”, “Dim”, “All On”, and “All Off”.

That’s it! You now have everything you need for controlling your X10 devices using the Insteon Hub and your own Python applications.

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Leave a Reply