I've the LJ18A3-8-Z/BX-5V NPN sensor for counting water consumption of my Sensus 620 meter.
The sensor seems to be working perfect. When the metal part of the rotating wheel is being detected, the LED is on and v.v.
I'm using the script of @Micha123
Code: Select all
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import urllib2
import os
#Watermeter stand (wordt alleen initeel gebruikt als er geen bestand meterstand.txt is)
global Counter
Counter = 00000
#Domoticz URL
domoticz_url = "http://192.168.x.xx:8080"
#Domoticz IDX van de water sensor (RFXMeter)
idx = 602
#Open meterstand.txt file en lees meterstand
#Als meterstand.txt niet aanwezig is maakt script bestand aan en vult de meterstand
#fn = "/home/pi/domoticz/scripts/meterstand.txt"
fn = "/home/pi/meterstand.txt"
if os.path.exists(fn):
f = file(fn, "r+")
f = open(fn)
inhoud = f.readline()
a,b,c = inhoud.split()
Counter = int(c)
else:
f = open(fn, "w")
f.write( 'meterstand = ' + repr(Counter))
f.close()
#Board is pin nr, BMC is GPIO nr
#Read output from water meter op pin 10
GPIO.setmode(GPIO.BOARD)
# Set GPIO 15 (Pin 10) als Input aditioneel als Pulldown-Weerstand aktiveren
GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
#Functie callback
#Dit is de functie die aangeroepen wordt in de interrupt
def Interrupt(channel):
#Teller elke interupt uitlezen en met 1 liter verhogen
f = file(fn, "r+")
f = open(fn)
inhoud = f.readline()
a,b,c = inhoud.split()
Counter = int(c)
Counter = Counter + 1
f.close()
#Schrijf meterstand naar bestand
f = open( fn, 'w')
f.write( 'meterstand = ' + repr(Counter))
f.close()
#Send counter to domoticz JSON
url1 = domoticz_url+'/json.htm?type=command¶m=udevice&idx='+str(idx)+'&svalue='+str(Counter)
req1 = urllib2.Request(url1)
response1 = urllib2.urlopen(req1)
#Voor debug => print voorbeeld van de JSON aanroep en/of de counter
print "JSON call = "+ str(url1)
print "Watermeter Counter = " + str(Counter)
#Interrupt-Event toevoegen, bij een NPN off geeft sensor een 0 en en bij detectie een 1
#Bij detectie (LED on) een 1 daarom check dalende interrupt.
GPIO.add_event_detect(10, GPIO.RISING, callback = Interrupt, bouncetime = 800)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print "\nBye"
The GPIO port is configured/used for detecting on rising edges, but the falling edges are also resulting in a increment of the counter.
I've no idea why. Have to say that I'm not an GPIO guru....
I found one message on this topic about counting 0.5l, so I'm not the only one.
Can someone explain this?
Based on the idea/code of @WillyWong1 I made some changes to get the correct amount of water consumption (instead of twice the amount)
Code: Select all
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import urllib2
import os
#Watermeter stand (wordt alleen initeel gebruikt als er geen bestand meterstand.txt is)
global Counter
global TempCounter
Counter = 00000
TempCounter = 0
#Domoticz URL
domoticz_url = "http://192.168.x.xx:8080"
#Domoticz IDX van de water sensor (RFXMeter)
idx = 602
#Open meterstand.txt file en lees meterstand
#Als meterstand.txt niet aanwezig is maakt script bestand aan en vult de meterstand
fn = "/home/pi/meterstand.txt"
if os.path.exists(fn):
f = file(fn, "r+")
f = open(fn)
inhoud = f.readline()
a,b,c = inhoud.split()
Counter = int(c)
else:
f = open(fn, "w")
f.write( 'meterstand = ' + repr(Counter))
f.close()
#Board is pin nr, BMC is GPIO nr
#Read output from water meter op pin 10
GPIO.setmode(GPIO.BOARD)
# Set GPIO 15 (Pin 10) als Input aditioneel als Pulldown-Weerstand aktiveren
GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
#Functie callback
#Dit is de functie die aangeroepen wordt in de interrupt
def Interrupt(channel):
#Teller elke interupt uitlezen en met 1 liter verhogen
global TempCounter
if TempCounter == 0:
TempCounter = 1
else:
TempCounter = 0
f = file(fn, "r+")
f = open(fn)
inhoud = f.readline()
a,b,c = inhoud.split()
Counter = int(c)
Counter = Counter + 1
f.close()
#Schrijf meterstand naar bestand
f = open( fn, 'w')
f.write( 'meterstand = ' + repr(Counter))
f.close()
#Send counter to domoticz JSON
url1 = domoticz_url+'/json.htm?type=command¶m=udevice&idx='+str(idx)+'&svalue='+str(Counter)
req1 = urllib2.Request(url1)
response1 = urllib2.urlopen(req1)
#Voor debug => print voorbeeld van de JSON aanroep en/of de counter
#print "JSON call = "+ str(url1)
#print "Watermeter Counter = " + str(Counter)
#Interrupt-Event toevoegen, bij een NPN off geeft sensor een 0 en en bij detectie een 1
#Bij detectie (LED on) een 1 daarom check dalende interrupt.
GPIO.add_event_detect(10, GPIO.RISING, callback = Interrupt, bouncetime = 800)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print "\nBye"