I recently got to play with the LightwaveRF Wifi Link box (http://www.lightwaverf.com/). They’re becoming fairly common in the UK with companies like screwfix selling them. You can get a range of devices to pair with the box including lights, plug sockets and even PIR sensors. We plugged things in, got a lightbulb paired up and had a play. It’s pretty cool however the next step was writing our own software to control it. Who doesn’t want to control their house using Ruby?
Unfortunately the LightwaveRF Wifi Link doesn’t come with an API or any obvious way to communicate with it outside the tools that the company provide. I was just about ready to get out wireshark and try to figure out the protocol it was using to communicate however, fortunately someone had already done this.
It turned out talking to the box is pretty simple over UDP. First you have to initialise the conversation and pair the device you want to use to communicate with the Wifi box. You can do that with the following:
socket = UDPSocket.new socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true) socket.send("533,!R1D1F0|", 0, "255.255.255.255", 9760) socket.close
Once you’ve done this commands are sent in the following format:
"001,!R1D2F1" Prefix: 001 Room: 1 Device: 2 Function Code: F1
The prefix is a random number between 001 and 099 Function code F0 means turn off, F1 means turn on and to Dim a device you can send FdPXX where XX is a number between 1 and 32, with FdP32 meaning full brightness. So the following code would dim Room 2, Device 3 to 50% brightness:
socket = UDPSocket.new socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true) socket.send("001,!R2D3FdP16|", 0, "255.255.255.255", 9760) socket.close
To make this easier I’ve published the LightwaveRF Ruby Gem to let you interact with the device from Ruby in a much easier way.
lightwave = LightwaveRF.new # Initial Registration (Only needs doing once per device) lightwave.register # Turn the light on in room 1, device number 1 lightwave.turn_on(1, 1) # Dim the lights in room 2, device number 3 after turning them on lightwave.turn_on(2, 3) lightwave.dim(2, 3, 50) # Turn off the lights lightwave.turn_off(1, 1) lightwave.turn_off(2, 3)
For more details or if you find any other features to add checkout the project on Github. The next step was to add this gem to a small Sinatra webserver, which I’ll post soon.