Controlling stuff from a web page is really cool. By stuff, I mean stuff in the real world, like lights, household appliances, or perhaps... killer robots.
While it's still tough to find an affordable automaton in the "killer robot" category, there are some fairly inexpensive alternatives like the version 1.0 Robosapien. This model is a bit less expensive because the Robosapien Version 2.0 is now the hot item for Christmas.
These robots are controlled via an Infrared remote, much like your TV or cable box. So how do we go about controlling Infrared devices from our PC using .NET?
STEP 1: Get an IR Blaster
First your PC needs a device to transmit IR signals. You can certainly build your own IR transmitter, but I found that for just $12, you can get a very good quality device from www.irblaster.info.
Here is what the $12 device looks like. It has a basic DB9 Connector to fit into an available COM port:
STEP 2: Setup WinLIRC
WinLIRC is an open source program that allows you to transmit and receive infrared remote signals. When you run WinLIRC, you get a config dialog where you select the COM port that your IR transmitter sits on.
Notice the "Config" textbox at the bottom. WinLIRC works off config files which store remote code information for different devices. There is an extensive database of remote codes already available for a whole bunch of IR devices.
For the Robosapien, Eric Buehl created a compatible config file as part of a project. You can download his config file here.
Once you download your config file and enter it into the WinLIRC configuration screen, you can select OK.
Now you will be at the WinLIRC test screen. Here you can select a remote (defined in your config file) and send a code to the IR device. I call this a "test screen" because that's all its really good for - making sure that your IR Blaster and config file are all working happily.
Is it working? Is your PC making the device do stuff? Cool, then you can click the "Hide Window" button which will make WinLIRC disappear into your system tray and appear like a little virtual LED light:
STEP 3: Using WinLIRC as a Server
Using the WinLIRC test screen is fun and all, but we want to control our IR device from some .NET code! To do this, WinLIRC provides a telnet server which we can communicate with on TCP port 8765.
To test the telnet server you can open a command prompt and type:
telnet 127.0.0.1 8765
...after which you will be logged into the WinLIRC telnet service. There are a few basic commands you can type here, such as:
(gives a list of available remotes from your config)
LIST [REMOTE name]
(gives a list of remote codes available on a remote)
Now, to send an actual IR command, there is some security in place here. After all, this is a telnet server and if you are not behind a firewall, any lame hacker could come in and start using your killer robot against you!!
So to send IR codes through the telnet server, we need to add a password to our registry. Run Regedit and go to the HKEY_LOCAL_MACHINE\SOFTWARE\LIRC node. Add a new String value named password with the value of a password:
Now, in a telnet session, you can use a command to send IR signals in this format:
[PASSWORD] [REMOTE] [CODE] [# repeats]
For example, to have my SONY-TV change to channel 4, I would use the command:
password SONY-TV 4 1
STEP 4: Using the WinLIRC Telnet Service from .NET
.NET makes a lot of things easy, including Telnet! To perform a telnet session to the WinLIRC server and send a command, the code is short and sweet:
TcpClient client = new TcpClient("127.0.0.1", 8765);
Stream s = client.GetStream();
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.WriteLine("password SONY-TV 4 1");
Oh, so much to do! We could create an ASP.NET page to send whatever IR codes we want to our household appliances! We could create a queuing mechanism so that multiple users could submit commands to the devices simultaneously! We could create an army of Robosapiens equipped with Lasers and control them all from the comfort of our keyboard.