Script to read gas pulses from a CC dev board
Here's a script (in applet form) to count gas use from a Schlumberger R5 gas meter using a CC dev board attached to the RJ11 socket on the underside of the digital display.
The CC sensor (sensor.1 in my case) alternates between 0 and 500 watts output depending on the state of the reed switch in the meter. This script just counts up and records the pulses on the livebox. The switch opens/closes once per cu ft of gas consumed.
_______________________________________________________________________________________________________
require ('xap')
require('xap.bsc')
require("string")
local io = require("io")
-- Reader for a Schlumberger R5 gas meter using a CurrentCost digital development board and an HAH
module(...,package.seeall)
info={
version="1.0", description="Gas Counter"
}
Postinterval=5
tempcount=0
gas=0
function init()
local f = xap.Filter()
f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:sensor.1")
f:add("xap-header","class","xAPBSC.event")
f:callback(gaslog)
end
function gaslog(frame)
gas = frame:getValue("input.state","text")
-- When the sensor toggles to 500 the reed switch has incrememted by 1 cu ft
if tonumber(gas) == 500 then
tempcount=tempcount+1
end
end
function gassave(self)
-- save the gas meter count at 5 minute intervals to mitigate data loss from an outage
local file = io.open("/etc/plugboard/gascuft","r")
cumulativegas = file:read("*all")
file:close()
cumulativegas = tonumber(cumulativegas) + tempcount
tempcount = 0
local cmd = string.format("echo '%s'>/etc/plugboard/gascuft", cumulativegas)
os.execute(cmd)
self:reset()
end
xap.Timer(gassave, Postinterval*60):start()
__________________________________________________________________________________________________________
I haven't yet done anything with the data but obviously posting to pachube or similar would be possible with a little work.
Easy fix make the source address be something like "dbzoo.livebox.gas:sensor"
Brett
For a CC digital development board :)
Allan, thanks for posting the script, and Brett for making some improvements.
I'm currently debating whether to use CC development boards to catch pulses from my gas and water meters or if a roomnode of some sort would be better. Any thoughts?
Using CC Dev Boards:
Pro- Cheap per board
Con- Limited number of channels available on currentcost (already using lots of IAM's etc)
Con- Extra hop via the currentcost
Using room node:
Pro- Can use a single node to count multiple device pulses
Con- A little pricier
Con- Extra hop via the hahcentral "node"
L
I think using a separate board ie a modified hahnode might be the go this would decouple the unit from the CurrentCost. This would give you more control locally for processing so that the information was only sent every minute or two. An IAM on a CC unit would trigger every pulse and this is going to create a lot of RF noise and possibly interfere with other IAM's units and/or other RF devices. Not to mentioned the additional load induced by xap-currentcost and subsequently Plugboard in having to deal with the incoming data for each generated pulse. You really want to off load this capability to a remote processor (ie an AVR)
The HAH Node are also bi-directional RF which means you can have an ACK capability to make sure the Transmission was received by the base unit. Something we do for the PIR movement sensor. Something I'm not sure the IAM unit can do either.
Using a remote node you would expose at least a couple of endpoints.
gas:reading
gas:usage
Where the reading would be an aggregation of the usage its value could be persisted in the AVR's eeprom so it would not be lost on a power cycle. Usage would be the number of ticks measured between reporting periods. For the Reading you would need some mechanism for setting this value remotely from the HAH too, that is easy enough.
Just thoughts.
Brett
I have a CC dev board with a flip-flop. The flip-flop is set as the magnet passes the reed switch and is reset by the LED so I know the last pass of the magnet has been sent.
The dev board was a free gift from CC, the flip-flop was 32p and the reed switch was 99p from Map***
Now thanks to this script I can log my gas consumption.
Hi Mark,
Would you be able to provice details and a diagram that could be used to your gas monitor. I already have the CC board, but would need to get and build the rest,
Thanks
Karl
Hi Karl
Here's the schematic I used.
The flip flop catches the pulse from the magnet and holds the output high until reset by the pulse to the LED (message sent). This means that the every pulse gets sent. The RC network is to ensure that only one pulse is sent if the magnet parks itself over the sensor.
Mark
Attachment | Size |
---|---|
gasmonitor.jpg | 49.75 KB |
Thanks for the diagram Mark, but I'm not an electrician, so just struggling with the pin numbers on the 4013. I count 6 connections to it, but can only work out the following, (assuming first side of flip flop) :-
S - Set, pin 6
Q - Q1, pin 1
R - Reset, pin 4
Set - (Vss, ground, pin 7)
CLR - ??
> - (Vdd, positive pin 14)
My chip is a CD4013BE, if you could fill in the blank and sort my mistakes out that would be great,
Thanks
Karl
Sorry about that Karl I assume everyone can read schematics sometimes :)
Hopefully this will help
The 300M resistor is something I added as I was getting a floating voltage from the LED, enough to trigger the reset.
Attachment | Size |
---|---|
gas_interface.jpg | 27.75 KB |
That's exactly what I needed, Thanks.
Making an operating system call just to write a value into a file is "heavy"
local cmd = string.format("echo '%s'>/etc/plugboard/gascuft", cumulativegas)
os.execute(cmd)
You can do the same in pure LUA which is more efficient - Its a little ODD that you use LUA for the READ but an OS call for the WRITE ?!
file = io.open("/etc/plugboard/gascuft","w+")
file:write(tostring(cumulativegas))
file:close()
OH another refinement.... You trigger the event and then check for a value. Why not filter and only trigger when you have a value of 500? Then you can compress this code down to something even more simpler.
local f = xap.Filter()
f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:sensor.1")
f:add("xap-header","class","xAPBSC.event")
f:add("input.state","text","500")
f:callback(function() tempcount = tempcount + 1 end)
One more thing you should put xap.Timer construct inside the function init() you don't need to have it outside like you do, and this is actually not recommended as it will be activated as soon as the module is loaded and not when the module it actually initialized which isn't the same thing.
Hope these tips are useful.
UPDATE: I had a crack at writing the code for to so it exposes the GAS reading as a BSC endpoint, from here is very easy to pick the value up and feed it to Pachube for graphing.
One thing thou - I'm not sure that graphing an abolsute value is what you want to do - I woudl have thought that just the delta for each 5 min period would be better as this would give you a rolling consumption figure... anyway there is enough skeleton there to get you going ......
Brett