Flow meter using Brewblox and Spark

Has anyone had success using flow meters with Spark and Brewblox? Looking forward adding two flow meters for each pump on my brewhouse and I would like to control the quantitative flow and measure flow quantity.

getting the data into brewblox from the flow meters is pretty easy. with rpi you can use the serial ports to read in data with the script in brewblox developer docs “reading serial port of pi” or something . I’ve been using some ADS1115 boards to read analog sensor data over i2c quite successfully.

my poorly written code:
https://github.com/hackrsackr/brewblox-meters

that said that just reads data in. if the actuator you want to control is controlled by brewblox, you’ll have to use node-red as the data is not actionable. hopefully it’s in the works for the next spark module.

Elco and Bob will have more insight.

1 Like

If you don’t mind me asking, what flow meters are you using @hackrsackr ?

I might give your code a go, looks great!

Interesting do you know if I can use connecting using 1-wire input to the Spark?

I’m not sure I get what you mean. You can use external values as mock temp sensor values, but this is generally somewhat hacky.

Any 0-5V analog output flow meter will work. I have these specific meters https://a.aliexpress.com/_m0NiExA

I got the ones with the display but it’s unnecessary and I don’t use them. Basically just wanted to verify the data. These don’t have the display and are cheaper.
https://a.aliexpress.com/_mOC0Kl2

I only use these for water. I have used them for wort, but there is a plastic propeller in there, and grain bits and wort residue can negatively affect them over batches. Also, the temp rating is like 80c I think. For those reasons I only use them on my incoming water, and on my mash underlet pipe to measure strike water. The meters work best for slowly changing flow rates as quick surges can overwhelm the propeller. I don’t use these for volume measurement in the kettles though. I measure that with pressure sensors on the bottom of the kettles.

1 Like

Not currently. My setup is:

Flow meter → ads1115 → rpi → spark

The rpi → spark bit is documented in the dev docs very well here: https://www.brewblox.com/dev/tutorials/pubscript/#source-code

Rpi reads output of ads1115, RPi communicates with brewblox over Mqtt.

1 Like

Are you trying to do something similar to this?

&

Cause this is very similar hardware / setup from the looks of things. I am just using it to help gauge water Into the system

Yes i’d like to read, control quantity and speed of the flow of my brewing pumps, so i was researching a way to have this feature on Brewblox.

Did that help answer your question? I currently have mine wired… but im trying to get the script to work where it recognizes the flow, from the propeller, been sporadic and still testing.

give this a shot.

pip3 install paho-mqtt if you need it.

edit: terminal output should look like this
{“key”: “flowMeter”, “data”: {“flow[l/min]”: 0.0}}
if it does; it should be available in metrics and graphs.

"""
Code example for publishing data to the Brewblox eventbus

Dependencies:
- paho-mqtt
"""

import json
import RPi.GPIO as GPIO
import time, sys

from paho.mqtt import client as mqtt


# 172.17.0.1 is the default IP address for the host running the Docker container
# Change this value if Brewblox is installed on a different computer
HOST = '172.17.0.1'

# 80 is the default port for HTTP, but this can be changed in brewblox env settings.
PORT = 80

# This is a constant value. You never need to change it.
HISTORY_TOPIC = 'brewcast/history'

TOPIC = HISTORY_TOPIC + '/flowMeter'

# Create a websocket MQTT client
client = mqtt.Client(transport='websockets')
client.ws_set_options(path='/eventbus')

FLOW_SENSOR_GPIO = 13

GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR_GPIO, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
global count
count = 0
 
def countPulse(channel):
   global count
   if start_counter == 1:
      count = count+1
 
GPIO.add_event_detect(FLOW_SENSOR_GPIO, GPIO.FALLING, callback=countPulse)

try:
    client.connect_async(host=HOST, port=PORT)
    client.loop_start()

    while True:
        start_counter = 1
        time.sleep(1)
        start_counter = 0
       
        # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
        Q = 8       
        flow = (count / Q) 
        
        # MQTT message to send to brewblox
        message = {
            'key': 'flowMeter',
            'data': {'flow[l/min]': flow}
        }        

        print(json.dumps(message))

        # Publish message
        client.publish(TOPIC, json.dumps(message))
        
        count = 0
        time.sleep(5)

finally:
    client.loop_stop()
    GPIO.cleanup()
    sys.exit()
1 Like

Not yet I am still transitioning my brew setup to Spark 4 and just arrived a new flow meter I am going to test with digiten, I’ll give updates of my findings with Spark :+1:

Doesn’t this run into issues with variable declaration for start_counter?

Oh maybe I was just putting that code that he had posted before into the pub script. Maybe that’s the cause of the sporadic readings.

This solves it I think. I wrapped it in a class to handle variable scoping.

"""
Code example for publishing data to the Brewblox eventbus

Dependencies:
- paho-mqtt
"""

import json
import time

import RPi.GPIO as GPIO
from paho.mqtt import client as mqtt

# 172.17.0.1 is the default IP address for the host running the Docker container
# Change this value if Brewblox is installed on a different computer
HOST = '172.17.0.1'

# 80 is the default port for HTTP, but this can be changed in brewblox env settings.
PORT = 80

# This is a constant value. You never need to change it.
HISTORY_TOPIC = 'brewcast/history'
TOPIC = HISTORY_TOPIC + '/flowMeter'

FLOW_SENSOR_GPIO = 13


class FlowMeter:
    def __init__(self) -> None:
        # Create a websocket MQTT client
        self.client = mqtt.Client(transport='websockets')
        self.client.ws_set_options(path='/eventbus')

        self.collecting = False
        self.count = 0

        # GPIO setup
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(FLOW_SENSOR_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(FLOW_SENSOR_GPIO, GPIO.FALLING, callback=self.on_pulse)

    def on_pulse(self, channel):
        if self.collecting:
            self.count += 1

    def run(self):
        try:
            self.client.connect_async(host=HOST, port=PORT)
            self.client.loop_start()

            while True:
                self.count = 0
                self.collecting = True
                time.sleep(1)
                self.collecting = False

                # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
                Q = 8
                flow = (self.count / Q)

                # MQTT message to send to brewblox
                message = {
                    'key': 'flowMeter',
                    'data': {'flow[l/min]': flow}
                }

                print(json.dumps(message))

                # Publish message
                self.client.publish(TOPIC, json.dumps(message))
                time.sleep(5)

        finally:
            self.client.loop_stop()
            GPIO.cleanup()


if __name__ == '__main__':
    FlowMeter().run()

1 Like