Watermeter lezen met raspberry en Inductieve NPN sensor Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Micha123
Posts: 62
Joined: Monday 28 August 2017 16:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 »

I dont know why it is so written in your link, i have tested this sensor, it gives 0V if no contact on BLACK wire, and if contact (RED Led is on) then i have 5V on black wire.


If you have a simple voltage meter you can test it.
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

I asked if yours is from kiwi electronics. Mine works reverse when testing with a multi meter. The led lids up when contact, but voltage drops on the black wire. Looks like it uses a pull up resistor.
Micha123
Posts: 62
Joined: Monday 28 August 2017 16:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 »

No i have mine from aliexpress.

So guys please test your sensors, what type of sensor you have.

Maybe you can place the edited script on your first page, for guys that have not reverse working sensors.

Im testing it the while day, and it is realy good. No liter less or to much. Works perfect.
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

The sensor is working fine, but I have some problems possitioning the sensor on my elster meter. I'm currently fine tuning the position and my script. I have 0.3 m3 difference a day. So I'm fine tuning the script to correct this difference.
EdwinH
Posts: 6
Joined: Tuesday 29 August 2017 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Almere
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by EdwinH »

Great post markiemark !
Thanks to all who contributed !!

I ordered PNP sensor via aliExpress : https://nl.aliexpress.com/item/THGS-DC6 ... 99944.html

Code: Select all

THGS-DC6-36V 300mA NPN NO 3-wire 4mm Tubular Inductive Proximity Sensor Switch LJ12A3-4-Z-BX
Prijs US $2.72 / piece
Connected it to Raspberry PI2 it's 5V, GND and GPIO 10 ( pin 19).
' How-to' made by markiemark is easy to follow.
I used altered script of benbammens ( many thanks to you also ! )
Just one change in that script worked for me :

Code: Select all

Counter = Counter + 5 
made counter go up 5 points per liter.
solution was easy :

Code: Select all

Counter = Counter + 1
Domoticz now shows water usage perfect (after one week still spot on)
Domoticz Watermeter
Domoticz Watermeter
2017-08-30.png (15.87 KiB) Viewed 3500 times
For those who struggle , additional information about my setup :

Code: Select all

pi@raspberrypi ~ $ sudo crontab -e
#
# m h  dom mon dow   command
0 9 * * 0 /home/pi/domoticz/scripts/push_now.sh
*/5 * * * * /home/pi/domoticz/scripts/Domoticz_push.sh
*/5 * * * * /home/pi/domoticz/scripts/Omnik-Data-Logger/start_omnik.sh
*/5 * * * * /home/pi/domoticz/scripts/round/evo-update.sh
0 7 * * * /home/pi/domoticz/scripts/backup_domoticz.sh
2 15 * * * sudo reboot
@reboot sudo /usr/bin/python /home/pi/domoticz/scripts/watermeter.py >/var/log/cronlog.log 2>&1

Vitens watermeter
Vitens water meter
Vitens water meter
vitens water meter.jpg (320.61 KiB) Viewed 3498 times
PNP sensor via aliexpress:
https://nl.aliexpress.com/item/THGS-DC6 ... 99944.html, Glued with 'hot glue' (glue-gun) available in any DIY store.:
Attachments
PNP sensor
PNP sensor
water pnp.jpg (101.53 KiB) Viewed 3498 times
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

I removed all crontab and init.d stuff and created a LUA SCRIPT. In my case it's more accurate and runs directly from Domoticz. I had both init.d and LUA active and the LUA is accurate where the init.d script was of sometimes.

My LUA Script
commandArray = {}

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

-- 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);

-- For Debuging
-- print("Water usage until now is " .. sWaterUsagePrint .. "m3 ");
-- print('GPIO Watermeter = '..otherdevices['GPIO Watermeter']);

if (devicechanged['GPIO Watermeter'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)

print("Water usage is now " .. sWaterUsageTot / 1000 .. "m3 ");
commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..''
end

return commandArray
The red colored parts need to me reconfigured for your case.
To set the initial meter values http://<<YOUR DOMOTICZ IP>>/json.htm?type=command&param=udevice&idx=34&svalue=999999
Where "999999" = 999.999m3

Make sure to set LUA to DEVICE instead off ALL
Last edited by pvangorp on Sunday 03 September 2017 7:58, edited 1 time in total.
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

The problem with my previous script is that when no water used, the devices shows offline.
I fixed this with a keep a live part. When no water detected update with keep a live every 5 minutes, just like the other smart meters in Domoticz.

Code: Select all

commandArray = {}

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

-- 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);

-- For Debuging
-- print("Water usage until now is " .. sWaterUsagePrint .. "m3 ")
-- print('GPIO Watermeter = '..otherdevices['GPIO Watermeter'])


function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

if (devicechanged['GPIO Watermeter'] == 'Off')
then
	sWaterUsageTot = (sWaterUsage + 1)
	
	print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ");
	commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..''
