Page 2 of 2

Re: GPIO Interrupt for water meter

Posted: Monday 21 March 2016 20:45
by JelleB
RidingTheFlow,

I faked the value of the IDX to the actual meter reading.
Flush the toilet, count 7 pulses on the TCTR5000 sensor, yet the value is updated to value +14. What could cause this or how can I fix it?

Fully agree your script is very robust, but in Domoticz strange things happen it seems.

Re: GPIO Interrupt for water meter

Posted: Monday 21 March 2016 20:45
by JelleB
RidingTheFlow,

I faked the value of the IDX to the actual meter reading.
Flush the toilet, count 7 pulses on the TCTR5000 sensor, yet the value is updated to value +14. What could cause this or how can I fix it?

Fully agree your script is very robust, but in Domoticz strange things happen it seems.

Re: GPIO Interrupt for water meter

Posted: Monday 21 March 2016 21:05
by JelleB
RidingTheFlow,

I faked the value of the IDX to the actual meter reading.
Flush the toilet, count 7 pulses on the TCTR5000 sensor, yet the value is updated to value +14. What could cause this or how can I fix it?
Fully agree your script is very robust, but in my case 1 liter is counted as 2 ticks and the delta becomes 2. Should I just divide the delta by 2? or subtract 1?

I increased bouncetime to 2000 but that does not help. Should it be longer or does not help in this case?

Code: Select all

Water delta 0
Water delta 0
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water counter tick: 1
Water counter tick: 2
Water delta 2
Water delta 0
fixed:
Changed

Code: Select all

def gpio_intr(pin):
    if pin == WATER_GPIO:
        global WATER_DELTA
        with WATER_COUNTER_LOCK:
            WATER_DELTA = 1
instead of WATER_DELTA +=1 and now the increment never becomes more than 1 liter per tick. Let see how this behaves....

Re: GPIO Interrupt for water meter

Posted: Tuesday 22 March 2016 2:24
by RidingTheFlow
Its not a script problem, the problem that your PI getting two signals for each meter increment. Looks like false edges, I had same issues with my sensors until I did some tweaking.

How have you connected the sensor (I mean schematics)? Did you use shielded cable/what pullup resistor, etc? How long is the cable?
Could it that be that metal detector actually sending pulse twice per counter increment?

If you don't do it properly on hardware side you most likely will suffer from EM interference which will cause false interrupts. Raspberry GPIO not really suited for long cables without some tweaking.
JelleB wrote:fixed:
Changed

Code: Select all

def gpio_intr(pin):
    if pin == WATER_GPIO:
        global WATER_DELTA
        with WATER_COUNTER_LOCK:
            WATER_DELTA = 1
instead of WATER_DELTA +=1 and now the increment never becomes more than 1 liter per tick. Let see how this behaves....
Sorry to say it, but you didn't really "fix"it. Now instead of too many ticks you will get too little sometimes. It will lose ticks if interrupt happens more than once in update cycle. You need to fix noise on your interrupts or whatever other way to make sensor actually signal it once per watermeter increment...

Re: GPIO Interrupt for water meter

Posted: Tuesday 22 March 2016 20:37
by JelleB
Well, maybe not fixed, but it works. I'd rather have a few liters too many or too sort than than the usage get doubled.

E.g. when I open several taps in the house, on the terminal I see several ticks in a row, but count visually only 1 revolution (liter) of the meter and 1 sensor on/off.

Code: Select all

Water counter tick: 1
Water counter tick: 1
Water delta 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water delta 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water delta 1
Water counter tick: 1
and

Code: Select all

Water delta 0
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water counter tick: 1
Water delta 1
I have updated the Domoticz total usage by json to reflect the actual meter reading and will track the differences the next couple of days.

Re: GPIO Interrupt for water meter

Posted: Tuesday 22 March 2016 21:01
by RidingTheFlow
If so you better just divide usage by 2 and convert to int when you update, but keep increment in interrupt - that will make more or less correct usage without assuming that meter can't blink faster then update interval.

Though myself I found that errors accumulate pretty quick and had to really fix the false triggers. I've used 2.2K pull up resistor on cable and gpiozero library to great result (I will post updated script with gpiozero soon after I test it for a bit).

Re: GPIO Interrupt for water meter

Posted: Friday 25 March 2016 15:09
by JelleB
How would the divide by 2 and INT value look like?

Code: Select all

WATER_DELTA_POST = INT(WATER_DELTA / 2)
?

I haven't used shielded cable, cable is some 2.5 m long and runs next to fuse box etc, so EM interference is very well possible.

Have the TCTR5000 connected directly to GPIO. The sensor has a potmeter to adjust.

Re: GPIO Interrupt for water meter

Posted: Friday 25 March 2016 18:01
by RidingTheFlow
JelleB wrote:How would the divide by 2 and INT value look like?
Lowercase int

Code: Select all

WATER_DELTA_POST = int(WATER_DELTA / 2)
I haven't used shielded cable, cable is some 2.5 m long and runs next to fuse box etc, so EM interference is very well possible.

Have the TCTR5000 connected directly to GPIO. The sensor has a potmeter to adjust.
Potmeter adjust sensitivity of sensor, it's unlikely to help with signal robustness. Though it may help you to fine tune pulse length (so its not too short but no false pulses). I recommend doing this with simple script which just prints GPIO value in infinite loop, you turn meter and tune pot for good pulse length.

See if with divide by two you get numbers which will diverge to much. If they eventually will, you'll have to fight interference.
One thing I definitely would recommend is to add a proper pull-up resistor at the start of the cable (near PI) between 3.3V and sensor signal line - about 2.2K
Raspberry Pi internal pullup is about 60K and will do no help with such long cable.

Re: GPIO Interrupt for water meter

Posted: Sunday 27 March 2016 14:54
by FlyinGNL
i got this error after a new tick , dummy counter is not updating! pls help needed

Water counter tick: 1
HTTP Error 401: Unauthorized
Water delta 1

this is the script:

#!/usr/bin/env python

#imports
import time
import RPi.GPIO as GPIO
import json
import urllib2
import threading

#vars
GET_URL = 'http://localhost:8080/json.htm?type=devices&rid=%d'
SET_URL = 'http://localhost:8080/json.htm?type=com ... &svalue=%d'
WATER_DELTA = 0
WATER_IDX = 221
WATER_GPIO = 27
WATER_COUNTER_LOCK = threading.Lock()

def gpio_intr(pin):
if pin == WATER_GPIO:
global WATER_DELTA
with WATER_COUNTER_LOCK:
WATER_DELTA = 1
print 'Water counter tick: %d' % WATER_DELTA

def main():
global WATER_DELTA

GPIO.setmode(GPIO.BCM)
GPIO.setup(WATER_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(WATER_GPIO, GPIO.RISING, callback=gpio_intr, bouncetime=100)

while True:
time.sleep(5)

WATER_DELTA_POST = WATER_DELTA

if WATER_DELTA_POST != 0:
try:
res = json.load(urllib2.urlopen(SET_URL % (WATER_IDX, WATER_DELTA_POST)))
if res['status'] != 'OK':
raise Exception('Domoticz json error')
with WATER_COUNTER_LOCK:
WATER_DELTA -= WATER_DELTA_POST
except Exception as e:
print e

print 'Water delta %d ' % (WATER_DELTA_POST)

if __name__=="__main__":
main()


EDIT: Solved....domoticz PW protected

Re: GPIO Interrupt for water meter

Posted: Monday 28 March 2016 19:31
by RidingTheFlow
You need to add localhost to "local networks" in settings or add password in script.