Last active
May 17, 2018 21:09
-
-
Save alanb128/979c6b182116f05aa916dc2046f0af92 to your computer and use it in GitHub Desktop.
Basic Raspberry Pi water meter pulse counting and MQTT push to Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import paho.mqtt.client as mqtt | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
global pcount # variable for counted pulses | |
pcount = 0 | |
count1 = 0 | |
client = mqtt.Client() | |
client.connect(192.168.1.100, 1883, 60) #IP address of your Home Assistant (HA) | |
client.loop_start() | |
def reportstatus(channel): | |
# will execute on every rising pulse | |
global pcount | |
pcount += 1 | |
GPIO.add_event_detect(3, GPIO.RISING, callback=reportstatus, bouncetime=120) | |
while True: | |
# loop updates flow in HA every 3 seconds | |
count1 = pcount | |
count1 = count1 * 9.6 #convert pulses to ounces | |
pcount = 0 | |
sleep(3) | |
# push the flow data to HA using MQTT | |
client.publish(topicname, round(float(count1)/3,0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment