Page 8 of 20

Water meter readout with raspberry and Inductive NPN sensor

Posted: Friday 13 October 2017 20:48
by Trigun
pvangorp wrote: Friday 13 October 2017 19:36 Should be sWaterUsage in keep Alice part!
alright, I tested it with using the line in a browser and that works! the virtual device did change, great!

but back in the script it does nog change, I've also tried the "Commandarray... etc" no luck either.

any ideas?

this is my setup:

Code: Select all

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


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'] = '8|0|'..sWaterUsageTot..''
  os.execute('wget http://IPADDRESS:PORT/json.htm?type=command&param=udevice&idx=IDX&svalue='..sWaterUsageTot..' -O /dev/null')
else
    -- Keep a live device
    if (timedifference(otherdevices_lastupdate["Watermeter"]) > 300)
    then
        print("Water usage is still " .. sWaterUsage / 1000 .. "m3 ");
        commandArray['UpdateDevice'] = '8|0|'..sWaterUsage..'' 
        os.execute('wget http://IPADDRESS:PORT/json.htm?type=command&param=udevice&idx=IDX&svalue='..sWaterUsage..' -O /dev/null')   
    end
end

return commandArray

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 20:58
by pvangorp
Do you get some sort of error in the log? Looks oke to me. Is the ip included in your system settings network e.g. 127.0.0.*;192.168.*.* of both devices? Executing in browser should be the same as in script. You can use a print for debuging instead of os.execute. Otherwise I need to create a simmilar environment to test it in real.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 21:11
by Trigun
pvangorp wrote: Friday 13 October 2017 20:58 Do you get some sort of error in the log? Looks oke to me. Is the ip included in your system settings network e.g. 127.0.0.*;192.168.*.* of both devices? Executing in browser should be the same as in script. You can use a print for debuging instead of os.execute.
I can see the lua script being triggerd but the master device is not being updated.
I don't see any errors at all. I'try the print option.

thank you!

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 21:21
by Trigun
Trigun wrote: Friday 13 October 2017 21:11
pvangorp wrote: Friday 13 October 2017 20:58 Do you get some sort of error in the log? Looks oke to me. Is the ip included in your system settings network e.g. 127.0.0.*;192.168.*.* of both devices? Executing in browser should be the same as in script. You can use a print for debuging instead of os.execute.
I can see the lua script being triggerd but the master device is not being updated.
I don't see any errors at all. I'try the print option.

thank you!
Strange, it seems nothing happens. is this what you meant with "print"?

Code: Select all

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


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'] = '8|0|'..sWaterUsageTot..''
  print('wget http://192.168.1.94:8080/json.htm?type=command&param=udevice&idx=157&svalue='..sWaterUsageTot..' -O /dev/null')
else
    -- Keep a live device
    if (timedifference(otherdevices_lastupdate["Watermeter"]) > 300)
    then
        print("Water usage is still " .. sWaterUsage / 1000 .. "m3 ");
        commandArray['UpdateDevice'] = '8|0|'..sWaterUsage..'' 
        print('wget http://192.168.1.94:8080/json.htm?type=command&param=udevice&idx=157&svalue='..sWaterUsage..' -O /dev/null')   
    end
end

return commandArray
it doen't show anything at all in the log file. it's just like the line is not being used.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 21:27
by pvangorp
What happens when you swap the commandarray and weet line order. The print above the commandarray is in the log?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 21:33
by Trigun
pvangorp wrote: Friday 13 October 2017 21:27 What happens when you swap the commandarray and weet line order. The print above the commandarray is in the log?
something happend.

Code: Select all

2017-10-13 21:29:06.049 LUA: Water usage is still 173.005m3
2017-10-13 21:29:06.050 LUA: wget http://192.168.1.94:8080/json.htm?type=command¶m=udevice&idx=157&svalue=173005 -O /dev/null
2017-10-13 21:29:06.067 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua
but unfortunately nothing happens on the other device.

i just found out that I never see the following print:

Code: Select all

print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ");
is this correct or should it only set once, in the beginning?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 23 October 2017 10:19
by LouiS22
Trigun wrote: Friday 13 October 2017 21:33
pvangorp wrote: Friday 13 October 2017 21:27 What happens when you swap the commandarray and weet line order. The print above the commandarray is in the log?
something happend.

Code: Select all

2017-10-13 21:29:06.049 LUA: Water usage is still 173.005m3
2017-10-13 21:29:06.050 LUA: wget http://192.168.1.94:8080/json.htm?type=command¶m=udevice&idx=157&svalue=173005 -O /dev/null
2017-10-13 21:29:06.067 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua
but unfortunately nothing happens on the other device.

i just found out that I never see the following print:

Code: Select all

print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ");
is this correct or should it only set once, in the beginning?
Have you found the bug yet?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 25 October 2017 18:07
by pvangorp
New feature tip. You can hide the dummy “GPIO Watermeter” by putting a $ in front of the name (don’t forget to change the script aswell) or put the device in the “hidden devices” Floor plan.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Sunday 29 October 2017 20:26
by Draakje
Hobbybob wrote: Sunday 13 August 2017 20:36 Okay, I've tried to read up on the information about this, and I came up with the following wiring-scheme. As said, I am not very good at reading diagrams with symbols, so I made something a litte more visual for myself.

Can anybody with a little more knowledge than me check if this would be a good way? I calculated the 1kOhm and 2kOhm on the site oohlaf mentioned, and fiddled with the Ohms to get two values that I can actually buy, and produces as close to 3.3V as possible.

Schakeling watermeter.jpg
Ok I would like to have some more information about this setup..

I am about to connect the same sensor on my Raspberry pi3.
I have read about the voltage being 3,3V but My raspberry has (2) 5 volt connections on the GPIO.

So my real question is.. do I need the resistors shown in the diagram? as I am putting 5 volt in.....

Many thanks for any info.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Sunday 29 October 2017 20:58
by Trigun
LouiS22 wrote: Monday 23 October 2017 10:19
Trigun wrote: Friday 13 October 2017 21:33
pvangorp wrote: Friday 13 October 2017 21:27 What happens when you swap the commandarray and weet line order. The print above the commandarray is in the log?
something happend.

Code: Select all

2017-10-13 21:29:06.049 LUA: Water usage is still 173.005m3
2017-10-13 21:29:06.050 LUA: wget http://192.168.1.94:8080/json.htm?type=command¶m=udevice&idx=157&svalue=173005 -O /dev/null
2017-10-13 21:29:06.067 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua
but unfortunately nothing happens on the other device.

i just found out that I never see the following print:

Code: Select all

print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ");
is this correct or should it only set once, in the beginning?
Have you found the bug yet?
Hi louiS22,

Haven't found it yet as the code seems correct.
I was hoping pvangorp could help us out.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 4:24
by pvangorp
Draakje wrote: Sunday 29 October 2017 20:26
Hobbybob wrote: Sunday 13 August 2017 20:36 Okay, I've tried to read up on the information about this, and I came up with the following wiring-scheme. As said, I am not very good at reading diagrams with symbols, so I made something a litte more visual for myself.

Can anybody with a little more knowledge than me check if this would be a good way? I calculated the 1kOhm and 2kOhm on the site oohlaf mentioned, and fiddled with the Ohms to get two values that I can actually buy, and produces as close to 3.3V as possible.

Schakeling watermeter.jpg
Ok I would like to have some more information about this setup..

I am about to connect the same sensor on my Raspberry pi3.
I have read about the voltage being 3,3V but My raspberry has (2) 5 volt connections on the GPIO.

So my real question is.. do I need the resistors shown in the diagram? as I am putting 5 volt in.....

Many thanks for any info.
Use a zener diode, as I mentioned before. The zener diode is for protection. It prevents that the GPIO pin get more then 3.3v. https://www.kiwi-electronics.nl/zenerdiode-3v3-500ma

You can also use a logic level converter from 5v to 3.3v, more accurate, but zener is cheaper.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 4:32
by pvangorp
I created a test environment for your situation.

As you can see in your dump something goes wrong!!!

Code: Select all

