Introduction: Control X10 Modules Via MythTV

About: I am a graphic art hobbyist, web cartoonist, and wannabe electronics hobbyist. Other hobbies: cooking, baking, exercise, computers, video games, trivia, and some more I'm probably not remembering.

Good day!
I recently set up a MythTV box in a room where several X10 modules are installed to help set the proper lighting in the room.

First off, if you don't know what MythTV is, think of it as an open-source, free TiVO. If you have the right components for a computer, and you have the right OS and software, you can have a MythTV box giving you DVR capability, even hi-def DVR, for much cheaper than TiVO or your cable/fibre provider's DVR (YMMV, naturally).
I am using the KnoppMyth linux release, which is the Knoppix liveCD remastered into a MythTV standalone/backend installer and a front-end liveCD (meaning you don't need to install it for just frontend use).
There's plenty of documentation about installing and running KnoppMyth, or MythTV in general, so I'm not going to bore you with those details here.

I have an old X10 Firecracker kit, which comes with a lamp module, a transceiver module, a remote, and an serial device known as a CM17A, aka the FireCracker. It's a small black adapter that plugs into a serial port on your computer. I have also installed several X10 wall switches for the room, so that the room lighting as well as a lamp are all controlled by the X10 remote. Lights can be dimmed, brightened, turned on or off. I don't have any appliances but in theory, you could get additional modules, and the sky is the limit (radios, microwaves, gas/electric fireplaces...).

There are three things I want to do to make this work:
1. Get control of the X10 serial adapter
2. Give MythTV the ability to control the device
3. Give my MythTV remote control that control as well

Prerequisites:
1. A working MythTV system, for TRUE ease of instruction-following use KnoppMyth
2. For step 3, you need a remote control
3. Some linux knowledge.
4. MythTV box needs a network connection to download files. If you can't provide this, download the appropriate files separately and copy to the MythTV computer somehow.

Onward, friends!

Step 1: Get Control of the CM17A X10 Serial Module

With the MythTV box booted up hit escape enough times to exit the MythTV frontend.
Once you're at the KnoppMyth desktop, right-click to access the root menu, select XShells, and then Xterm. I won't have a lot of screenshots, because a lot of the work you'll be doing for these steps is in the terminal... but I'll show some screenshots of key events so you know what to expect.
(Note, in order to make the tutorial readable, in various places I split commands across multiple lines with a \ -- the \ indicates to the shell that you are continuing the same command on a new line. This way, if you're copying/pasting commands out of the tutorial, you won't have any troubles pasting the commands that span across multiple rows.)

Once you're in the terminal, change to your super-user in order to install the modules you'll need to control the X10 devices. Typically the command is su, followed by the password.
sh-3.1$ suPassword:root@holodeck:/home/mythtv#

Change to a working directory of your choice for downloading a package and installing. Out of habit, I use /opt. You'll need to download this file:
http://search.cpan.org/CPAN/authors/id/B/BB/BBIRTH/ControlX10-CM17-0.07.tar.gz
root@holodeck:/home/mythtv# cd /optroot@holodeck:/opt# wget \http://search.cpan.org/CPAN/authors/id/B/BB/BBIRTH/ControlX10-CM17-0.07.tar.gz

When wget is finished, if there are no errors, unpackage the tarball:
root@holodeck:/opt# tar -xzvf ControlX10-CM17-0.07.tar.gz

It should display a list of files that were unpackaged. Change into the ControlX10-CM17-0.07 directory and read the README for more information on how to proceed. When you're done reading, here are the steps to follow to install the module:
root@holodeck:/opt# cd ControlX10-CM17-0.07root@holodeck:/opt/ControlX10-CM17-0.07# perl Makefile.PLChecking if your kit is complete...Looks goodWriting Makefile for ControlX10::CM17root@holodeck:/opt/ControlX10-CM17-0.07# makecp CM17.pm blib/lib/ControlX10/CM17.pmManifying blib/man3/ControlX10::CM17.3pmroot@holodeck:/opt/ControlX10-CM17-0.07# make testPERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.tt/test1....ok                                                                All tests successful.Files=1, Tests=29,  8 wallclock secs ( 0.02 cusr +  0.00 csys =  0.02 CPU)root@holodeck:/opt/ControlX10-CM17-0.07# make installWriting /usr/local/lib/perl/5.8.8/auto/ControlX10/CM17/.packlistAppending installation info to /usr/local/lib/perl/5.8.8/perllocal.podroot@holodeck:/opt/ControlX10-CM17-0.07# 

While you're the root user, run the next two commands to ensure you have all the serial port support in place. Trust me on this one.

root@holodeck:/home/mythtv/x10# apt-get update...root@holodeck:/home/mythtv/x10# apt-get install libdevice-serialport-perl...Setting up libdevice-serialport-perl (1.002-0.3) ...root@holodeck:/home/mythtv/x10# 

Next, you need to fetch a script to control the modules. For this we'll go to the MythTV-User mailing list and snag a script written by a person calling himself John, and posted to the list by Kenneth.
Use ctrl-D to switch back to the default user in your knoppmyth terminal, and use the cd command to ensure you're in the home directory. You can download the attachment to that email with the following command. Note, the flag to wget is a capital o, not a zero or lowercase o. The purpose is to rename the file to something useful, since otherwise it's just "attachment.bin" which doesn't do us any good. Link to the file below.
http://mythtv.org/pipermail/mythtv-users/attachments/20060607/e3181c75/attachment.bin
root@holodeck:/opt/ControlX10-CM17-0.07# exitsh-3.1$ cdsh-3.1$ wget -O x10-myth.tar.gz \http://mythtv.org/pipermail/mythtv-users/attachments/20060607/e3181c75/attachment.bin

Naturally, unpack the tarball, then change into the directory it makes:
sh-3.1$ tar -xzvf x10-myth.tar.gz x10/x10/mainmenu.xmlx10/x10.plx10/x10.xmlsh-3.1$ cd x10

Check that the x10.pl script is using the proper device for your serial port by opening the file with your favorite text editor and changing /dev/ttyS1 to the proper device (it might be /dev/ttyS0 or /dev/ttyS2). I had to change mine to /dev/ttyS0 to work. The only way to know for sure is to test them, so here's how.
You'll need to know your X10 devices' house codes and device codes. For example, my ceiling lights are A2, and my lamp is A3.

sh-3.1$ ./x10.pl A3Jsh-3.1$ ./x10.pl A3K

This turned my lamp on, and then off. Success! I now have control over my X10 devices! Just to be sure, I also issued a few commands to the ceiling lights, to be sure I could dim them. Here's a video:



So, we're good to go! Next step: Give MythTV control.

Step 2: Give MythTV the Ability to Control the Device

This step isn't going to take as much time. As if the last step took very long?

You're going to create a custom root menu for MythTV, which you can do one of two ways. you COULD edit the default and/or theme menus in /usr/share/mythtv/themes or you could create one in your default myth user's .mythtv directory. The latter was my choice, but I copied the system default theme menu so I didn't have to start over.
sh-3.1$ cdsh-3.1$ cp /usr/share/mythtv/mainmenu.xml ~/.mythtv/mainmenu.xml

While you're at it, copy two files from the unpacked tarball file into the .mythtv directory:
sh-3.1$ cp ~/x10/x10.* ~/.mythtv

This copies x10.pl and x10.xml into place.
Next, using your favorite text editor, add to the mainmenu.xml file near the end, right above the section of the XML file that starts with <!-- <button>:
   <button>     <type>X10</type>     <text>X10</text>     <action>MENU x10.xml</action>   </button>

Note, do not prepend the path to x10.xml -- as long as x10.xml is in the .mythtv directory, you're good. Some themes may have special settings so that certain menu items also have icons. I don't know how to do that (yet) so that's not in this tutorial. Not to mention, as you can see from screenshots, I'm not using such a theme.

Next, you'll want to edit the file x10.xml so that you can list all of the commands you want to be able to execute. Again, the x10-myth tarball has samples in the file already, so you can start with those, or create your own.

Start by changing the mythmenu name to something more appropriate, like "Light Controls"
Note how a menu item is at basic structured:
   <button>     <type>CeilingLightsOn</type>     <text> Ceiling Lights On</text>     <action>EXEC ~/.mythtv/x10.pl A2J</action>   </button>

Define a button; set the type; the text is what displays in MythFrontend; the action is an EXEC which calls the perl script to execute a command; then close the button definition. Not too difficult.
I created entries to turn the ceiling lights on and off, the lamp on and off, and to dim the ceiling lights in steps, plus a preset "Movie Mode" which turns on the lights and dims them right away to 90% dim.

Also, fix up those paths to the x10.pl script. change all of the /myth/x10.pl to /home/mythtv/.mythtv/x10.pl so that the Frontend can find these items. Restart your myth session/frontend by hitting the key-sequence ctrl-alt-backspace (NOT ctrl-alt-delete). this will stop and restart the X session and reload the myth Frontend. Take a look at your pretty new menu, and take it for a test-spin!

Video below.


Step 3: Give the MythTV Remote Control Power Over X10

So I do want to take this one step further. My TV capture card came with a LIRC-supported, Myth-supported remote control that so far works great with MythTV. However, I want it to work with this new functionality, too!

I want to rebind several of the buttons on the remote to control the lights.
The Hauppauge WinTV PVR-350 comes with a grey remote control that has four colored buttons across the bottom. A proper LIRC (Linux Infrared Remote Control) install should make all four of these buttons visible to your MythTV machine, and Knoppmyth does take care of this terrifically.

However, it also has these four buttons assigned to functions already, but I know I'm not using these functions.

In your terminal window, make sure you are back to the regular mythtv user, NOT the root user. Use your favorite text editor to edit the file .lircrc --
sh-3.1$ vi .lircrc 
In this file, you can set various button functions. If you need additional information about how to do this, there's a good information at the bottom of this page at LIRC.org.
Knoppmyth already has a fully-configured file at this location, so to change it up, find the section titled:
### MYTHTV SETTINGS FOR IT'S CONTROL
In this section is a definition right at the top for the GREEN button:
# Program Guidebegin   remote = grayHauppauge   prog = mythtv   button = green   config = F2end
I changed it to this:
begin   remote = grayHauppauge#   prog = mythtv#   button = green#   config = F2   prog = irexec   button = green   config = /home/mythtv/.mythtv/x10.pl AP s A2J "A2-90" &end

This means that any time the green button is pressed, a small program called 'irexec' will run the command '/home/mythtv/.mythtv/x10.pl AP s A2J "A2-90" &' which should, in theory, set "Movie" mode in the media room by turning off all devices, turning on the ceiling lights, and then dimming them by 90%.

I also had to search through the text file for any other mentions of the red button. It is possible for the button to do different things in different programs, but not if you want to use irexec to trigger general commands from anywhere in the system. I also did this when I configured the yellow, red, and blue buttons.

And it works! See the video to witness the goodness of full X10 control with the Hauppauge remote. In this video I have also configured the red button as "All Off"; the yellow button as "TV Mode" which turns off all devices, turns on the lamp, turns on the ceiling lights and dims them by 50%, and the blue button turns the ceiling lights on full. The true beauty in this is that, as long as the MythTV computer is turned on, these buttons will always control the lights. You don't need the TV and audio receiver turned on, you don't need to be watching any recordings or even using the TV or audio system at all... the remote can be used as a glorified light switch, if you so choose. My other 3 button configs are below the last video.



begin   remote = grayHauppauge   prog = irexec   button = red   config = /home/mythtv/.mythtv/x10.pl AP &endbegin   remote = grayHauppauge   prog = irexec   button = yellow   config = /home/mythtv/.mythtv/x10.pl AP s A2J "A2-50" s A3J &endbegin   remote = grayHauppauge   prog = irexec   button = blue   config = /home/mythtv/.mythtv/x10.pl AP s A2J &end