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 system.net.sockets 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...

6 comments:

  1. There is no space between the comma and OK in the response.Also to be clear the 000 should reflect the sequence number of the original command. This typically increments by 1 each time.

    Thanks for the pointers

    ReplyDelete
  2. thank you very much for taking the time to post this lightwaveRF info. This was very helpful to me, and I've starting working on some php stuff for my NAS.

    ReplyDelete
  3. hi, could you pls post the php code for the receive part? I can't script something to receive packets no matter what I try. I'm really frustrated.

    thanks

    ReplyDelete
  4. Hi,
    I didn't use PHP but rather used C# through Microsofts Visual Studio. I used the udpclient which I recall was easy to do once I worked out you need to get your client machine to listen on port 9761.

    This chap http://blog.networkedsolutions.co.uk/?p=149 looks to have used PHP so try his blog or maybe try contacting him.

    Cheers
    Paul

    ReplyDelete
  5. here's something I was playing with for the power monitor


    ";
    echo "currently using $powerNumsArr[0]kW
    ";
    echo "peak today $powerNumsArr[1]kW
    ";
    echo "running total today $powerNumsArr[2]kWh
    ";
    echo "total used yesterday $powerNumsArr[3]kWh
    ";

    //echo 'Please Enter';
    //echo "Power in use " . $msg . PHP_EOL;
    ?>

    ReplyDelete
  6. oh. that didn't go well

    ReplyDelete