MoonChin
Posts: 10
Joined: Sun Jun 14, 2020 8:11 am
Location: South Australia

Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 10:54 am

I have a 2-wire flowmeter that changes output between open and closed every 10 L(itres) of water volume that goes through. E.g. 0-10L open circuit, 10-20L closed circuit, 20-30L open circuit...
Therefore I have it connected to a GPIO and have enabled the internal pull-up resistor.

The flowmeter is being used to monitor and log household usage and is connected to the main line into the house. I have written some code which attaches an interrupt to the GPIO the flowmeter is connected to, and every pulse I detect will add the current time and the pulse volume (10L) into a list of objects that each contain a datetime and volume variable. I keep this list trimmed to x samples (set to 5 at the moment).

So for example, the list might contain (pretend the object is simply its own list and that the date part of the date is included but excluded here for ease of notation):
[10:00:00, 10]
[10:03:00, 10]
[10:04:30, 10]
[10:06:15, 10]
[10:08:00, 10]

Calculating the flowrate is easy, just sum the volumes, get the difference in max and min dates in minutes, and divide:
40L/8min = 5 L/min (exclude the first volume as this applied to the previous period)

Where I'm stuck is for this scenario:
[10:00:00, 10]
[10:03:00, 10]
[10:04:30, 10]
[10:06:15, 10]
[11:08:00, 10]

The same method works, but it's not what you expect, as it's no longer an instantaneous flowrate, but an average-over-longer-time flowrate.

And this is my issue; how do I calculate the flowrate as you would expect to see it? and when the flow stops and the interrupts therefore stop, how do I know when to drop the pulses from the list and update the flowrate?
Last edited by MoonChin on Sun Jun 14, 2020 11:08 pm, edited 2 times in total.
When life gives you lemons, collect the seeds and plant them for more lemons :)

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 12:49 pm

You probably want to take a measurement every (say) 10 minutes, counting the pulses that have occurred. If none, we can see the flow rate in last 10 minutes was zero.

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 2:12 pm

Trouble is, with the data you've got (signal every 10l), you can't get real-time flow. The nearest you can get is the average flow over the period of the last 10l. That is never going to look pretty -- imagine you wash the car, using 10l every few minutes, stopping with 9l of the 10l interval gone. Then use no water for several hours. You cannot distinguish that from a drip feed irrigation using water steadily over the whole period.

If you want something closer to conventional real-time data, you need either an actual volume total reading every few (five?) minutes, or a signal on a much lower volume (maybe 0.1l).

EDIT: no pulse for a period doesn't mean zero flow, just that the flow is insufficient to trigger the next signal, that is, less than 10l.
Signature retired

jbudd
Posts: 1409
Joined: Mon Dec 16, 2013 10:23 am

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 2:40 pm

Presumably the device is counting more frequent impulses than one every 10 litres.
The only one I've seen, in my coffee machine, is a tiny turbine with a magnet and a hall effect sensor.
Is there any chance of getting inside yours and intercepting the signals directly from it's sensor?

MoonChin
Posts: 10
Joined: Sun Jun 14, 2020 8:11 am
Location: South Australia

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 10:38 pm

jbudd wrote:
Sun Jun 14, 2020 2:40 pm
Presumably the device is counting more frequent impulses than one every 10 litres.
The only one I've seen, in my coffee machine, is a tiny turbine with a magnet and a hall effect sensor.
Is there any chance of getting inside yours and intercepting the signals directly from it's sensor?
I have a hall effect sensor flowmeter as well, a cheap chinese one, that counts every 2.25mL with a 3-wire cable: power +/- to power the hall effect sensor, and signal. However I certainly wouldn't trust one of the cheaply made flowmeters at constant mains pressure.

The quality flowmeter I'm using only has two wires and the signal I get flip-flops between open circuit and closed circuit each pulse, so I can only assume that it's geared and has a physical switch inside that is operated. e.g. for 0-10L the switch is open, 10-20L it's closed, 20-30L it's open...
And just like a physical switch, I noticed that I needed to debounce it, as I was seeing multiple triggers occurring within microseconds of each other.