2017-10-13 21:29:06.049 LUA: Water usage is still 173.005m3
2017-10-13 21:29:06.050 LUA: wget http://192.168.1.94:8080/json.htm?type=command¶m=udevice&idx=157&svalue=173005 -O /dev/null
2017-10-13 21:29:06.067 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua
I looked into it and &param is translated wrong by Domoticz. (type=command¶m=udevice)

Change the order of the url parameters and use:

Code: Select all

json.htm?param=udevice&type=command&idx=......
and it will work.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 6:53
by philchillbill
LouiS22 wrote: Wednesday 11 October 2017 10:35 Is there any way to transfer these values from a slave PI to a Master PI? (Virtual counters don't get updated this way :( ).
I'm just curious why everybody seems to think that the slave-pi in this situation needs to run Domoticz? I have a slave-pi in my house to read out a quad current meter that I have installed in my meter closet (better English than meter cast :mrgreen: ) to give more granularity per breaker-group than my p1 smart-meter does. I just update the counters/meters/switches in my main (and only) Domoticz instance (running on a NUC incidentally, not a pi) using JSON.

Code: Select all

    if (pwr[i] != oldpwr[i] ):
      idx=str(meters[i])
      url = 'http://192.168.1.12:8080/json.htm?type=command&param=udevice&nvalue=0&idx='+idx+'&svalue='+pwr[i]
      print "Meter | "+time.ctime()+"\n "+url
      try:
        request = urllib2.Request(url)
        response = urllib2.urlopen(request)
      except urllib2.HTTPError, e:
        print e.code; time.sleep(60)
      except urllib2.URLError, e:
        print e.args; time.sleep(60)


is an example of how I update 'remotely' without having to have Domoticz locally on the remote machine. What's the benefit of having Domoticz on the slave-pi - might be easier than DDNS if the master and slave pi's are not on the same WiFi?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 7:41
by Draakje
pvangorp wrote: Monday 30 October 2017 4:24
Draakje wrote: Sunday 29 October 2017 20:26
Hobbybob wrote: Sunday 13 August 2017 20:36 Okay, I've tried to read up on the information about this, and I came up with the following wiring-scheme. As said, I am not very good at reading diagrams with symbols, so I made something a litte more visual for myself.

Can anybody with a little more knowledge than me check if this would be a good way? I calculated the 1kOhm and 2kOhm on the site oohlaf mentioned, and fiddled with the Ohms to get two values that I can actually buy, and produces as close to 3.3V as possible.

Schakeling watermeter.jpg
Ok I would like to have some more information about this setup..

I am about to connect the same sensor on my Raspberry pi3.
I have read about the voltage being 3,3V but My raspberry has (2) 5 volt connections on the GPIO.

So my real question is.. do I need the resistors shown in the diagram? as I am putting 5 volt in.....

Many thanks for any info.
Use a zener diode, as I mentioned before. The zener diode is for protection. It prevents that the GPIO pin get more then 3.3v. https://www.kiwi-electronics.nl/zenerdiode-3v3-500ma

You can also use a logic level converter from 5v to 3.3v, more accurate, but zener is cheaper.
And the zener diode goes on the input (GPIO pin 40) ?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:28
by pvangorp
To your input GPIO of your Pi. Check your zener diode with a voltage meter. It does mater how you connect it. If wrong it stil gives 5v to your pi!

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:33
by Draakje
pvangorp wrote: Monday 30 October 2017 11:28 To your input GPIO of your Pi. Check your zener diode with a voltage meter. It does mater how you connect it. If wrong it stil gives 5v to your pi!
Ok I am not an expert in zenerdiodes. So what is the correct way? how do I check it with a voltage meter?
what to look for?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:35
by pvangorp
The output is about 3volt or about 5volt when wrong way connected.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:36
by Draakje
pvangorp wrote: Monday 30 October 2017 11:35 The output is about 3volt or about 5volt when wrong way connected.
Ok so test when the sensor is connected (but not to the GPIO)

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:38
by pvangorp
Yep

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 30 October 2017 11:41
by Draakje
pvangorp wrote: Monday 30 October 2017 11:38Yep
Many thanks! will follow up :)