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

User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by philchillbill »

Evertjob89 wrote: Sunday 18 November 2018 9:22 The only problem i have is that the script (most likely) is pushing random liters to Domoticz, for example on 04:00 i've used 0 liters but Domoticz got 11 liters counted.
Does it happen every day or you only saw this behaviour once? When the little rotating metal disc stops due to water-flow stopping, it stops in a random position with respect to the NPN sensor. If, by chance, it stops just at the edge of the detection zone, you can get random ghost readings as the sensor thinks it sees metal, then doesn't, then does again, etc. The script tries to introduce some hysteresis to cope with this but at times you can still get some garbage. Nothing to worry about. If you get garbage every day, however, you need to check for a faulty sensor or wiring.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Evertjob89
Posts: 7
Joined: Sunday 18 November 2018 9:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Evertjob89 »

philchillbill wrote: Sunday 18 November 2018 10:02
Evertjob89 wrote: Sunday 18 November 2018 9:22 The only problem i have is that the script (most likely) is pushing random liters to Domoticz, for example on 04:00 i've used 0 liters but Domoticz got 11 liters counted.
Does it happen every day or you only saw this behaviour once? When the little rotating metal disc stops due to water-flow stopping, it stops in a random position with respect to the NPN sensor. If, by chance, it stops just at the edge of the detection zone, you can get random ghost readings as the sensor thinks it sees metal, then doesn't, then does again, etc. The script tries to introduce some hysteresis to cope with this but at times you can still get some garbage. Nothing to worry about. If you get garbage every day, however, you need to check for a faulty sensor or wiring.
Don't know if it's happening every day. Got sensor up and running for 24 hours and as i can see it happens every hour of the day. This is my water usage by the night according to Domoticz:

00:00 - 13 Liter
01:00 - 10 Liter
02:00 - 7 Liter
03:00 - 12 Liter
04:00 - 13 Liter
05:00 - 13 Liter
06:00 - 18 Liter
07:00 - 9 Liter
08:00 - 13 Liter

in 9 hours Domoticz get's 108 liters to much. Exactly 1 Liter every 5 minutes.

On this moment i don't use a Python script anymore and switched to LUA, the last update was 10:53:16 with a total of 319085 liters (cannot delete that one huge record to start on zero, don't ask why). I will do an assumption that the previous Pyton script won't push the liters as mentioned above in one time (it's impossible also, because the scripts does a simple +1 every time) and that some false liters from Python will be pushed randomly.

Will test this for some days, it's starting to look positive! :lol:

Edit: Maybe to positive, just start using some water and the switch is turned on and off but to liters are added to the "waterverbruik"
This is the error i get:

Code: Select all

2018-11-18 11:29:20.169 Error: EventSystem: in Watermeter: [string "commandArray = {}..."]:18: attempt to perform arithmetic on global 'sWaterUsage' (a nil value)
Evertjob89
Posts: 7
Joined: Sunday 18 November 2018 9:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Evertjob89 »

Oke,

Just got is tested 24 hours and everything looks stable, the only problem i just had was that Domoticz counted liters while the meter stood still. The sensor itself was 'on' (the LED was on). The history of the logs are to short to view something logic :?:
melgi
Posts: 23
Joined: Tuesday 15 August 2017 15:50
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by melgi »

My water meter has just been replaced by an exact water meter as posted in this thread. I'd like to tinker a bit with the water meter sensor. My only problem is I don't have a raspberry pi, but an Asustor NAS running my Domoticz installation. How can I connect the inductive meter to my NAS? I was thinking through USB, but I don't have any experience with making USB cables. Is there anyone out there with experience on this field and maybe an example how I can connect the inductive water sensor to my NAS?
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 »

User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by philchillbill »

Or run the python script on a rpi zero-w (cheap) dedicated to your water meter. It does NOT need to have Domoticz installed so you can leave D on your NAS. This is what I have running smoothly for over a year here.


Sent from my iPhone using Tapatalk
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by assenzuid »

Is there a way to read this type Watermeter?

Image
Milifax
Posts: 69
Joined: Friday 23 June 2017 9:27
Target OS: Linux
Domoticz version: 2024.4
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Milifax »