https://www.hunterindustries.com/en-met ... flow-meter
Last edited by MoonChin on Sun Jun 14, 2020 11:04 pm, edited 2 times in total.
When life gives you lemons, collect the seeds and plant them for more lemons :)

MoonChin
Posts: 10
Joined: Sun Jun 14, 2020 8:11 am
Location: South Australia

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 10:41 pm

davidcoton wrote:
Sun Jun 14, 2020 2:12 pm
Trouble is, with the data you've got (signal every 10l), you can't get real-time flow. The nearest you can get is the average flow over the period of the last 10l. That is never going to look pretty -- imagine you wash the car, using 10l every few minutes, stopping with 9l of the 10l interval gone. Then use no water for several hours. You cannot distinguish that from a drip feed irrigation using water steadily over the whole period.

If you want something closer to conventional real-time data, you need either an actual volume total reading every few (five?) minutes, or a signal on a much lower volume (maybe 0.1l).

EDIT: no pulse for a period doesn't mean zero flow, just that the flow is insufficient to trigger the next signal, that is, less than 10l.
This is all true, I'm not sure why this flowmeter only pulses 10L, however it is meant for irrigation systems and not really for short start/stop water usage.
However the good news is, I found that I can actually use the smaller 20mm sensor (max flowrate 80LPM, I measured ours at 69LPM) which has a much lower resolution of 1L per pulse :) I can work with that!
When life gives you lemons, collect the seeds and plant them for more lemons :)

MoonChin
Posts: 10
Joined: Sun Jun 14, 2020 8:11 am
Location: South Australia

Re: Flow meter - Calculating average (instantaneous) flowrate

Sun Jun 14, 2020 11:15 pm

neilgl wrote:
Sun Jun 14, 2020 12:49 pm
You probably want to take a measurement every (say) 10 minutes, counting the pulses that have occurred. If none, we can see the flow rate in last 10 minutes was zero.
Hmm, interesting suggestion. Instead of discarding every X samples (which now that I think about it is probably the cause of my issues..) I could discard samples after time. Instead of a fixed period, I'm thinking it would be better to use something like the last delta time multiplied by 3? Or the average of the last 2 delta times by 3? Maybe with a fallback of 10 mins.

Now to figure out how to best achieve this...
Thinking out loud, I'll see if I can setup a timer thread (terminology??) to countdown by this time, the average time delta between samples multiplied by 3, and then check if there were any samples added to the list in the meantime and if not, set the flowrate to 0. If there were samples added, do nothing.
I'll start the timer thread on every pulse detected, so technically there could be multiple of these running. Is there any way to kill a thread? t.stop()? I guess i'll need to keep a global list of the thread objects as well

Thread code example I found on here that I would use:

Code: Select all

import threading

def checkFlowrate():
    # do something
    
t = threading.Timer(5.0, checkFlowrate)
t.start()
Or maybe better to use something like this:
https://stackoverflow.com/a/12435256/1733467

Code: Select all

class MyThread(Thread):
    def __init__(self, event):
        Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(0.5):
            print("my thread")
            # call a function

##In the code that started the timer, you can then set the stopped event to stop the timer.

stopFlag = Event()
thread = MyThread(stopFlag)
thread.start()
# this will stop the timer
stopFlag.set()
When life gives you lemons, collect the seeds and plant them for more lemons :)

MoonChin
Posts: 10
Joined: Sun Jun 14, 2020 8:11 am
Location: South Australia

Re: Flow meter - Calculating average (instantaneous) flowrate

Tue Jun 16, 2020 11:28 pm

FWIW, I've attached my code if it's useful to anyone. It has comments, but could do with some more + function headers. All in good time..
It includes my super basic web server as well which serves up the current flowrate, flow total, and 5 entries from the pulse history.


(Feel free to let me know if there are things I could improve; syntax, semantics, or otherwise. I'm not a software engineer)
Attachments
flowmeter_webserver_v1_4.zip
(5.91 KiB) Downloaded 6 times
When life gives you lemons, collect the seeds and plant them for more lemons :)

Return to “Beginners”