Based on a topic on the Domotiz forum and the new setup from my side regarding the readings from my new smart meter (DSMR5) and a need to have solution too for the watermeter. I want to get rid of the RFXmeter with infrared sensor, so this is a good opportunity for switching hardware. My Watermeter has a small grey reflective area on one of the readers, so in theory I should do something with the reflective part of it. The reflective part is showing for every 1 liter of water:

Aquadis-WML-P1

What do you need?

  • A working Raspberry Pi. In my case I use an older Pi 2B. For my setup I installed the Raspbian Stretch Lite ISO: https://www.raspberrypi.org/downloads/raspbian on a 8GB micro SD card.
  • 3 jumper wires (female) to connect the NPN sensor to the GPIO on the Pi.
  • One inductive proximity sensor, like LJ12A3-4-Z/BX sensor N/O NPN 4mm. 3 wires. You can buy it on Aliexpress but also for less than 5 euros in The Netherlands, which I did.

LJ12A3-4-Z-BX

Connecting wires

The NPN sensor have 3 wires:

Blue= GND (pin 6)
Brown= 5V (pin 2 or 4)
Black = GPIO (a GPIO input, for example GPIO4 at pin 7)

The NPN sensor operates normally between 6-36 Volt, but for this purpose the 5Volt from the Pi is enough for unknown reason…

Pi2Bpinout

 

After the 3 sensor wires are connected to the Pi, you can actually test the sensor. On the sensor there is red led (next to the cable connection). If you hold a piece of metal (knife, screwdriver) within 4 mm of the blue top, the red led should be activated.

 

Homeseer configuration

I’ve chosen for a Homeseer counter that will be increased. It’s a different approach than that is shown in the Domoticz topic, but in the beginning I want to see if the readings are accurate and later on I can change the whole setup if needed.

In Homeseer, create a new counter via “Tools” – “Global variables, Timers & Counters”, like this:

Watermeter-counter

Because a counter can’t be controlled by JSON, nor via the RestAPI, an event is necessary to increase the counter. The Python script on the Pi will call this event to control the counter.

As visible in the screenshot below, the event groupname = “Energy”. This name could be different in your setup, but is necessary in the Python script later:

Watermeter-event

Tto call this particularly event, the following command has to be used:

http://homeseer-ip:port/JSON?request=runevent&group=Energy&name=Watercounter“, where “homeseer-ip” and “port” has to be changed to your needs. Test this url in your bowser to see if the createdevent is increasing the counter.

 

Thoughts that can be done, but are not necessary:

  • For copying files to the Pi, and even test everything I use the free tool “MobaXterm Personal Edition“. Use the “sudo raspi-config” console command ithe enable SSH access in the interface first.
  • In the raspi-config, set your timezone correctly (via Localisation Options – Change Timezone)
  • In the raspi-config, enable console auto login (via Boot Options – Desktop/CLI – Console Autologin Text console).

 

On your local computer create the “watermeter.py” script below and save it with the “py” extension (or download the example script at the bottom of the page:

#!/usr/bin/env python
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import urllib2
import datetime
# = GPIO 4 =
TrackingPin = 7

def button_callback(channel):
datetime.datetime.now().strftime(“%a, %d %B %Y %H:%M:%S”)
print(datetime.datetime.now())
print(“1L of water is used”)
url = “http://homeseer-ip:port/JSON?request=runevent&group=Energy&name=Watercounter”
f = urllib2.urlopen(url)
print f.read()
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(TrackingPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin TrackingPin to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(TrackingPin,GPIO.RISING,callback=button_callback) # Setup event on pin TrackingPin rising edge
message = input(“Script started. Press enter to quit\n\n”) # Run until someone presses enter
GPIO.cleanup() # Clean up

 

In my case have copied the script to the following location: ~/home/pi/. Via the MobaXterm tool it’s easy drag & drop. In case you want to store the Python script somwhere online, you can use the following command on the Pi to grab the file:

wget http://<domain>/watermeter.py

When the Pi is having a reboot, we want to have the script started automatically:
sudo nano /etc/profile
Scroll to the bottom of the file and add the following line of code (where “/home/pi/watermeter.py” is the path to your script):
sudo python /home/pi/watermeter.py
Type “Ctrl+X” to exit, then “Y” to save followed by “Enter” twice.
P.S: If you don't want to see any script output on the console screen, you can replace above line with:

sudo python /home/pi/watermeter.py &

 

To test the changes, give the Pi a reboot with command:

sudo reboot

If everything is set correct, the following line will be visible in the console after reboot:

Watermeter-scriptstarted

When you now hold a piece of metal within the 4mm sensor range, the led will activate, and in the console screen the following lines should be visible: Watermeter-pulsedetected

The “Response:ok” is a confirmation that the JSON call to Homeseer is done correctly. So if we check the status of the watermeter counter, it should be increaed with 1:

Watermeter-counternew

Which also means the event ran correctly as well.

 

Final setup for watermeter and NPN sensor

Aquadis-WML-P1-result

 

Downloads:

  Watermeter.py (1.0 KiB, 2,638 hits)

 

Related Posts

Privacy Preference Center