assenzuid wrote: Thursday 13 December 2018 21:21 Is there a way to read this type Watermeter?

Image
You could try a magnetometer.
We have a meter similar as your without the ability to detect metal dials.
However, inside is a magnetic connection between dial and flow detector.

Try a a magnetometer app first on your phone. If it reacts in a sinuswave like form or if it is triggered in a rythm when water is running, a magnetometer is the way to go.
Works perfect over here, magnetometer connected to a arduino controller. All for a couple of bucks.

Let me know your findings and I'll be happy to share my solution
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by assenzuid »

Great, I will try it later today with the suggested app.
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by assenzuid »

Hi, there is some reaction when i use an Magnometer.
WillyWong1
Posts: 2
Joined: Saturday 20 October 2018 10:34
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by WillyWong1 »

I was triggered by the simple logics of jnmulder, so I rewrote the script myself and it's working.
No messing around with bounce values or event reading on the GPIO etc.
I enriched the code with a config file and a subroutine to write the logs. Logs are now written in daily logfiles and cleaned after 20 days.

Here's the code snippet:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import urllib2
import os
import json
import base64
import logging
import logging.handlers
import sys
from time import localtime, strftime
from datetime import datetime
from ConfigParser import SafeConfigParser


###########################
# Watermeter stand (wordt alleen initeel gebruikt als er geen bestand meterstand.txt is)
###########################

global Counter
global timer1
global localbase64string
global remotebase64string

Counter = 530770

###########################
# PERSONAL CONFIG FILE READ
###########################
parser = SafeConfigParser()
parser.read('/home/pi/watermeter/watermeter.ini')
# Read path to log file
LOGFILE = parser.get('config', 'log_filename')
LOG_PERIOD = parser.getint('config', 'log_period')
# remote logging URL (Synology)
domoticz_url_syn = parser.get('config', 'remotelog_url')

# Domoticz url
domoticz_url = parser.get('config', 'local_url')

#Domoticz IDX van de water sensor (RFXMeter)
idx = parser.get('config', 'idx_local')
# Settings for the domoticz server
localdomoticzusername = "XXXXXXX"  #Your Domoticz username here
localdomoticzpassword = "XXXXXXX"  #Your Domoticz password here
localbase64string = base64.encodestring('%s:%s' % (localdomoticzusername, localdomoticzpassword)).replace('\n', '')

#Open meterstand.txt file en lees meterstand
#Als meterstand.txt niet aanwezig is maakt script bestand aan en vult de meterstand
WATER_FILE = parser.get('config', 'waterstand_file')

#################
#  LOGGING SETUP
#################
logging.basicConfig()

LOG_LEVEL = logging.INFO  # Could be e.g. "DEBUG" or "WARNING"

# Configure logging to log to a file, making a new file at midnight and keeping the last 3 day's data
# Give the logger a unique name (good practice)
logger = logging.getLogger(__name__)
# Set the log level to LOG_LEVEL
logger.setLevel(LOG_LEVEL)
# Make a handler that writes to a file, making a new file at midnight and keeping 20 backups
handler = logging.handlers.TimedRotatingFileHandler(LOGFILE, when="midnight", backupCount=10)
#handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=1000000, backupCount=5)
# Format each log message like this
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
# Attach the formatter to the handler
handler.setFormatter(formatter)
# Attach the handler to the logger
logger.addHandler(handler)

# Make a class we can use to capture stdout and sterr in the log
class MyLogger(object):
    def __init__(self, logger, level):
        """Needs a logger and a logger level."""
        self.logger = logger
        self.level = level

    def write(self, message):
        # Only log if there is a message (not just a new line)
        if message.rstrip() != "":
            self.logger.log(self.level, message.rstrip())

# Replace stdout with logging to file at INFO level
sys.stdout = MyLogger(logger, logging.INFO)
# Replace stderr with logging to file at ERROR level
sys.stderr = MyLogger(logger, logging.ERROR)

#################
#  INIT
#################

if os.path.exists(WATER_FILE):
    f = file(WATER_FILE, "r+")
    f = open(WATER_FILE)
    inhoud = f.readline()
    a,b,c = inhoud.split()
    Counter = int(c)
else:
    f = open(WATER_FILE, "w")
    f.write( 'meterstand = ' + repr(Counter))
    f.close()

    logger.info('Meterstand = ' + repr(Counter) + " - intiele stand opvragen\n")

def domoticzrequest (url,basestr):
    request = urllib2.Request(url)
    request.add_header("Authorization", "Basic %s" % basestr)
    response = urllib2.urlopen(request)
    return response.read()

#################
#  SETUP GPIO BOARD
#################

GPIO_PIN = 10   
#Board is pin nr 10, BMC is GPIO 15
#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(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)


try:
   TempVar = 1
   #Openen log en meter bestand
   f = file(WATER_FILE, "r+")
   f = open(WATER_FILE)
   inhoud = f.readline()
   a,b,c = inhoud.split()
   Counter = int(c)
   f.close()

   while True:
      time.sleep(0.5)
      varDetection = GPIO.input(GPIO_PIN)
      #logger.info('GPIO=' + str(varDetection)
      varDetection = GPIO.input(GPIO_PIN)
      if varDetection == 1 and TempVar == 0:
        TempVar = 1
        #Teller elke interupt uitlezen en met 1 liter verhogen 
        logger.info( 'Meterstand=' + repr(Counter) + ' GPIO=' + str(GPIO.input(GPIO_PIN)) + " - Detection")
        Counter = Counter + 1
        #Schrijf meterstand naar bestand
        f = open( WATER_FILE, 'w')
        f.write( 'meterstand = ' + repr(Counter))
        f.close()
        logger.info( 'Meterstand=' + repr(Counter) + ' GPIO=' + str(GPIO.input(GPIO_PIN)) + " - Meterstand naar bestand geschreven")
        #Send counter to domoticz JSON
        url1 = domoticz_url+'/json.htm?type=command&param=udevice&idx='+str(idx)+'&svalue='+str(Counter)
        domoticzrequest(url1 + "&passcode=" + localdomoticzpassword,localbase64string)
      elif varDetection == 0 and TempVar == 1:
        #Wachten op positive puls van water meter sensor
        logger.info( 'Meterstand=' + repr(Counter) + ' GPIO=' + str(GPIO.input(GPIO_PIN)) + " - Wacht op pos. impuls van sensor")
        laag = datetime.now()  
        TempVar = 0
    
except KeyboardInterrupt:
  GPIO.remove_event_detect(GPIO_PIN)
  GPIO.cleanup()
  logger.info( "\nClosing watermeter reader")
  
The 'watermeter.ini' file look like this:

Code: Select all

[config]
waterstand_file = /home/pi/watermeter/meterstand.txt
log_filename = /home/pi/watermeter/watermeter.log
local_url = http://192.168.XX.XX:XXX    --- Local Domoticz URL 
idx_local = 5   --- IDX from the metermeter in Domoticz
log_period = 300
 
jnmulder wrote: Wednesday 04 April 2018 21:25 Thanx for youre repsponse.
instead I changed my script
Every 0.5 sec, I read out the level of the GPIO in an infinit loop.
When it has changed to on I add the counter with 1 and set a variable to on
next reading gpio = on and variable = on nothing happenes
next readin gpio = off variable = changed to off

so far (over 1 week now) not 1 liter deviation :-)


philchillbill wrote: Wednesday 21 March 2018 8:23
jnmulder wrote:Thanx for youre response.

The cable is about 1, m long and not shielded.
There are some electrical devices nearby so youre suggestion could be right. What is the best way to shield the cable? Shield the complete kabel from sensor until Rpi?

Yep, and make sure the shield is grounded. You basically want something like a coax cable where an earthed shield surrounds the core. For a 1m length, just wrapping some tin foil like used for baking might do the trick and be quick to try.


Sent from my iPhone using Tapatalk
l00pz
Posts: 42
Joined: Wednesday 01 April 2015 11:52
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by l00pz »

