All the examples I have found show how to publish things like switch on / off status - and being new to mqtt I am struggling to adapt the examples to my use case - can someone help and/or point me in the right direction please.
I have found some code online on a raspberry pi which is taking a measurement from an attached digital flow sensor (each time the impeller in the flow meter rotates i get an on / off digital signal and the code basically counts the number of rotations, expresses this in Litres per minute using the term “flow”). This works well and displays the flow value every 5 seconds on the screen.
I would like to publish the flow value to cayenne using mqtt. I can manage the cayenne dashboard end as the instructions there are clear - its the code that publishes the value I cannot fathom.
Thanks
The code is as follows:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time, sys
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)
while True:
try:
start_counter = 1
time.sleep(1)
start_counter = 0
flow = (count / 7.5) # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
print(“The flow is: %.3f Litre/min” % (flow))
count = 0
time.sleep(5)
except KeyboardInterrupt:
print('\nkeyboard interrupt!')
GPIO.cleanup()
sys.exit()