Page 1 of 2

Watermeter through GPIO and TCRT5000

Posted: Saturday 17 January 2015 20:31
by Lieuwke
I have connected my Smartmeter and come to the conclusion i am missing another meter in my metercabinet. Water!
I have read a thread on this forum http://www.domoticz.com/forum/viewtopic ... 1&start=20 about the gasmeter reading. I want to try this module: http://www.dx.com/p/tcrt5000-black-and- ... car-140732 and connect it to GPIO
I have ordered it to give it a try. It can't hurt to try since it only cost a couple of bucks.
Watermeter1.jpg
Watermeter1.jpg (218.73 KiB) Viewed 13543 times
I haven't got a very nice watermeter for this purpose, so i am hoping to detect the rotation of one of the red dials. If it doesn't work, well, bad luck.
It will take some time for the hardware to arrive, but i'll keep you posted about any progress that make (or any failures for that matter)

Re: Watermeter through GPIO and TCRT5000

Posted: Saturday 17 January 2015 21:51
by ThinkPad
If you have a smartphone look for a compass/magnetic field app and let the water flow somewhere in your house. Then hold your phone near your meter, and see if you can find a pulsating signal. Maybe the meter has a little magnet in it. Detecting that field is a lot easier than correctly aligning the TCRT5000 module i think.

Re: Watermeter through GPIO and TCRT5000

Posted: Wednesday 21 January 2015 9:55
by BigDog
have you got it working?
i have the same watermeter :)

greetz Bob

Re: Watermeter through GPIO and TCRT5000

Posted: Tuesday 17 February 2015 11:27
by jossie67
Thinkpad and Bob,

Any news about the water meter

Greetz Jos

Re: Watermeter through GPIO and TCRT5000

Posted: Wednesday 18 February 2015 15:34
by D'rMorris
I've been fiddling around with a water meter as well and this is how I did it:

I've bought the following items:
1. A small pcb (sku213567) to which I can hook multiple TCRT5000's. This PCB has 4 separate signal outputs so hook up to 4 GPIO inputs whereas you only need 1 VCC and 1 ground. I do not own a Piface (yet) but I wanted to figure out how everything works.
I know that measuring is much easier via the piface ;).
Image

2. Multiple TCRT5000's (sku171350):
Image

you can also buy these (sku213949):
Image

The difference between these two is that the latter is normally closed (so contact shows closed when light not reflected) whereas the first one is normally open (so contact shows open when light not reflected). Kinda depends what you want to do with them.

Once I had the GPIO working, it was time to write a script that counts the usage. It really depends on your water meter what the formula in the script should be. The water meter in the screenshot above has several dials and one of those spins 1 time per liter (the one on the top right has scale x 0.1, so 1 revolution per liter).

