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

Pantagruel
Posts: 7
Joined: Wednesday 24 April 2019 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10659
Location: Nieuw Vennep
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Pantagruel »

Thank you all for the great info and read.

As a relative n00b to domoticz (our gas and electrics metering got smart the beginning of april 2019), I was able to add the smart gas/electrics meter to domoticz (p1 cable, easy-peasy).

It took a bit more time and effort to add an NPN inductive sensor to monitor water usage (Sensus 620 meter) and after some messing and configuration it seems to work quite well.
Reading through the pages has given me enough food for thought and over time I'll reshape the current software side of life (python scripts combined from different source, using cron, etc). Guess my reading list for this summer vacation will be all about Python and LUA ;)
pedemo
Posts: 17
Joined: Saturday 04 May 2019 11:29
Target OS: Raspberry Pi / ODroid
Domoticz version: V 2020.2
Location: the Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pedemo »

I've a RPi with an NPN sensor (LJ12A3-4-Z/BX Nabijheids sensor N/O NPN 4mm) and connected (Black > pin 40, Brown > 2, Blue > 39).
I'm using the python code from markiemark (thanks !). When I hold the NPN against a metal item the LED lights up and when removing the metal de LED is off so NPN seems to work perfectly. But the strange thing is the RPi doesn't receive any kind of signal.

I read the whole forum but no clue there for me so can anyone help me out?

UPDATE:
I tested it with a multimeter and a 9V battery. In contact with metal the multimeter shows 9,67V and on 'no metal contact' it shows 0,67V
I switch thebattery with the RPi and it shows 4,22 V in both situation (with and without in contact with metal). So I assume 5V is not enough

Is my assumption correct? and if so are there any kind of NPN which properly work with 5V from the RPi
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 »

pedemo wrote: Saturday 04 May 2019 11:41 I've a RPi with an NPN sensor (LJ12A3-4-Z/BX Nabijheids sensor N/O NPN 4mm) and connected (Black > pin 40, Brown > 2, Blue > 39).
I'm using the python code from markiemark (thanks !). When I hold the NPN against a metal item the LED lights up and when removing the metal de LED is off so NPN seems to work perfectly. But the strange thing is the RPi doesn't receive any kind of signal.

I read the whole forum but no clue there for me so can anyone help me out?

UPDATE:
I tested it with a multimeter and a 9V battery. In contact with metal the multimeter shows 9,67V and on 'no metal contact' it shows 0,67V
I switch thebattery with the RPi and it shows 4,22 V in both situation (with and without in contact with metal). So I assume 5V is not enough

Is my assumption correct? and if so are there any kind of NPN which properly work with 5V from the RPi
its true that in some cases the 5v of the Rpi is not sufficient. a different power supply might help.
when you use the battery, does domoticz get a signal?
joostnl
Posts: 68
Joined: Wednesday 03 February 2016 19:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by joostnl »

I got it working with esp, sorry if there is a referral.
I had to sand down the plastic cover a few .1 mm before it detected the water meter indicator.
Espeasy works more stable and easy too tune, just my opinion

€ 2,44 10%OFF | Free shipping M12 4mm sensing DC 5V inductive proximity sensor switch NPN NO LJ12A3-4-Z/BX-5V work voltage 5VDC special for MCU
https://s.click.aliexpress.com/e/bq0nvxMM
pedemo wrote: Saturday 04 May 2019 11:41 I've a RPi with an NPN sensor (LJ12A3-4-Z/BX Nabijheids sensor N/O NPN 4mm) and connected (Black > pin 40, Brown > 2, Blue > 39).
I'm using the python code from markiemark (thanks !). When I hold the NPN against a metal item the LED lights up and when removing the metal de LED is off so NPN seems to work perfectly. But the strange thing is the RPi doesn't receive any kind of signal.

I read the whole forum but no clue there for me so can anyone help me out?

UPDATE:
I tested it with a multimeter and a 9V battery. In contact with metal the multimeter shows 9,67V and on 'no metal contact' it shows 0,67V
I switch thebattery with the RPi and it shows 4,22 V in both situation (with and without in contact with metal). So I assume 5V is not enough

Is my assumption correct? and if so are there any kind of NPN which properly work with 5V from the RPi
pedemo
Posts: 17
Joined: Saturday 04 May 2019 11:29
Target OS: Raspberry Pi / ODroid
Domoticz version: V 2020.2
Location: the Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pedemo »

its true that in some cases the 5v of the Rpi is not sufficient. a different power supply might help.
when you use the battery, does domoticz get a signal?
[/quote]

Yes domoticz get the signal. I now ordered a NPN which is more suitable for 5V
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 »

pedemo wrote: Wednesday 08 May 2019 13:08 its true that in some cases the 5v of the Rpi is not sufficient. a different power supply might help.
when you use the battery, does domoticz get a signal?
Yes domoticz get the signal. I now ordered a NPN which is more suitable for 5V
[/quote]

ok, great! please let us know your results.
PvHeerebeek
Posts: 3
Joined: Friday 21 June 2019 21:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Watermeter lezen met raspberry en Inductieve NPN sensor

Post by PvHeerebeek »

I used a different approach, I ordered a very small 5V to 12V up-convertor to power the inductive sensor with 12V from the RPI and a resistor divider of 10k and 3k9 to convert the 12V back to 3.3V for my RPI-Zero-W GPIO pin 21.
The sensor signal is very stable and all worked instantly.

I soldered step-up convertor and resisters into the connection wires of the sensor and used a heat-shrink to cover it all.

Image

Image
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 »

joostnl wrote:I got it working with esp, sorry if there is a referral.
I had to sand down the plastic cover a few .1 mm before it detected the water meter indicator.
Espeasy works more stable and easy too tune, just my opinion

€ 2,44 10%OFF | Free shipping M12 4mm sensing DC 5V inductive proximity sensor switch NPN NO LJ12A3-4-Z/BX-5V work voltage 5VDC special for MCU
https://s.click.aliexpress.com/e/bq0nvxMM
[/quote]

Hi Joostnl,

Does the sensor work natively with espeasy ? Are you using the esp software on a esp8266 or...? Liked your idea :)


Sent from my iPhone using Tapatalk
joostnl
Posts: 68
Joined: Wednesday 03 February 2016 19:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by joostnl »

Trigun wrote: Saturday 22 June 2019 10:50
joostnl wrote:I got it working with esp, sorry if there is a referral.
I had to sand down the plastic cover a few .1 mm before it detected the water meter indicator.
Espeasy works more stable and easy too tune, just my opinion

€ 2,44 10%OFF | Free shipping M12 4mm sensing DC 5V inductive proximity sensor switch NPN NO LJ12A3-4-Z/BX-5V work voltage 5VDC special for MCU
https://s.click.aliexpress.com/e/bq0nvxMM
Hi Joostnl,

Does the sensor work natively with espeasy ? Are you using the esp software on a esp8266 or...? Liked your idea :)


Sent from my iPhone using Tapatalk
[/quote]

Off course, esp easy very easy to implement. You can also filter your input signal
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 »

joostnl wrote:
Trigun wrote: Saturday 22 June 2019 10:50
joostnl wrote:I got it working with esp, sorry if there is a referral.
I had to sand down the plastic cover a few .1 mm before it detected the water meter indicator.
Espeasy works more stable and easy too tune, just my opinion

€ 2,44 10%OFF | Free shipping M12 4mm sensing DC 5V inductive proximity sensor switch NPN NO LJ12A3-4-Z/BX-5V work voltage 5VDC special for MCU
https://s.click.aliexpress.com/e/bq0nvxMM
Hi Joostnl,

Does the sensor work natively with espeasy ? Are you using the esp software on a esp8266 or...? Liked your idea :)


Sent from my iPhone using Tapatalk
Off course, esp easy very easy to implement. You can also filter your input signal[/quote]
Hi Joost,

How did you configure it? I got the esp and the sensor. Connected it directly to the esp and see some result in Domoticz. But you only see the usage. I also would like to see the the number the meter is showing in Domoticz, do you also have this working?



Sent from my iPhone using Tapatalk
MaartenTMM
Posts: 2
Joined: Monday 23 November 2015 16:13
Target OS: -
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by MaartenTMM »

rubberduck84 wrote: Friday 05 October 2018 20:39 Ik have problems with the python script.
I got this error on domoticz

2018-10-05 20:37:32.288 Error: EventSystem: Failed to execute python event script "watermeter"
2018-10-05 20:37:32.289 Error: EventSystem: File "<string>", line 61
2018-10-05 20:37:32.289 Error: EventSystem: print "JSON call = "+ str(url1)
2018-10-05 20:37:32.289 Error: EventSystem: ^
2018-10-05 20:37:32.289 Error: EventSystem: SyntaxError: invalid syntax

Have anyone the fix for this problem?
did u fixed it?
Justino
Posts: 19
Joined: Thursday 14 January 2016 19:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10548
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Justino »

Is there a way to change the "Last 24 hours" view from "1 hour" blocks into a "per 5 minute" graph like Power monitoring?

Example:
Image
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 »

Justino wrote: Friday 27 September 2019 22:08 Is there a way to change the "Last 24 hours" view from "1 hour" blocks into a "per 5 minute" graph like Power monitoring?

Example:
Image
you can export your data to an influx database and use grafana to create some cool graphs!
Justino
Posts: 19
Joined: Thursday 14 January 2016 19:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10548
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Justino »

Trigun wrote: Tuesday 01 October 2019 12:57
Justino wrote: Friday 27 September 2019 22:08 Is there a way to change the "Last 24 hours" view from "1 hour" blocks into a "per 5 minute" graph like Power monitoring?

Example:
Image
you can export your data to an influx database and use grafana to create some cool graphs!
Hi Trigun, i'm aware of this but i would like the "5 minute view" in domoticz since i don't use Grafana.
Any idea how to change the view in Domoticz?
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 »

[/quote]

sorry mate, can't help you with that.
i know you can zoom in but that's static. I don't believe you can alter the graphs.
akamming
Posts: 337
Joined: Friday 17 August 2018 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by akamming »

joostnl wrote: Monday 06 May 2019 19:58 I got it working with esp, sorry if there is a referral.
I had to sand down the plastic cover a few .1 mm before it detected the water meter indicator.
Espeasy works more stable and easy too tune, just my opinion

€ 2,44 10%OFF | Free shipping M12 4mm sensing DC 5V inductive proximity sensor switch NPN NO LJ12A3-4-Z/BX-5V work voltage 5VDC special for MCU
https://s.click.aliexpress.com/e/bq0nvxMM
Nice. I tried myself several times before, but apparently with the wrong npn sensor: I ordered this sensor en this was the 1st time i could detect an input signal if i check with the "gpio readall" command.

Next step was to get the input in domoticz. For that i used the info on https://www.domoticz.com/wiki/GPIO. But this fails for me.

I initialize the pin using the following statements

Code: Select all

#GPIO Watermeter
echo 18 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio18/direction
echo 0 > /sys/class/gpio/gpio18/active_low
echo both > /sys/class/gpio/gpio18/edge
raspi-gpio set 18 pd
on sysfs level it is working ok (cat /sys/class/gpio/gpio18/value tells me wheter the npn sensor is detecting metal).

In domoticz i use the generic sysfs gpio hardware, with autoconfigure enabled. And the pin is detected

Code: Select all

2019-10-19 11:03:45.431  Status: Sysfs GPIO: Startup - polling:no interrupts:yes debounce:50msec inputs:1 outputs:2
and i got a new device (switch, lighting 2, ac, and the unit is 18. Which is equal to the pin number, so to me this seems ok)

However this device does not read the npn sensor: The input does not reach domoticz: the switch is not triggered:

the gpio itself seems to work correctly:
- on fs level i see the correct value , but in domoticz i see no triggering of the switch.
- The interupt also works. Cause as a workaround i ran this python script: https://ehoco.nl/download/watermeter-ui ... on-versie/, which worked perfectly. I however do not like to run an additional script, so i want to make this work directly in domoticz itself

Does anyone have a suggestion on how the fix the gpio input device in domoticz?
akamming
Posts: 337
Joined: Friday 17 August 2018 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by akamming »

Update: i have found the gpio pins using the domoticz driver very unstable, so if someone can answer my previous post, that would be much appreciated

Nevertheless for now i accept this. But to circumvent all the manual steps written in this thread i've rewritten the python script to a domoticz plugin. which does all these steps (creating the device, configuring the gpio pin, etc..) itself, so i don't have to manage a seperate python script to keep it up and running on my system.

for me it works! But it would be nice if other people would test this plugin, so i can make it more broadly available.

https://github.com/akamming/domoticz-wa ... its/master

Installation is easy:
- go to you plugins directory
- give the command "git clone https://github.com/akamming/domoticz-watermeter.git"
- restart domoticz
- add the hardware. In the hardwarepage there is a description on how to configure the plugin

Please let me know you findings!

Feel free to enhance the code, as long as you create a pull request, so i can merge it back to the original
asg
Posts: 9
Joined: Wednesday 02 April 2014 11:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by asg »

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&param=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&param=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"
asg
Posts: 9
Joined: Wednesday 02 April 2014 11:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by asg »

I'm swithced to the script https://www.domoticz.com/forum/viewtopi ... e3#p201321 of WillyWong1. The script I posted above wasn't accurate enough: a mismatch of 10 - 50l a day. With the script of WillyWong1 I've no mismatch since using this script more than a week ago.
JeeGee
Posts: 22
Joined: Sunday 25 August 2019 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by JeeGee »

akamming wrote: Tuesday 22 October 2019 14:34 Update: i have found the gpio pins using the domoticz driver very unstable, so if someone can answer my previous post, that would be much appreciated

Nevertheless for now i accept this. But to circumvent all the manual steps written in this thread i've rewritten the python script to a domoticz plugin. which does all these steps (creating the device, configuring the gpio pin, etc..) itself, so i don't have to manage a seperate python script to keep it up and running on my system.

for me it works! But it would be nice if other people would test this plugin, so i can make it more broadly available.

https://github.com/akamming/domoticz-wa ... its/master

Installation is easy:
- go to you plugins directory
- give the command "git clone https://github.com/akamming/domoticz-watermeter.git"
- restart domoticz
- add the hardware. In the hardwarepage there is a description on how to configure the plugin

Please let me know you findings!

Feel free to enhance the code, as long as you create a pull request, so i can merge it back to the original
@akamming

As a NOOB I would like to get more information in how to set up this, with you plugin. I ordered the NPN and got it, didn't connect it yet to my Pi 3b.
I managed to download/l=install the plugin from the command line and did a reboot.

After that I am lost, sorry. Could you describe the next steps please?

How to add the hardware? Where to configure stuff? I don't see anything related to watermeter in the hardware page!

Thank you very much.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest