Water Tank Level With Node-RED Dashboard and InfluxDB on Synology NAS Using a Raspberry Pi

by MichaelH3777 in Circuits > Raspberry Pi

892 Views, 5 Favorites, 0 Comments

Water Tank Level With Node-RED Dashboard and InfluxDB on Synology NAS Using a Raspberry Pi

graph1.jpg
graph2.jpg
Water Tank Flow.jpg

I needed a way to check the level of my water tank so that I can manage water usage.

A Raspberry Pi monitors the water level using a submersed 4-20ma sensor and sends MQTT messages over WiFi to Node-RED which is running on a Synology NAS, but it could be running on another Raspberry Pi or other hardware.

Node-RED displays graphs showing the current water level and the change in level over time. It also pulls rain fall information from local weather underground weather stations and displays rainfall history. Data is written to a InfluxDB database to allow further analysis.

Node-RED allows you to manage the data from the tank sensor, perform calcuations and inferface to a host of other devices. You could automate your irrigation based on the tank level, and integrate it with HomeKit or openHAB.

My tank is used for garden irrigation, historical rain fail data and tank levels graphs can be used to understand when I need to start reducing irrigation usage. For example not watering grass so that water is available for the vegetable garden.

The system is very robust, its been running for over two years without issues. The Raspberry Pi is exposed to the weather and temperatures of -3C to +38C without any cooling and has not stopped.

Features:

  • High Accuracy
  • Very Robust and Reliable
  • Graphs and Historical Data
  • Uses Open Source software and can be run on a wide range of hardware
  • Endless interface options via Node-RED e.g. home automation or APIs

Supplies

4-20ma-r-click.jpg
pi-3-click-shield-thickbox_default-12x.jpg
4-20ma sensor.jpeg
s-l1600.jpg
polycarbonate-enclosure-nema-flange-base-120x120x80mm.jpg
High Endurance Micro SD.jpg

Water Level Sensor:

  • 4-20 ma Water Level Sensor . Choose a depth appropriate for your use e.g. 0-1m , 0-2m. Keep in mind you are going to get better accuracy if you choose a sensor that has a lower maximum range. https://www.aliexpress.com/item/1005002707952817.html

Electronics:

  • Raspberry Pi Zero W
  • Raspberry Pi Power Supply ( run some silcone over the power pack case to water proof it)
  • MIKROE-1387 4-20ma R Click https://www.mikroe.com/4-20ma-r-click
  • MIKROE-2756 Pi 3 Click Shield https://www.mikroe.com/pi-3-click-shield
  • High Endurance Micro SD Card
  • Optional Pi Zero HDMI cable

Enclosure:

  • Waterproof Enclosure
  • Cable Glands
  • Bolts, Nuts, Stand-offs

Optional Test Equipment:

  • 4-20ma Signal Generator, It’s difficult to test 4-20ma with conventual test equipment. This makes it much easier.

Setup the Raspberry Pi

Setup the Raspberry PI ,

  • Load Deitpi https://dietpi.com onto the Pi
  • Connect the Raspberry Pi to your Wifi Network
  • Setup SSH
  • Connect the Click Sheild and R Click .
  • The R Clink goes into Slot 1 on the Click Sheid
  • Switch AN1 on the click shield is set to GPIO4 and GPIO13


Test the 4-20ma Input

4 ma.jpg
20ma.jpg

Start by making sure you can read the 4-20ma input on the Pi.

Connect to the Pi via SSH or plug a screen into the HDMI and a USB Keyboard.

Either use the water sensor or the 4-20ma Signal Generator to test the input. The black wire goes into the left screw terminal, red into the right.

  • install spidev with: sudo pip3 install spidev paho-mqtt
  • check dtparam=spi=on --> in /boot/config.txt or (set via 'sudo raspi-config')
  • run: python mcp3201.py

You will get a scrolling display of the volatge from the R Click module. Use Control C to stop.

4ma with give you 0.65V and 20ma 3.23V.

Downloads

Calibrate the Water Sensor

We need to know the MSB count when the tank is 100%. If you are lucky your tank is full and you can submerse the sensor and take a reading using the program from Step 2. 20ma corresponds to a count of about 3994.

Or you can estimate it with. count = tank depth / sensor depth range * 3994.

e.g. for a 1.6m tank and 2m sensor count = 1.6 / 2.0 * 3994 = 3594 . I would use 3600 so your tank never shows as more than 100% full.

The value needs to be saved in mqtt_tank_level_test.py and mqtt_tank_level_cron.py

If you wish you can do that now or wait until MQTT is setup. If you want to run a test and see what % full you get run mqtt_tank_level_test.py now. It will display the values before trying to send them via MQTT which we have not setup yet, so the MQTT part will fail.

Install Synology Container Images

Untitled.jpg
Untitled node red.jpg
influxdb install.jpg
mqtt log.jpg

Using the Synology Container Manager install MQTT , InfluxDB and Node-RED . Alternatively use Docker on your device to install the images.

Set the MQTT IP address in mqtt_tank_level_test.py and mqtt_tank_level_cron.py

Start the MQTT Container Manager Image

Run mqtt_tank_level_test.py on the Pi this time it should not fail .

Check the ncaerlier-mqtt log for a successful connection.

Setup InfluxDB

influx 1.jpg
influxdb 2.jpg

Node-RED will loose its graphs etc if you reboot it, so save the data to InfluxDB.

Access InfluxDB at http://YOUR_SYNOLOGY_IP:8086

  • create an organisation watertank
  • create an API key and save it for the next step
  • create a bucket watertank

Setup Node-RED

Tank Level.jpg
MQTT Test.jpg
backup.jpg
Node Red Info.jpg

Access Node-RED at http://YOUR_SYNOLOGY_IP::6335

From Manage Pallet / Install

Install the following packages

  • node-red-contrib-influxdb
  • node-red-dashboard

Import MQTT Test , Tank Level and Backup json files

Click the i ( info tab and setup MQTT and InfluxDB using the API key (token) from the previous step)

Click Deploy after making any changes to Node-RED

View the dashboard at http://YOUR_SYNOLOGY_IP:6335/ui

Optionally pull some rain data. You can either use an api e.g. from weather.com or scrape data. The attached Rain.json has examples of both.

Setup Cron on the Pi

The program mqtt_tank_level_cron.py tries to filter out noise and variations in the water level that could occur because of water flowing in or out of the tank, by taking multiple samples and averaging them.

Use crontab -e to add a command to set the tank level to Node-RED every 30 minutes.


*/30 * *  *  *  /usr/bin/python /root/mqtt_tank_level_cron.py

Downloads

InfluxDB DashBoards

influxdb.jpg

Node-RED sends the tank level and weather to the InfluxDB database. If the NAS or Pi running Node-RED restarts the Node-RED daashboard will be cleared. So saving the data in InfluxDB gives you persistant data, and you can create some nice dashboards in InfluxDB. You could also use Grafana to pull data from InfluxDB to create sophiscated dashboards.

You will need to wait for a few data points to be sent from the water tank Pi before you see anything in InfluxDB.