I ordered this sensor from AliExpress and connected it to a Raspberry Pi 2B and Wemos D1, but I'm unable to get a reading from it. When connected to a Multimeter I see that the voltage doesn't drop when holding a metal object in front of it. Connecting it to a 9V battery does seem to work, so the sensor is functional. There are multiple people in this topic that ordered this specific sensor, so I'm wondering if I'm doing something wrong?
WillyWong1
Posts: 2
Joined: Saturday 20 October 2018 10:34
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by WillyWong1 »

Please check https://www.domoticz.com/forum/viewtopi ... 60#p155725
It a description how to make use of a level-shifter
l00pz wrote: Friday 04 January 2019 9:16 I ordered this sensor from AliExpress and connected it to a Raspberry Pi 2B and Wemos D1, but I'm unable to get a reading from it. When connected to a Multimeter I see that the voltage doesn't drop when holding a metal object in front of it. Connecting it to a 9V battery does seem to work, so the sensor is functional. There are multiple people in this topic that ordered this specific sensor, so I'm wondering if I'm doing something wrong?
l00pz
Posts: 42
Joined: Wednesday 01 April 2015 11:52
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by l00pz »

WillyWong1 wrote: Saturday 12 January 2019 16:14 Please check https://www.domoticz.com/forum/viewtopi ... 60#p155725
It a description how to make use of a level-shifter
l00pz wrote: Friday 04 January 2019 9:16 I ordered this sensor from AliExpress and connected it to a Raspberry Pi 2B and Wemos D1, but I'm unable to get a reading from it. When connected to a Multimeter I see that the voltage doesn't drop when holding a metal object in front of it. Connecting it to a 9V battery does seem to work, so the sensor is functional. There are multiple people in this topic that ordered this specific sensor, so I'm wondering if I'm doing something wrong?
For anyone else looking to do this, I ordered an NPN Sensor which is specified for 5V on AliExpress and this works perfect on a Wemos D1 Mini or Raspberry Pi without reverting to the use of level-shifters.
dfrenner
Posts: 1
Joined: Friday 18 January 2019 0:36
Target OS: -
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by dfrenner »

Jeff wrote: Friday 28 April 2017 0:17 Why don't you take it the easy way?
Since the pictured meter is a itron Aquadis+ you can achieve the same with less editing in files etc. by buying a so called Cyble sensor.
This sensor just snaps in place on top of the meter and can be connected to a piface input for monitoring the water consumption. The only downside of this is the price.
I ordered the Cyble meter.
So now i connected the cable to GPIO pin 39 and 40.

So the white cable is connected to "GPIO21" (pin 40)
And the brown cable is connected to "Ground" (pin 39)

But in what way is it easier to do this whit this cyble meter?
What are my next steps? I'm a noob in Domoticz :lol:
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 »

@WillyWong1

Thank you, with that script it is working like a charm, science one month no one liter more or less. It is working perfect.

But if no water is used more than one hour, the watersensor in domoticz wil be shown as offline, is it possible to ad to the scipt that it send the "meterstand" every 5 min to domoticz if no water is used?
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by philchillbill »

@micha123

Just go to setup --> settings --> other --> sensor timeout in Domoticz and set a value like 86400 and you're done.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Doudy
Posts: 246
Joined: Tuesday 09 August 2016 9:09
Target OS: -
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Doudy »

;)
RaspberryPi - RFLink - Zwave - WH2600
Domoticz : 2020.2 | Dashticz : V3.12 Master | dzvents : 3.0.2 | Python : 3.7.3
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 »

Yes i know, but i have other sensors and i need to see it if there are offline.

Thats the problem hehe
Bver
Posts: 3
Joined: Monday 04 February 2019 20:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Bver »

@WillyWong1: thanks for the code, I wrote a more basic but similar code before I saw yours, also based on jnmulders suggestion.

Wouldn't it be possible to increase the meter readout resolution to 0.5 L instead of 1 L, since you detect both rising and falling of the GPIO value? In the 'elif' routine, the counter could also be increased with 0.5 L.
My first attempt at this failed, I guess since the Counter variable only allows rounded values. Perhaps someone with more python knowledge could hint at how to have the variable 'Counter' also allow decimal values?
Last edited by Bver on Thursday 25 April 2019 21:42, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest