LightwaveRF and Siri

Posted by Mark Hornsby on 20/12/2014

This weekend I've successfully managed to get Siri talking to my LightwaveRF set up. The video below shows this in action using one of my sockets that I'm currently using to turn my christmas lights on and off! Carry on after the jump for details ...

So if you're interested in doing the same you'll need the following:

  • An Apple iOS developer license
  • An Apple device with Siri (No jail-breaking required!)
  • The LightwaveRF wifi link
  • Some LightwaveRF devices to control

Here are the steps to get it working:

Firstly you need to install the server side process that handles the HomeKit communication from your iOS devices. You can download HAP-NodeJS from github. You'll need to get Node.js up and running for before you can run this code.

Now download the source for the iOS app that interacts with HomeKit to pair devices and interact with their services and characteristics - HomeKitDemo. Compile this and install it on to your iOS device.

Run the server by executing:

> node Core.js

Now when you fire up HomeKitDemo and hit the + button to add a device you should be able to pair with any of the accessories listed in the Hap-NodeJS accessories folder. For my example above I paired with the device definied in the Light_accessory.js file. The code to pair the device is at the top of the file.

Once paired it should be possible to see the characterisitics and to cycle the power state of the light. (I removed hue, saturation and brightness settings as I just want on / off at the moment.)

Now we have Siri talking to our HAP-NodeJS server we can add code to switch the LightwaveRF devices on and off. Modify the Light_accessory.js file and change the execute function (called when the power state characteristic changes) to the following:

var execute = function(accessory,characteristic,value) {
	var message;

	if (value) {
    		message = new Buffer("139,!R1D1F1|christmas lights|On\n");
	} else {
		message = new Buffer("140,!R1D1F0|christmas lights|Off\n");
    	}

	var client = dgram.createSocket("udp4");

	client.send(message, 0, message.length, 9760, "192.168.0.14",
		function(err, bytes) {
			console.log(err);
			client.close();
		}
	);
}

If you've ever used the LightwaveRF UDP protocol the above should look familiar. You may need to change some of the values:

  • R1D1 - this refers to room 1 device 1 (if you already have things paired with the wifi link then you should just set the correct room / device number)
  • The IP address for the UDP packet - I can't seem to get Node.js to correctly set the SO_BROADCAST flag and send to 255.255.255.255, so I've targeted my wifi link ip directly.

At this point you should be able to use HomeKitDemo to turn your LightwaveRF device on and off. As HomeKitDemo uses the HomeKit API this will also allow Siri to talk to your devices as well.

All credit to Khaos Tian for publishing HomeKitDemo and HAP-NodeJS, it wouldn't be possible for me to have done the above without his code!

I'm now planning to do the following:

  1. Get HAP-Node.js running on a Raspberry PI rather than my Mac and ensure that it can send broadcast packets
  2. Develop my own LightwaveRF link using a Spark Core
  3. Enhance HAP-Node.js to act as a true bridge to the Spark Core and have the core handle adding / removing devices and advertising them through HAP-Node.js
  4. Enhance the iPhone app to allow it to speak to the Spark Core to add and pair devices (this should make the whole end to end process easier for the user)