With pulses being detected, the last step in this power consumption sketch is to properly count the pulses, measure the time between ‘em, and send off the results over wireless.
There are a few details to take care off, such as not sending off too many packets, and sending out the information in such a way that occasional packet loss is harmless.
The way to do this is to track a few extra values in the sketch. Here are the variables used:
Some of these were already used yesterday. The new parts are the pulse counters, last-pulse millisecond values, and the payload buffer. For each of the three pulse counter sources, I’m going to send the current count and the time in milliseconds since the last pulse. This latter value is an excellent indication of instantaneous power consumption.
But I also want to keep the packet size down, so these values are sent as 16-bit unsigned integers. For count, this is not so important as it will be virtually impossible to miss over 65000 packets, so we can always resync – even with occasional packet loss.
For the pulse time differences, having millisecond resolution is great, but that limits the total to no more than about a minute between pulses. Not good enough in the case of solar power, for example, which might stop on very dark days.
The solution is to “compress” the data a bit: values up to one minute are sent as is, values up to about 80 minutes are sent in 1-second resolution, and everything above is sent as being “out of range”.
Here are the main parts of the sketch, the full “homePower.ino” sketch is now on GitHub:
Sample output, as logged by a process which always runs here at JeeLabs:
L 22:09:25.352 usb-A40117UK OK 9 2 0 69 235 0 0 0 0 103 0 97 18
Where “2 0 69 235″ is the cooker, “0 0 0 0″ is solar, and “103 0 97 18″ is the rest.
Note that results are sent off no more than once a second, and the careful distinction between having data-to-send pending and actually getting it sent out only after that 1000 ms send timer expires.
The scanning and blinking code hasn’t changed. The off-by-one bug was in calling setblinks() with a value of 0 to 2, instead of 1 to 3, respectively.
That’s it. The recently installed three new pulse counters are now part of the JeeLabs home monitoring system. Well… as far as remote sensing and reporting goes. Processing this data will require some more work on the receiving end.