else
    -- Keep a live device
    if (timedifference(otherdevices_lastupdate["Waterverbruik"]) > 300)
    then
        print("Water usage is still " .. sWaterUsage / 1000 .. "m3 ");
        commandArray['UpdateDevice'] = '34|0|'..sWaterUsage..''    
    end
end

return commandArray
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 »

Guys, a stupid question: do you use this method with a second pi (with domoticz installed), so a slave device?
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

Questions are never stupid. I have a test and production environment (two pi's). You don't need a slave device. One works fine! However you can push values from one to another using slave.
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 »

pvangorp wrote: Sunday 03 September 2017 11:59 Questions are never stupid. I have a test and production environment (two pi's). You don't need a slave device. One works fine! However you can push values from one to another using slave.
Thanks. My problem is that the meter cast is outside of the flat, so it's not possible to wire the sensor into my main pi. However I've configured a second pi with wifi (old pi2b with a dongle). I'd like to use this one as the host of the sensor. Just a bit confused with the current script. Do I need to install domoticz to this second pi, or just place the script?
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

Make sure everything is installed in the meter cast. Make sure the script is working. You need domoticz on both. Connect them as master slave. You can add the sensors of the one in the meter cast to the main one.
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun »

Hi Guy's, I bought the npn sensor as mentioned. But since I attached it to my Rpi the light on the back of the sensor is On and stays on. I reinstalled it and it seems the sensor is not working correctly as it goes on for a while and off as well without any metal being near the sensor. Any chance you guys have similar experiences? To be sure a new one is on its way.thnx in advance!


Sent from my iPhone using Tapatalk
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

With how many volt did you test it? And has your pi adapter enough ampère? Disconnect it completely from your pi and connect an 9v battery. Does it work correct with the battery? By testing it with a battery you know if the sensor is oké.
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 »

pvangorp wrote: Saturday 02 September 2017 20:53 I removed all crontab and init.d stuff and created a LUA SCRIPT. In my case it's more accurate and runs directly from Domoticz. I had both init.d and LUA active and the LUA is accurate where the init.d script was of sometimes.

My LUA Script
commandArray = {}

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

-- 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);

-- For Debuging
-- print("Water usage until now is " .. sWaterUsagePrint .. "m3 ");
-- print('GPIO Watermeter = '..otherdevices['GPIO Watermeter']);

if (devicechanged['GPIO Watermeter'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)

print("Water usage is now " .. sWaterUsageTot / 1000 .. "m3 ");
commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..''
end

return commandArray
The red colored parts need to me reconfigured for your case.
To set the initial meter values http://<<YOUR DOMOTICZ IP>>/json.htm?type=command&param=udevice&idx=34&svalue=999999
Where "999999" = 999.999m3

Make sure to set LUA to DEVICE instead off ALL
Making progress now, 3 more questions:

1. I'm a bit confused by the red colored parts? What are Watermeter and GPIO Watermeter?
2. I figured that 34 is your virtual counter (If I'm not mistaken. So that has to be created before, right?)
3. with this lua scripts, the initial python script and metercast.txt are obsolete, right?
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

1. Watermeter is the name of the virtual counter and GPIO Watermeter is the name of GPIO connected to the sensor
2. 34 is the idx of the virtual created counter Watermeter
3. Correct and you don't need cron job of unit.d everything is handled by domoticz

Make sure to set LUA to DEVICE instead off ALL
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 »

pvangorp wrote: Sunday 03 September 2017 17:45 1. Watermeter is the name of the virtual counter and GPIO Watermeter is the name of GPIO connected to the sensor
2. 34 is the idx of the virtual created counter Watermeter
3. Correct and you don't need cron job of unit.d everything is handled by domoticz

Make sure to set LUA to DEVICE instead off ALL
Thanks, last question: GPIO Watermeter is also a virtual counter or Generic sysfs GPIO (from Hardware Tab)?
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

A Generic sysfs GPIO on/off icon water
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun »

pvangorp wrote: Sunday 03 September 2017 13:51 With how many volt did you test it? And has your pi adapter enough ampère? Disconnect it completely from your pi and connect an 9v battery. Does it work correct with the battery? By testing it with a battery you know if the sensor is oké.
Hi pvangorp,

It just tested it with a 9volts battery and the light stays on, even when I hold a metal piece to it.
still need to test it with a multimeter but am affraid it not working correctly.
just to be sure:
brown = +
Blue = GND
Black = Signal

Thnx!
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 »

pvangorp wrote: Sunday 03 September 2017 18:13 A Generic sysfs GPIO on/off icon water
GPIO INIT ERROR :D

I know I'm almost there, damn it. Rechecking everything again. Connected sensor as described in OP. When I'm placing a magnet near the sensor, the right led goes on (so I assume the sensor is ok). I've tried switching black and blue wires as well.
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp »

Trigun, looks to me your sensor is defect!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest