Sunday, 8 April 2012

The UDP Interface

A bit of Googling on all things LightwaveRF led me to this site.  It' someone's Blog where they describe how to control the LightwaveRF WiFi Link box using commands in UDP segments.  Very interesting and perhaps a way to get around having to use LightwaveRF's software, (which has distinct "improvement opportunities").

In simple terms you form a UDP segment with a LightwaveRF command in it, send it to the WiFi Link (which listens on port 9760) and, hey presto, it executes the command.

Example: The command “000,!R1D1F1|” means room 1 ("R1"), device 1 ("D1"), switch on ("F1").  Conversely “000,!R1D1F0|” would mean switch off.  The "000" is a command reference, (so you increment it for each command in turn).

I thought I'd have a play myself using the only programming language I've any recent experience in, Visual Basic.  Imagine my delight when I found out that Microsoft now provide free "Express" versions of Visual Basic, C# and C++.

So dusting off my Visual Basic knowledge I hacked together an application that can send UDP segments to my WiFi Link. I've used the Winsock control before for IP/UDP/TCP stuff but in the latest version of VB (Visual Studio 10) you have to import the object.  The example at this MSDN page was what I used as the basis of my code.  

With a bit of trial and error I worked out that you need to:
  1. Bind a UDP socket to local port 9761*.
  2. Form a UDP packet with the LightwaveRF command in it.
  3. Send the command to port 9760 on the IP address that you WiFi Link is on.  (Both PC and LightwaveRF were on my LAN so I just used the private IP address that had been allocated to the LightwaveRF).
  4. Wait for the response which is in the form of  “000,OK”
....and the command is executed by the LightwaveRF WiFi Link.

* This was the hardest thing to find out.  The LightwaveRF sends all response packets back to port 9761, no matter what port you send from.  I needed to download and install Wireshark to spot this.

When you first send a command from your PC, a prompt comes up on the WiFi Link asking you to confirm access from this particular IP address.  A security feature I guess and you confirm access by pressing a button on the WiFi link.

I was intrigued by the pipe ("|") character in the command and a but more tinkering showed how you could get extra messages on the LED display of the WiFi link by extending the command.  For example "“000, !R1D1F1|Study|Dehumidifier” would mean "Study" and "Dehumidifier" appear on consecutive lines on the WiFi link. 

Looking around the LightwaveRF forums there's lots of talk of the LightwaveRF "UDP Protocol".  So more investigations and updates on this later...