Script to read gas pulses from a CC dev board

14 replies [Last post]
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011

 

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.

 

brett
Offline
Providence, United States
Joined: 9 Jan 2010
One tweak

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

AttachmentSize
gasApplet.lua 1.3 KB
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Cool! Thanks

Hey Brett, Thanks for this. I'm not sure that I quite understand BSC endpoints yet but I'm sure that your script will help with that. I confess that the file input/output stuff was just laziness! I picked the routine up from someone else's script and as it worked OK, in the spirit of "if it ain't broke don't fix it" I just used it as it stood. :-p

I'll give the script a run and see how it goes.

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
I said I didn't understand it :-(

Having run the script I get the following error:

 

Error Message: A xAP message received was malformed. The UID FF00D801 is setup as a subaddress but the Source address is not: dbzoo.livebox.gas
Received From: 192.168.0.2:3078

 

Now I'm in trouble!

brett
Offline
Providence, United States
Joined: 9 Jan 2010
An easy one.

Easy fix make the source address be something like "dbzoo.livebox.gas:sensor"

Brett

g7pkf
Offline
United Kingdom
Joined: 11 Jan 2011
Another fine use

For a CC digital development board :)

tickett
Offline
Joined: 19 Nov 2011
Allan, thanks for posting the

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Thoughts

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

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Thoughts

Just a couple of thoughts on this. I searched the internet for methods of monitoring gas consumption and although I found a couple of commercial (and therefore quite expensive) methods of doing it, there seems to be a dearth of readily available methods out there.

Basically my solution was dictated by what I had available lying around. Hence an old modem cable and a CC dev board. I'm not claiming any elegance or sophistication but it does work, and it works now. (and with a little help from Brett it probably works better :-)

If this forum helps to create an elegant and sophisticated solution as a result then I'm sure that there will be much interest. I'm sure that room nodes are probably getting closer to that but if you want a "working  now" solution then many people seem to already use Current Cost and the dev boards are relatively cheap. You do, of course, have to be lucky enough to have a reed switch and RJ11 socket on your meter.

Glad to be able to at least start development route.

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Thanks for the script

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.

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
Hi Mark,Would you be able to

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

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Hi Karl glad to help

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

AttachmentSize
gasmonitor.jpg 49.75 KB
BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
Almost there....

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

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Sorry about that Karl I

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.

AttachmentSize
gas_interface.jpg 27.75 KB
BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
That's exactly what I needed,

That's exactly what I needed, Thanks.

Hardware Info