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.
Post Reply
Trigun
Posts: 183
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 3.5877
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » Sunday 03 September 2017 18:58

pvangorp wrote:Trigun, looks to me your sensor is defect!
I thought so, thnx for confirming my suspicion. A new one is on its way, just have to wait for a few weeks ;)


Sent from my iPhone using Tapatalk

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Sunday 03 September 2017 19:00

LouiS22,

Connect the brown wire to 5v of the pi and Blue to the gnd. Connect black to a GPIO with a zener diode to convert the 5v to 3.3v. the GPIO needs 3.3v instead of 5v. Have you configured the sysfs export?

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Sunday 03 September 2017 19:06

Trigun wrote:
Sunday 03 September 2017 18:58
pvangorp wrote:Trigun, looks to me your sensor is defect!
I thought so, thnx for confirming my suspicion. A new one is on its way, just have to wait for a few weeks ;)


Sent from my iPhone using Tapatalk
Your welcome!

User avatar
LouiS22
Posts: 448
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Sunday 03 September 2017 19:18

pvangorp wrote:
Sunday 03 September 2017 19:00
LouiS22,

Connect the brown wire to 5v of the pi and Blue to the gnd. Connect black to a GPIO with a zener diode to convert the 5v to 3.3v. the GPIO needs 3.3v instead of 5v. Have you configured the sysfs export?

Code: Select all

sudo raspi-gpio set 21 ip pu  
echo "21" > /sys/class/gpio/export
echo "1" | sudo tee -a /sys/class/gpio/gpio21/active_low
echo "both" | sudo tee -a /sys/class/gpio/gpio21/edge

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Sunday 03 September 2017 19:26

LouiS22 wrote:
Sunday 03 September 2017 19:18
pvangorp wrote:
Sunday 03 September 2017 19:00
LouiS22,

Connect the brown wire to 5v of the pi and Blue to the gnd. Connect black to a GPIO with a zener diode to convert the 5v to 3.3v. the GPIO needs 3.3v instead of 5v. Have you configured the sysfs export?

Code: Select all

sudo raspi-gpio set 21 ip pu  
echo "21" > /sys/class/gpio/export
echo "1" | sudo tee -a /sys/class/gpio/gpio21/active_low
echo "both" | sudo tee -a /sys/class/gpio/gpio21/edge

Code: Select all

echo 21 > /sys/class/gpio/unexport
echo 21 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio21/direction
echo 0 > /sys/class/gpio/gpio21/active_low
echo both > /sys/class/gpio/gpio21/edge
raspi-gpio set 21 pd
You can test it using:

Code: Select all

sudo raspi-gpio get 21
Level=0 changes in Level=1

User avatar
LouiS22
Posts: 448
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Sunday 03 September 2017 19:46

pvangorp wrote:
Sunday 03 September 2017 19:26
LouiS22 wrote:
Sunday 03 September 2017 19:18
pvangorp wrote:
Sunday 03 September 2017 19:00
LouiS22,

Connect the brown wire to 5v of the pi and Blue to the gnd. Connect black to a GPIO with a zener diode to convert the 5v to 3.3v. the GPIO needs 3.3v instead of 5v. Have you configured the sysfs export?

Code: Select all

sudo raspi-gpio set 21 ip pu  
echo "21" > /sys/class/gpio/export
echo "1" | sudo tee -a /sys/class/gpio/gpio21/active_low
echo "both" | sudo tee -a /sys/class/gpio/gpio21/edge

Code: Select all

echo 21 > /sys/class/gpio/unexport
echo 21 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio21/direction
echo 0 > /sys/class/gpio/gpio21/active_low
echo both > /sys/class/gpio/gpio21/edge
raspi-gpio set 21 pd
You can test it using:

Code: Select all

sudo raspi-gpio get 21
Level=0 changes in Level=1
Thanks for the directions. I've tested, the level changes to 1 when placing a magnet near to the sensor - so it should be ok. However I still get the GPIO INIT ERROR in Domoticz.

To tell the truth, it's connected without the zener diode (have to purchase it tomorrow) - but I really don't think that's the cause of the GPIO INIT ERROR as the GPIO is ok (for now).

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Sunday 03 September 2017 19:56

That doesn't cause the error. It's for protection of the GPIO. It's designed for 3.3v. Do you use sysfs. Make sure not to use Raspberry GPIO and sysfs GPIO combined in domoticz.The GPIO part outside domoticz is oké. You only have to focus on domoticz.

User avatar
LouiS22
Posts: 448
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Sunday 03 September 2017 20:00

pvangorp wrote:
Sunday 03 September 2017 19:56
That doesn't cause the error. It's for protection of the GPIO. It's designed for 3.3v. Do you use sysfs. Make sure not to use Raspberry GPIO and sysfs GPIO combined in domoticz.The GPIO part outside domoticz is oké. You only have to focus on domoticz.
Hah... Found it another topic: DO NOT USE the manual add for the switch, the found devices are directly created in devices tab, if you try a manual add it will still display "GPIO INIT ERROR".

Which is true, I found the switch on my device tab created an hour ago... Now I have to recheck everything again...

Code: Select all

2017-09-03 20:01:22.841 Sysfs GPIO: gpio21 new state = on
2017-09-03 20:01:22.846 (Watermeter GPIO) Lighting 2 (GPIO Watermeter)
2017-09-03 20:01:27.405 Sysfs GPIO: gpio21 new state = off
2017-09-03 20:01:27.409 (Watermeter GPIO) Lighting 2 (GPIO Watermeter)

User avatar
LouiS22
Posts: 448
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Sunday 03 September 2017 20:12

