Watermeter doesn't work

Moderator: leecollings

Post Reply
JarekP
Posts: 15
Joined: Monday 16 May 2016 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Watermeter doesn't work

Post by JarekP »

Guys, I'm still a newbie and probably I made a mistake or didn't do something important, please help with investigation :)

I have watering system managed by domoticz, I'd like to add watermeter. As a hardware I have popular flow sensor from Aliexpress, powered by 3.3V, output is connected to GPIO4 (physical pin 7 on GPIO). Flowmeter is working I checked by oscilloscope, that on this pin there is a sq vawe, freq proportional to flow, so hw works perfectly.

Next I adapted a description found here on this forum:

viewtopic.php?f=36&t=10272

My adaptation: I deleted Gas section and changed ELEC into WATER, added a WATER.GPIO=4 and WATER.IDX=5 (according to idx of my dummy water counter in domoticz):

Code: Select all

#!/usr/bin/env python

import time
import json
import urllib2
import threading
import logging
import logging.handlers
import os
from gpiozero import DigitalInputDevice

GET_URL = 'http://127.0.0.1:8080/json.htm?type=devices&rid=%d'
SET_URL = 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=%d&svalue=%d'
WATER_DELTA = 0
WATER_IDX = 5
WATER_GPIO = 4
WATER_COUNTER_LOCK = threading.Lock()
WATER_LAST_TIME = 0
WATER_POST_TIME = 0

def water_intr():
    tme = time.time()
    global WATER_DELTA
    global WATER_LAST_TIME
    global WATER_POST_TIME
    with WATER_COUNTER_LOCK:
        WATER_LAST_TIME = tme
        if WATER_POST_TIME == 0:
            WATER_POST_TIME = WATER_LAST_TIME
        else:
            WATER_DELTA += 1
    logging.debug( 'Water counter tick: %d' % WATER_DELTA )
    
def main():
    global WATER_DELTA
    global WATER_LAST_TIME
    global WATER_POST_TIME
    global WATER_COUNTER

    syslog = logging.handlers.SysLogHandler(address='/dev/log', facility='local1')
    syslog.setFormatter(logging.Formatter('local_sensors.py: %(levelname)s %(message)s'))
    logging.getLogger().addHandler(syslog)
    logging.getLogger().setLevel(logging.INFO)

    while True:
        try:
            res = json.load(urllib2.urlopen(GET_URL % WATER_IDX))
            if res['status'] != 'OK':
                raise Exception('Domoticz json error')
            break
        except Exception as e:
            logging.warning( e )
        time.sleep(30.0)

    WATER_COUNTER = int(float(res['result'][0]['Data'][:-4]) * 1000)
#    WATER_COUNTER = <Your initial count here * 1000, don't forget to remove after Domoticz updated!>
    logging.info( 'Current water counter is: %d' % WATER_COUNTER )

    waterSensor = DigitalInputDevice(WATER_GPIO, pull_up=True)
    waterSensor.when_deactivated = water_intr

    os.nice(-20)

    logging.info('Polling loop starting')

    while True:
        time.sleep(60)

        with WATER_COUNTER_LOCK:
            if WATER_LAST_TIME > WATER_POST_TIME:
                WATER_LOAD = WATER_DELTA * 3600 / ( WATER_LAST_TIME - WATER_POST_TIME )
            else:
                WATER_LOAD = 0

            WATER_COUNTER += WATER_DELTA

            WATER_DELTA = 0
            WATER_POST_TIME = WATER_LAST_TIME

        if WATER_LOAD != 0:
            try:
                res = json.load(urllib2.urlopen((SET_URL+';%d') % (WATER_IDX, int(WATER_LOAD), WATER_COUNTER)))
                if res['status'] != 'OK':
                    raise Exception('Domoticz json error')
                logging.info('Water load %.2f counter %d' % (WATER_LOAD, WATER_COUNTER) )
            except Exception as e:
                logging.warning( e )

if __name__=="__main__":
    main()  

This script I copied into newly created file wodomierz.sh, made this file by chmod as executable and tried to run it. Unfortunately, after run this script my telnet session is frozing. No answer from raspberry. It works, I can use my domoticz at this time, also I can run parallel telnet session, but this one is not answering. Also in domoticz there is no reaction of my counter, it shows still 0.

So, where I'm wrong, or what should I do else? My domoticz is a new installation, maybe there should be something installed else?
RidingTheFlow
Posts: 72
Joined: Friday 11 March 2016 18:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Essex, UK
Contact:

Re: Watermeter doesn't work

Post by RidingTheFlow »

Not sure what you mean by session frozen, but such script generally intended to run as background process (e.g. via cron or & )
JarekP
Posts: 15
Joined: Monday 16 May 2016 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter doesn't work

Post by JarekP »

Ok, but in my imagination it should work that way: I'm running a script, receiving any confirmation or not and have a prompt again.
In my case when I put a command:

pi@raspberrypi:~$ ./wodomierz.sh

there is nothing after it. No cursor, no prompt, only as I can do is closing this telnet session and run it again.

As I wrote I'm not so familiar with linux and raspberry, so maybe I should do something obvious and trivial with this script but I don't know about it, so I'd like to ask about step by step :)

cron, etc - in my understanding such tool is necessary to automatic calling of this script, but it also should work after manual run as I'm trying, isn't it?
BobS
Posts: 15
Joined: Friday 05 June 2015 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Rotterdam, NL
Contact:

Re: Watermeter doesn't work

Post by BobS »

Looks like your script contains a while True loop so it will run forever and not return to the command prompt.
JarekP
Posts: 15
Joined: Monday 16 May 2016 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter doesn't work

Post by JarekP »

As I wrote, I used script published on forum by @RidingTheFlow, assume it is working, especially that some of colleagues according to their posts also are using it :)
So, I think there are two possibilities:
- I destroyed something during making changes (delete "gas" section and change "elec" into "water")
- it shouldn't be run manually as I'm trying, but here I really need step by step, please, what it should be made :)
JarekP
Posts: 15
Joined: Monday 16 May 2016 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Watermeter doesn't work

Post by JarekP »

Some new facts: when I'm trying to run it with &, it gives an answer:

Code: Select all

pi@raspberrypi:~$ ./wodomierz.sh &
[1] 1000
But still no changes on my dummy counter in domoticz, it still shows 0.
Also I checked syslog, at the time, when I did my tries (tried to run script and enabled water flow) syslog is full of following:

Code: Select all

May 23 18:05:39 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:05:44 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:05:46 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:05:48 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:05:50 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:06:06 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:06:07 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:06:09 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
May 23 18:06:14 raspberrypi local_sensors.py: WARNING HTTP Error 401: Unauthorized
RidingTheFlow
Posts: 72
Joined: Friday 11 March 2016 18:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Essex, UK
Contact:

Re: Watermeter doesn't work

Post by RidingTheFlow »

Looks like your domoticz has no allowed unauthenticated access for localhost. Check that 127.0.0.* is in local networks in domoticz and you can access it from pi without entering password.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest