Home-automation reloadedUniversal remote controlsMy control panelThe actual codeKeen supportThe next stepNo, Kira is not the name of my girlfriend, it is not the name of my cat either, and I'm not in love with any particular film celebrity bearing a similar name.
Kira1 is only a small device you connect to your network, which in turn allows you to control anything that uses infra-red, kind of like an universal remote control.
Here it is how it looks like2:
Home-automation reloadedHome-Automation concept.
The last post was about showing information collected from the web, in particular how to show when the next metro arrives. It's cool, but it's not really automation. With Kira you can start to build the real thing.
Universal remote controlsOf course I could have chosen the easy way and buy something like a Logitech Harmony universal remote control. As far as I can tell, anyone I know who bought one only had positive things to say about it, so kudos to Logitech for designing apparently a very good product.
The reason why I did not do that, is that I wanted to be able to control everything from a single location, possibly make it react to external events (like mute the TV or stereo system if somebody calls), be able to access all that out from home (perhaps because I forgot to record a program I absolutely wanted to see), and generally speaking: Just because I can :)
So here is how it works:
- You plug the device on your network.
- You program up to 128 commands using the provided software.
- You use the Kira's integrated web server to select one of the 128 commands.
That's it. Nothing really mysterious, but I admit, that's pretty basic and far to be as practical as a remote control. Now the cool thing is that you can do much much more than that:
- The device itself has an infra-red receiver you can use to record control codes from your existing remote control; you can of course pilot your PC this way.
- You can pair two devices to create a long-distance infra-red relay system
- There is a complete UDP based API wich can be used to send raw streams of infra-red data, update the pre-programmed codes, change the DHCP parameters, update the firmware...
If you are interested by this device, you can find it on Keene Eletronics's site, here is the link to their infra-red related products. They even have a magazine showing how the various products can be used together, available in PDF here.
My control panelThe end objective is of course to write complex macros to automatically do operations like switching on the media box, switching off the digital cable TV box, set the TV input source and amplifiers to use the media box, etc...
I'm not there yet, but what I have so far is a complete web version of my media box remote control commands (using a more practical layout), which can be used from any mobile device with a touch screen.
When the web page buttons are selected, the page sends the correct pre-recorded control code to the Kira device, which then send the infra-red stream to the device.
The actual codeInteracting with the Kira device from a web page was actually less simple that I expected, mostly because when I started this project I was a total AJAX n00b
The Kira own's remote control page is very simple, it's basically just a list of clickable buttons with a onclick event:
<a href="#" onclick="send_code(1)">POWER</a>
<a href="#" onclick="send_code(2)">UP</a>
<a href="#" onclick="send_code(3)">DOWN</a>
<a href="#" onclick="send_code(128)">SHUT DOWN INTERNET</a>
var ajax=new XMLHttpRequest();
if (code<100) code="0"+code;
if (code<10) code="0"+code ;
So given a code between 1 and 128, the function creates an URL containing a string from remote2.htm?button001 to remote2.htm?button128.
The first idea I had was of course to try to just have my control panel link to these buttons (did not work), then to copy the ajax code to my own page (did not work either). The reason is because of some nasty security constraints related to something called cross-site scripting.
Basically in order to avoid hackers and phishers to exploit real sites and trick users by integrating some elements from a real site into another, there are restrictions on what can be used. An example of that is that you cannot make a script access data coming from another domain than the one where the script was loaded from. Bummer.
I needed some help.
Keen supportAfter few hours of head-scratching, duckduckgoing5 and stackoverflowing I decided to contact Keene's customer support. Of course I did that on Saturday morning, so I was planning to do something else during the week end... until surprise! I got an answer from the guy who wrote the firmware, complete with explanations on how to do things, some Wireshark packet dump showing the content of the data exchanged between the Kira device and the browser. Call me impressed.
It took some back-and-forth exchanges until we finally managed to find something that works well, so I'm going to share it here. Basically the idea is to not use the ajax method (due to the security constraints), but to instead use the UDP API.
Here is the result:
$dest_address = '192.168.1.12'; // Kira's IP
$sock = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
$msg = "cmdT".$_GET['button'];
socket_sendto($sock, $msg, strlen($msg), 0, $dest_address,65432);
I admit the code is a bit violent, but it's working well6.
The same code can be used to send any of the other cmdT commands described in Keene's API details document.
The next stepWhat remains to do in this part of the system is first to add the control codes for the remaining devices (the TV, the GET Digital Box, and the Thomson 5.1 system), then add support for sending multiple commands to multiple devices in one single request.
At this point my biggest hurdle is to get the page layout to nicely adapt to the native resolution and orientation of the device so it stays at a usable size on screen: A 2x2 pixel icon is not super practical to click on :)
I'm going to document all the parts of the system, and probably release the whole set of pages, scripts and css data so anyone can have fun of their own.
That's all for today :)
1. Keene IR Anywhere↩
2. I had to add a colourful gradient and orange neon glow effect, because truth be told, the while square box with cryptic text on it is not that exciting to look at.↩
3. At the pain of having to deal with a security model which really does not want you to mix and match stuff from various sources, but that can be dealt with.↩
4. The actual code is doing error checking, I removed it for the sake of readability.↩
6. You of course need to replace the hard coded IP by the one used by your device.↩