First I set the water meter divider in the setup to 1000. Then, I wrote this script (there are a lot of print statements, but this shows what's really going on).
What the script does is:
- Retrieve the svalue from the water meter
- In the event of a pulse from the TCRT5000 via the GPIO port, add 1 (liter) to the svalue
- Write the new svalue to the device

I hope it's useful for somebody :).

Code: Select all

commandArray = {}

print('> Monitoring water consumption')

-- Water usage
-- Retrieve value from water meter device:
sWaterUsage = otherdevices_svalues['Water']

-- To have a better readable format, divide number by 1000:
sWaterUsagePrint = tonumber(sWaterUsage / 1000);

-- calculation is done with the unmodified water value
sWaterUsage = tonumber(sWaterUsage);

print(">> Utilities: Water usage until now is " .. sWaterUsagePrint .. " ");

if (devicechanged['GPIO_WATER'] == 'Close')
then
	sWaterUsageTot = (sWaterUsage + 1)
	
	print(">> Utilities: Water usage is now " .. sWaterUsageTot .. " ");
	commandArray['OpenURL']='http://IPADDRESS:PORT/json.htm?type=command&param=udevice&idx=164&svalue='..sWaterUsageTot..''
end

return commandArray

Re: Watermeter through GPIO and TCRT5000

Posted: Tuesday 10 March 2015 13:19
by jossie67
Hi D'rMorris,

Thanks for sharing your script, I think it a python script?
And what is you GPIO_water on the Pi I want to use GPIO17

Re: Watermeter through GPIO and TCRT5000

Posted: Tuesday 10 March 2015 20:34
by sincze
jossie67 wrote:Hi D'rMorris,

Thanks for sharing your script, I think it a python script?
And what is you GPIO_water on the Pi I want to use GPIO17
Ha, nice solution. Running WIringPi will result in a lot of false interrupts for the tcrt-5000 which have to be compensated.

Re: Watermeter through GPIO and TCRT5000

Posted: Wednesday 11 March 2015 11:49
by D'rMorris
sincze wrote:
jossie67 wrote:Hi D'rMorris,

Thanks for sharing your script, I think it a python script?
And what is you GPIO_water on the Pi I want to use GPIO17
Ha, nice solution. Running WIringPi will result in a lot of false interrupts for the tcrt-5000 which have to be compensated.
Depends on the frequency of the interrupts...for a gas meter (which has slow revolutions) it is OK. This was (for me at least) just a small project to check if I could get it working.
We're currently looking for a new house, which will have a smartmeter in the future anyway. So, for me it's just for fiddling around a bit. The interrupt handling is also an issue in the current version of the Pi firmware I think. If I have too many interrupts, it will skip some. Or maybe I configured it incorrect :).

@ Jossie: No, it's not python, it's a LUA script and my GPIO_water is 18.

Re: Watermeter through GPIO and TCRT5000

Posted: Sunday 26 April 2015 17:42
by jvandenbroek
Thanks D'rMorris, using your script successfully for a gas meter! Only moved the devicechanged rule to the top, otherwise it would constantly print the calculations, not just on an interrupt. :lol:

[edit]Well, not that successfully it seems. As stated above, due to the many false interrupts, Domoticz many times fails to catch the correct off (low) interrupt, but instead I get many on interrupts. Maybe putting a large cap on both ends of the line will help a bit, will try that later and post my findings here.[/edit]
[edit2]Tadaa, adding a large 1000uF elco at the TCRT5000 side (D0-GND) fixed the false interrupts :)[/edit2]

Re: Watermeter through GPIO and TCRT5000

Posted: Friday 01 May 2015 21:28
by sincze
jvandenbroek wrote:Thanks D'rMorris, using your script successfully for a gas meter! Only moved the devicechanged rule to the top, otherwise it would constantly print the calculations, not just on an interrupt. :lol:

[edit2]Tadaa, adding a large 1000uF elco at the TCRT5000 side (D0-GND) fixed the false interrupts :)[/edit2]

Just curious, can you send picture of total setup please?
I solved false interrupts by not using them at all, only polling the pin value :D

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 15:00
by D'rMorris
jvandenbroek wrote:Thanks D'rMorris, using your script successfully for a gas meter! Only moved the devicechanged rule to the top, otherwise it would constantly print the calculations, not just on an interrupt. :lol:

[edit]Well, not that successfully it seems. As stated above, due to the many false interrupts, Domoticz many times fails to catch the correct off (low) interrupt, but instead I get many on interrupts. Maybe putting a large cap on both ends of the line will help a bit, will try that later and post my findings here.[/edit]
[edit2]Tadaa, adding a large 1000uF elco at the TCRT5000 side (D0-GND) fixed the false interrupts :)[/edit2]
Haha, yeah that's right. I put it there because for debugging I wanted the pin values to print constantly.

Hey that's cool! I'm just learning how to work with condensators and stuff. Could you post a picture of the setup with the elco?
I have to put it serial to the D0 and GND? Or parallel?

@ Sincze: how do you "poll" the pin value?

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 20:18
by sincze
D'rMorris wrote: @ Sincze: how do you "poll" the pin value?
I use wiringPi library

