Tuesday, 24 April 2012

Power Measurements and the UDP Interface

More reading of the LightwaveRF forum led me to discover a command that allows you to interrogate the WiFi Link unit to get power measurements.  I want to use this as, although the handset app and web interfaces give you instantaneous measurements, you can't track usage over time.

So if you send the command 123,@?\0 in a UDP message (see previous post on how to do this) to the WiFi Link you get a response along the lines of 123,?W=150,7710,3148,14970.  Here:
  • 150 is the current instantaneous power measurement in Watts
  • 7710 is the maximum power measurement for the current day (in Watts)
  • 3148 is the cumulative usage since midnight that day (in Watt Hours)
  • 14970 is the total usage (in Watt Hours) for the whole of the previous day
Based upon this command I knocked up a Visual Basic application that takes one of these measurements every minute and logs the resulting information to a text file.  Now the application is working on my Vista PC which is crumby and unreliable and it's also my family PC so there's a good chance of it being stopped.  However if I can get the application to run for long enough I can perform some interesting long term usage analysis.

Here's a graph I put together using this data:




Max_Day (blue series) is a day when the whole family was in and we had another family visiting us (so lots of cooking, making cups of tea etc).

Min_Day (red series) is a day when we were in and out in the morning up to lunch time and then went out in the afternoon and evening.

So on the Max_Day you can see:
  • Minimal usage through the early hours
  • First cup of tea of the day at 0630 is
  • Big lunch so lots of usage
  • Even bigger tea (between 1900 and 2000) so even more usage
  • To bed just after 2200
...and on the Min_Day you can see:
  • Slightly later breakfast but lasted longer (probably something cooked)
  • A light lunch
  • Out in the afternoon and then in to the evening so minimal usage
  • Back just after 2200, a bit of usage then off to bed
So for me very interesting as I can see explicit links between what I do and the resulting usage. 

Looking at the cumulative measurements (see below) are also interesting:


So as expected the lines (so usage) are similar for the early hours but then diverge significantly.  Min_Day is also interesting as it highlights the quiescent or steady state usage of my house through the things that are always on.

Focussing in on a shorter time period for the absolute measurements is interesting:

You can see that usage is broadly quantised which I assume is down to things like my fridge and freezer kicking in and out.  It shows my houses steady state is roughly 270 Watts.

So overall this is all very fascinating to me.  The power meter and the ability to log measurements gives you excellent visibility of the cause and effect of using electrical items.  Additionally, the steady state measurements show how important it is to look at the energy rating of electrical items before you buy the,.


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