pvangorp wrote:
Sunday 03 September 2017 19:56
That doesn't cause the error. It's for protection of the GPIO. It's designed for 3.3v. Do you use sysfs. Make sure not to use Raspberry GPIO and sysfs GPIO combined in domoticz.The GPIO part outside domoticz is oké. You only have to focus on domoticz.

Code: Select all

2017-09-03 20:10:00.929 EventSystem: Started
2017-09-03 20:10:00.929 EventSystem: Queue thread started...
2017-09-03 20:10:00.976 Active notification Subsystems: (0/12)
2017-09-03 20:10:09.776 Sysfs GPIO: gpio21 new state = on
2017-09-03 20:10:09.786 (Watermeter GPIO) Lighting 2 (Watermeter GPIO)
2017-09-03 20:10:09.981 LUA: Water usage is still 0m3
2017-09-03 20:10:09.986 EventSystem: Script event triggered: Watermeter
2017-09-03 20:10:13.458 Sysfs GPIO: gpio21 new state = off
2017-09-03 20:10:13.467 (Watermeter GPIO) Lighting 2 (Watermeter GPIO)
2017-09-03 20:10:13.601 LUA: Water usage is set to 0.001m3
2017-09-03 20:10:13.607 EventSystem: Script event triggered: Watermeter
Thank you so much, I owe you big time! The latest dumb was me: Eventsystem wasn't on (strange, I remember switched it on hours ago). So thanks again, now it's time to test the thing, and tomorrow install it at the meter cast. Hope the best, expect the worst :D

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Sunday 03 September 2017 20:16

You're welcome! It will work fine in master slave mode!

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Monday 04 September 2017 13:13

@markiemark,

can you add also an Keep Alive to your script ??? i dont know how to do that, but if no wather is used it Shows the RFXdevice as offline after one hour. so an Keep alive every 5 min if no water used wil be fine in your script


here is my script (it is modified by me for my NPN sensor)

#!/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:8081"
#Domoticz IDX van de water sensor (RFXMeter)
idx = 16

#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: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Monday 04 September 2017 15:52

Your using a Python script, the script I created with the keep a live is a LUA script in Domoticz. The script in Python is bound to the GPIO event and triggered when detect a GPIO rise "GPIO.add_event_detect(10, GPIO.RISING, callback = Interrupt, bouncetime = 800)". It works different, LUA checks every X seconds if status is changed and Python triggers on a detect of the GPIO.

Why don't you use LUA instead of Python? Than the script is in Domoticz and not an external script.

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Monday 04 September 2017 16:38

Im new in domoticz, linux is not a problem

I created an lua script file, and added a generic gpio device in domoticz,, domoticz recognized in devices an IO device with name watermeter.

But id dindt works.

So if you can exactly explain what to do i can use the lua script.

What is watermeter gpio? Simply type gpio 10 (in my case) or only the gpio number?

Why domoticz created an switch device and not an counter? Sorry i use domoticz since 2 weeks, its new for my.

But if you can explain what to do so i use the lua script.

Now i know i needtomake scrip_watermeter.lua in
../domoticz/scripts/lua and then??

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Monday 04 September 2017 16:45

You don't need a file! Goto settings -> More Options -> events (translated from Dutch, so can be a bit different). Give it an event name, change blockly to lua and all to device. You can paste my code and change device names and idx into yours. Thats all.

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Monday 04 September 2017 18:30

Ok, ik zou dit probeeren als ik thuis ben.

I try it when im home. Thanks.

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Tuesday 05 September 2017 0:00

i give it up with the LUA sript

im now few hours busy with it...

- i exported GPIO 15 (ist my sensor connected GPIO)

- added an generic sysfs gpio devie in hardware tab

- no i have a new "open close" device in devices tab, changed it to ON/OF and renamed to GPIO15

- created an Virtual RFX counter and named it "Water" (idx = 16)


changed the values in your script and i have always that error in the log tab..
2017-09-05 10:21:12.510 Error: EventSystem: in NPN: [string "commandArray = {} ..."]:8: attempt to perform arithmetic on global 'sWaterUsage' (a nil value)
2017-09-05 10:21:21.808 Error: EventSystem: in NPN: [string "commandArray = {} ..."]:8: attempt to perform arithmetic on global 'sWaterUsage' (a nil value)
what im doing wrong ???


here is the script with my changed values
commandArray = {}

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

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


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['GPIO15'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)

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

return commandArray

im was a half night and now 3 hour busy with it, and it won't work, i'm going back to the Python Script, and live with it when the device show Offline. my head explode .......

the python script is realy acurate, 2 weeks and no one liter to much or to less, but im changed it a Little. so it is ok with that, i hope i find a way to add an 300sec counter to that script that push the value of meterstand.txt to domoticz if no detection

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Tuesday 05 September 2017 20:31

If you read my and LouiS22 posts in this topic it should work. I've described it step by step for LouiS22. He got it working. Make sure to set LUA to DEVICE instead off ALL. That generates the same sort of error in your log. Please check all device names and idx!

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Wednesday 06 September 2017 0:13

Im set LUA to DEVICE

Im checking the names again and again. I became that error message.

Maybe i test it with an other browser to start the lua script but i dont think what the problem is....


tested again, script startet, readed the Value from virtual Counter, so i restart the pi and after a simply restart i got the error again.

Trigun
Posts: 183
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 3.5877
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » Wednesday 06 September 2017 6:28

Not sure if it helps, but I see in your script some inconsistencies with the ";" at the end of the line


Sent from my iPhone using Tapatalk

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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Micha123 » Wednesday 06 September 2017 20:15

I'm still use the python script,

I made another little script that reads the watermeter.txt created by the python script, and send it to domoticz ( like the python script if npn bounce), then i added it to an cronjob that execute that script every 5 min, so my device dont show offline anymore.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests