The random ramblings of a French programmer living in Norway...
← Home-automation experimentsAre Cheap Androids An Electric Dream →
  I'm in love with Kira
Sun 27th March 2011   
No, 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 reloaded

Kira 128 show-off
Kira 128 show-off
As I posted earlier this week, I'm currently toying with the whole Home-Automation concept.

The way I see it, I should be able to control everything from one single place, and in 2011 what makes sense is to make it web-based. The internet protocols and software stack has evolved to the point where it's basically a no-brainer: Between browsers that can display rich content using CSS (and/or HTML/canvas, Silverlight, Flash, or whatever else to come in the future) plus PHP and CGI on the server side, you can really do whatever you want3. So basically as long as a device can access web pages and understand javascript, it can be used to control my system.

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 controls

Of 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 panel

The 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.

The remote control web interface (WIP)
The remote control web interface (WIP)

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 code

Interacting 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>

The send_code function is just a simple javascript function performing an AJAX operation4:

function send_code(code)
var ajax=new XMLHttpRequest();
if (code<100) code="0"+code;
if (code<10) code="0"+code ;"GET","remote2.htm?button"+code,true);

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 support

After 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:

<a href="?button=001">POWER</a>
<a href="?button=002">UP></a>
<a href="?button=003">DOWN></a>

$dest_address = ''; // 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 step

What 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.
5. Other people are Googling, these days instead I'm using DuckDuckGo
6. You of course need to replace the hard coded IP by the one used by your device.
comments powered by Disqus