Code: Select all

// What GPIO input are we using?
//      This is a wiringPi pin number
#define IN_PIN                          1
A nice function would be digitalRead in a loop.
int pin_value = digitalRead(IN_PIN);

Please find the script in the zip file. It is based on the interrupt script however... not using interrupts at all. :D due to the amount of fake onces.
It is not a script it is .c 8-)

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 20:28
by D'rMorris
Ah yes, the famous isrcounter :), found that one as well (I use wiringpi as well). Never got around to test it. My issue is that when there are too much interrupts, the gpio does not capture them. Something in the logs with interrupt in queue, skipping interrupt or something. Does the isrcounter fix this as well?

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 20:37
by sincze
D'rMorris wrote:Ah yes, the famous isrcounter :), found that one as well (I use wiringpi as well). Never got around to test it. My issue is that when there are too much interrupts, the gpio does not capture them. Something in the logs with interrupt in queue, skipping interrupt or something. Does the isrcounter fix this as well?
For TCRT-5000 position it wiseley just on the glass.
Facing straight down.
Adjust the screw it will give you at least a solid valid on / off (this doesn't prevent jumping of the interrupt, that will happen eventually)

Using the script attached it will poll the pin value in stead of the interrupt catcher.
For water it should be 1 on and 1 off. No such thing will happen < 1 sec. I tried it by filling up the bath tub.

In a case where you watermeter stops right at the edge of on/off the script will compensate a lot of fluctuations for you. (stable pin).

It is my best effort without any additional hardware attached to the tcrt-5000. And has been running nicely for over 58 days now.

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 20:40
by D'rMorris
sincze wrote:
D'rMorris wrote:Ah yes, the famous isrcounter :), found that one as well (I use wiringpi as well). Never got around to test it. My issue is that when there are too much interrupts, the gpio does not capture them. Something in the logs with interrupt in queue, skipping interrupt or something. Does the isrcounter fix this as well?
For TCRT-5000 position it wiseley just on the glass.
Facing straight down.
Adjust the screw it will give you at least a solid valid on / off (this doesn't prevent jumping of the interrupt, that will happen eventually)

Using the script attached it will poll the pin value in stead of the interrupt catcher.
For water it should be 1 on and 1 off. No such thing will happen < 1 sec. I tried it by filling up the bath tub.

In a case where you watermeter stops right at the edge of on/off the script will compensate a lot of fluctuations for you. (stable pin).

It is my best effort without any additional hardware attached to the tcrt-5000. And has been running nicely for over 58 days now.
Thanks for the info, will try that. First I have to search what to do with the isrcounter. Any hints on where I have to put it? Do I need to recompile wiringpi?

Thanks so far anyway, you helped me in the right direction :)

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 07 May 2015 20:43
by sincze
did you install the whole wiringpi including the c compiler? I can help you with the rest.

Re: Watermeter through GPIO and TCRT5000

Posted: Monday 01 June 2015 12:03
by Bramz
Is polling really the right way to go? I always read that it takes up a disproportional amount of processor time if the pulses to be counted are not widely spaced apart.

Re: Watermeter through GPIO and TCRT5000

Posted: Sunday 07 June 2015 11:01
by sincze
Bramz wrote:Is polling really the right way to go? I always read that it takes up a disproportional amount of processor time if the pulses to be counted are not widely spaced apart.
I thought so to.
That is why I tried it out.
The process takes 0.3% of the processor.

Impact almost nothing and having all the fun of a working watermeter without a resistor.

Re: Watermeter through GPIO and TCRT5000

Posted: Thursday 03 December 2015 10:33
by sincze
Latest watermeter mount based upon tcrt-5000 sensor

Re: Watermeter through GPIO and TCRT5000

Posted: Saturday 18 February 2017 21:02
by joostnl
Can someone write me a step guide to install, Isrcounter.
My script for water is working only i want to make it more accurate