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

Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

Aaaah, too bad...

Today my sensor came in (the one with the yellow cap on the end), and it worked really well when I held a screwdriver next to it, but it looks like the plastic cap on top of the half-moon disc is too thick. It doesn't respond to that in any way... I've tried it with 5V and GND from the Pi (signal-cable not attached of course), and also with a 12V-adapter connected to it.

Any ideas if I can do something about this, or do I have to find a completely other way to register the water-usage? I'd rather not start damaging the water-meter since that belongs to the water-company.
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 zener diode is for protection you have to combine them. It prevents that the GPIO pin get more then 3.3v. Which brand and type of meter do you have? You tested it with a screwdriver and the led did respond?
Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

pvangorp wrote: Friday 25 August 2017 19:20 The zener diode is for protection you have to combine them. It prevents that the GPIO pin get more then 3.3v. Which brand and type of meter do you have? You tested it with a screwdriver and the led did respond?
Yeah, I understand the diode, but I have not even come that far yet.

I have tested with a scredriver, and the sensor responds: the led on the back lights up, and the voltage on the black wire drops to 0,5/0,6 volts.
I have a Sensus 620 watermeter (picture isn't mine, but mine is identical):

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

Should work, strange I use https://www.kiwi-electronics.nl/inducti ... anel-mount. Your meter is capabele http://www.homewizard.nl/shop/energie/p ... ensus.html. The higher the voltage the better the detection. Can you test 24v?
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 »







Oupu


Sent from my iPhone using Tapatalk
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by heggink »

There are many people who have done this successfully. I use the tcrt5000 with the gpio and have a spare one if you want.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

pvangorp wrote: Friday 25 August 2017 19:38 Should work, strange I use https://www.kiwi-electronics.nl/inducti ... anel-mount. Your meter is capabele http://www.homewizard.nl/shop/energie/p ... ensus.html. The higher the voltage the better the detection. Can you test 24v?
Will search for an adapter. Thanks for the tip, and I will report back.
Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

heggink wrote: Friday 25 August 2017 20:13 There are many people who have done this successfully. I use the tcrt5000 with the gpio and have a spare one if you want.
Do you have the same watermeter?
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by heggink »

yes.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

heggink wrote: Friday 25 August 2017 20:53yes.
Then I sure am interested in that spare!
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by heggink »

sent you a pm.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
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 was testing my meter on demand and that worked fine. I modified the Python script for my meter and it runs fine on command line. But when I try to create a crontab job @reboot it won't work.

Code: Select all

sudo mkdir /var/log/watermeter
sudo crontab -e
@reboot /usr/bin/python /home/pi/domoticz/scripts/watermeter.py >/var/log/watermeter/cronlog 2>&1

Code: Select all

grep cron /var/log/syslog
shows the job, but

Code: Select all

sudo cat /var/log/watermeter/cronlog
is empty

I've modified the python script so it would print output on start and on stop. But it doesn't work

When I execute the code below the log file is filled correctly after I cancel the process

Code: Select all

sudo su
/usr/bin/python /home/pi/domoticz/scripts/watermeter.py >/var/log/watermeter/cronlog 2>&1
Since this didn't work I changed it to an init.d process

Code: Select all

#! /bin/bash
### BEGIN INIT INFO
# Provides:          Watermeter
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Initialize the watermeter
# Description:       This daemon will start monitoring the watermeter
### END INIT INFO
sudo /usr/bin/python /home/pi/domoticz/scripts/watermeter.py

Code: Select all

sudo chmod a+x ~/watermeter_init.sh
sudo mv ~/watermeter_init.sh /etc/init.d/
sudo update-rc.d watermeter_init.sh defaults
That also doesn't work

But when I execute it from the command line

Code: Select all

sudo /etc/init.d/watermeter_init.sh
It works fine. What am I doing wrong? I have shutdown.sh script @reboot which works fine. When you push a button the system goes down (correct shutdown). This should work the same?!
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by D'rMorris »

In which cron did you place this? Cron for root? Or cron for user pi for example?
If in cron for pi, try to add the sudo command in the cron :-).
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 used

Code: Select all

sudo crontab -e
that is the root one?!
I got the init.d method working. Are there any downsites of using the init.d instead of cron?
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by D'rMorris »

Not necessarily. As far as I know: when you do crontab -e it creates a cron for the user that you are currently logged in with. So if you do that when logged in with user pi, it creates a crontab for user pi. I think that GPIO access needs root so that is why you would eiter sudo it or create a crontab for user root.
Hobbybob

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Hobbybob »

D'rMorris wrote: Monday 28 August 2017 13:24 Not necessarily. As far as I know: when you do crontab -e it creates a cron for the user that you are currently logged in with. So if you do that when logged in with user pi, it creates a crontab for user pi. I think that GPIO access needs root so that is why you would eiter sudo it or create a crontab for user root.
I think pvangorp is correct.
When you do "crontab -e" you get the cronjobs for the user that is logged in (usually pi), but if you do "sudo crontab -e" you get the cronjobs for root.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by D'rMorris »

That's easy to check of course. Do a sudo crontab -e when logged in with user pi and then switch to user root and check the crontab there :-)
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 »

It's not an user issue, it was in the correct user crontab. Another script works fine. I think it's a netwerk/domoticz issue. When you reboot both services are not started and the script is executed before those services are up. Init.d is started later and vervifies that netwerk is up before starting the script. Thats way that option does work
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 »

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&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"
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 »

You also have the https://www.kiwi-electronics.nl/inducti ... anel-mount? It sends 5v when no contact and when contact drops it to 0v. I used a zener diode on the black wire to convert the 5v to 3.3v
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest