your script is not correct, so you have measuring faults.
i have the same NPN sensor, i have testet the sensor (measured it) it gives 5V on data pin (black wire) by contact,
so by detection gives the sensor a 1 not an 0, and by NPN OFF gives the sensor NOTHING (black data wire is not conneted to ground also not to VCC so it gives nothing)
your script set RPi PIN to 3.3V (Pullup) if your sensor gives NPN On, so it gives 5V to the RPis GPIO, so the pi see it, but thats not correct, the GPIO pin is still Pulled UP.
the next Thing is, the sensor gives soms an short "fault" Signal by shutting down NPN,
so i have edited your script
Code: Select all
#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)
as you see, im using GPIO 15 on my pi, and now the pin is PULLED DOWN by the pi, so it is 0 Volt on that pin.
then i edited that one:
Code: Select all
#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)
so GPIO is RISING to 5 Volts by NPN ON, and i set Bouncetime to 800 (now the short fault Signal from NPN sensor by shuting down NPN wil be ignored)
normaly the PIs GPIOs wil be acivated by 3,3 Volts, but 5V is also OK, it was a problem by the first Generation of the PI (the 700mhz one) the other PIs can also handle 5V on GPIO.
the script from you is working on most optical sensors, they gives GND on contact, so your script is good working with senosrs that shorts datapin with GND.
here is my complete script if someone will test with the NPN Sensor, i have no measuring faults, no liter to much or less.
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 = 1154651
#Domoticz URL
domoticz_url = "http://192.168.102.57:8080"
#Domoticz IDX van de water sensor (RFXMeter)
idx = 11
#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"
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"