Insteon PowerLinc ModemAs explained in yesterday’s article, I have been overhauling my home automation system (HAS) and have decided to release parts of my controller software written in Python and runs on Raspberry Pi. I have been working on breaking apart the controller software so that each piece can be ran standalone. For example, yesterday’s article¬†showed how to read your Google Calendar with Python. By doing that, you can schedule events to occur in your HAS by simply adding items to your Google Calendar.

Today, I want to share with you some Python code that allows you to control Insteon & X10 devices in your HAS using the Insteon PowerLinc USB modem which you can get from http://www.smarthome.com/powerlinc-modem-insteon-2413u-usb-interface-dual-band.html for about $80. Since the PowerLinc requires an always-on computer to be connected to it, it’s recommended to use a low powered computer such as the Raspberry Pi. Since the code below is written in Python, you can also use it from Windows or Mac if you want. Just make sure you change the “port” property in the serial connection to match your environment. If you combine this code with yesterday’s code, you can schedule your lights and other appliances to turn on and off by scheduling items in your Google Calendar.

Here is the code that allows you to send commands to an Insteon device using your Insteon PowerLinc USB modem.

# Note: Set PLM to sync mode first followed by device sync
# Commands list: http://www.madreporite.com/insteon/commands.htm

import serial

ser = serial.Serial(
                    port='COM5', #'/dev/ttyUSB0'
                    baudrate=19200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS,
                    timeout=0
                    )

ser.flushInput()
ser.flushOutput()

message = bytearray()
message.append(0x02) # INSTEON_PLM_START
message.append(0x62) # INSTEON_STANDARD_MESSAGE

# device id
message.append(0x29) # Addr 1
message.append(0x2A) # Addr 2
message.append(0x9F) # Addr 3

message.append(0x0F) # INSTEON_MESSAGE_FLAG
message.append(0x12) # 0x12 = FAST ON, 0x14 = FAST OFF, 0x19 = STATUS
message.append(0xFF) # 0x00 = 0%, 0xFF = 100%
ser.write(message)
ser.flush()

ser.close()

Here is the code that allows you to read the status of your Insteon devices as their state changes. To test this, run the code and flip an Insteon light switch either on or off. When you do, you will see the status of that device in the console.

# Note: Set PLM to sync mode first followed by device sync
# Commands list: http://www.madreporite.com/insteon/commands.htm

import serial, binascii

ser = serial.Serial(
                    port='COM5', #'/dev/ttyUSB0'
                    baudrate=19200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS,
                    timeout=0
                    )

ser.flushInput()
ser.flushOutput()

reply = ''
while True:
    string = ser.readline().strip()
    if string != '':
        reply = reply + str(binascii.hexlify(string))
        if len(reply) == 22:
            print binascii.hexlify(string)
            break

ser.close()

Here is the code that allows you to send commands to an X10 device using your Insteon PowerLinc USB modem.

# Note: Set PLM to sync mode first followed by device sync
# Commands list: http://www.madreporite.com/insteon/commands.htm

import serial, time

ser = serial.Serial(
                    port='COM5', #'/dev/ttyUSB0'
                    baudrate=19200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS,
                    timeout=0
                    )

ser.flushInput()
ser.flushOutput()

# X10 test
message = bytearray()
message.append(2)
message.append(99)
message.append(176 + 0)
message.append(0)
ser.write(message)
ser.flush()
time.sleep(0.5)
message = bytearray()
message.append(2)
message.append(99)
message.append(176 + 2) # 2 = on, 3 = off
message.append(128)
ser.write(message)
ser.flush()

ser.close()

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.
$2.00
$5.00
Other

Related Posts

6 Responses to Home Automation Control with Python, Insteon PowerLinc, and Raspberry Pi

  1. vitovega says:

    I realize this post is a bit old, but I just stumbled upon it and am extremely interested in the content. I attempted to run the second script, the one that allows me to read the status changes of my devices, but nothing came up in the console. I’m running it on a RPi and I changed the port to /dev/ttyUSB0. I chmod’ed the created file to be executable and when I execute it, the program appears to run without error, but flicking on/off any of my insteon devices doesn’t produce anything in the console, the cursor just blinks expectantly. I can currently control my devices with a IRLinc and with qwha on the same RPi. Any idea why it wouldn’t be giving me any feedback?

    • LuCuS says:

      Sorry for the late reply. I’ve been doing a lot of traveling lately and haven’t had much time for moderating this site.

      Before running these scripts, put your device that you want to monitor into “sync mode”. You can do that by holding the “set” button on the side of it until it beeps and begins flashing. Then, press and hold the “set” button on the side of the PowerLinc until it double-beeps indicating that it was able to pair with your device.

      You’ll need to repeat these steps for every device you want to monitor. After you have paired your devices with the PowerLinc, you can run the scripts.

  2. vitovega says:

    Thanks for the response. I actually already have all the devices linked to the PLM (i.e. I’ve done those steps) and I’m currently able to control everything just find using QWHA on a raspberry pi no problem. I’ve also been controlling just the dimmer with a IRLinc for a few years now. I thought it would be safer to try the monitoring script first, but, despite it appearing to run without error, nothing is showing up when I flick the light or turn off/on the OutletLinc. Any idea why that might be? Maybe I should just try to send it a command, but, again, I wanted to try to monitor first… Thanks for taking the time

    • LuCuS says:

      I’m not sure why it’s not working for you. I’ve been using this script for quite a while without any problems. I did however have to repair my devices in a particular order (put device in pair mode first followed by the PLM in pair mode). That’s why I suggested trying that. I know the article says to put the PLM in pair mode first, but I’ve had better success lately using the other sequence.

      The next thing you can do is to add “print string” between lines 20 and 21. It should begin outputting some stuff if the PowerLinc is capable of reading anything off the line. Also, try setting the “timeout” flag at line 12 to “30” so that it has time to buffer a little.

      Also, here is some code from another project I built that might help get you going: http://www.prodigyproductionsllc.com/downloads/pylinc.py

  3. vitovega says:

    Whoa, thanks for taking the time. I’ll try this out and report back!

  4. dan krill says:

    Just want to make sure everyone is completely clear here. You actually have to link the slave devices to the PLM twice! Once starting at the master to the slave to control it, then AGAIN but this time from the slave to the master to get its feedback. If you still aren’t getting data after doing that, you may have faulty hardware because this works for me as well using my raspberry pi B and 2413U insteon PLM.

Leave a Reply