# Water level in TANK

**URL:** https://discourse.nodered.org/t/water-level-in-tank/73947
**Category:** General
**Created:** [20 January 2023 09:52 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947 "2023-01-20T09:52:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pierrot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@pierrot](https://discourse.nodered.org/u/pierrot)
#### Post date: [20 January 2023 09:52 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/1 "2023-01-20T09:52:10Z")

</div>

Hello Guys,  
we install JSN-SR04T in a water tank with 120cm height .  
and below is the code but my problem is that the tank now is empty but it gives me 80% when pressing the button

```auto
[
    {
        "id": "2a29abc4.be4324",
        "type": "exec",
        "z": "e86239c2.ac7248",
        "command": "sudo python /home/pi/ultrasonic.py",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 400,
        "y": 180,
        "wires": [
            [
                "9971de04.727c6"
            ],
            [],
            []
        ]
    },
    {
        "id": "9971de04.727c6",
        "type": "function",
        "z": "e86239c2.ac7248",
        "name": "",
        "func": "var distance = parseInt(msg.payload);\nvar EmptyDist = 120 - distance;\nmsg.payload = parseInt((EmptyDist/120)*100);\n//msg.payload =(100- parseInt((temp/125)*100));\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 180,
        "wires": [
            [
                "b1617d6f.12bf9"
            ]
        ]
    },
    {
        "id": "b1617d6f.12bf9",
        "type": "ui_gauge",
        "z": "e86239c2.ac7248",
        "name": "",
        "group": "99602dee.56223",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "wave",
        "title": "",
        "label": "%",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 900,
        "y": 180,
        "wires": []
    },
    {
        "id": "a8b70e6a.6305",
        "type": "ui_button",
        "z": "e86239c2.ac7248",
        "name": "",
        "group": "99602dee.56223",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "button",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 160,
        "y": 240,
        "wires": [
            [
                "2a29abc4.be4324"
            ]
        ]
    },
    {
        "id": "99602dee.56223",
        "type": "ui_group",
        "name": "Fuel Tank",
        "tab": "94549.2567dab8",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "94549.2567dab8",
        "type": "ui_tab",
        "name": "Fuel Level",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

ultrasonic.py

```auto
import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

TRIG = 23

ECHO = 24

#print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
#print "Waiting For Sensor To Settle"
time.sleep(2)

GPIO.output(TRIG, True)

time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO)==0:
  pulse_start = time.time()
while GPIO.input(ECHO)==1:
  pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print distance
GPIO.cleanup()

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 January 2023 10:00 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/2 "2023-01-20T10:00:16Z")

</div>

When you run ultrasonic.py from the command line it reports 80% ?  
If so, you might get better support at the Raspberry Pi forum, without mentioning Node-red.

If not, can you show us what it does report - debug nodes to show output of the exec and function?

---

<div class="post-metadata">

### Author: ![pierrot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@pierrot](https://discourse.nodered.org/u/pierrot)
#### Post date: [20 January 2023 10:18 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/3 "2023-01-20T10:18:23Z")

</div>

> [@jbudd](#):
>
> When you run ultrasonic.py from the command line it reports 80% ?

no when i run the ultrasonic.py command the value is 23

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 January 2023 11:17 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/5 "2023-01-20T11:17:47Z")

</div>

Maybe there will be something useful [here](https://discourse.nodered.org/t/announce-node-red-contrib-tank-volume-beta/47101) .

---

<div class="post-metadata">

### Author: ![pierrot](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@pierrot](https://discourse.nodered.org/u/pierrot)
#### Post date: [23 January 2023 11:48 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/6 "2023-01-23T11:48:19Z")

</div>

the issue was the long distance between the rpi and tank .  
do you know how far can we install the sensor from the rpi ?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 January 2023 11:54 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/7 "2023-01-23T11:54:44Z")

</div>

> [@pierrot](#):
>
> do you know how far can we install the sensor from the rpi ?

No idea!

Use an arduino/ESP32 at the tank to read the sensor and MQTT over ethernet/wifi to communicate to Node-red on the Pi.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [24 March 2023 11:54 UTC](https://discourse.nodered.org/t/water-level-in-tank/73947/8 "2023-03-24T11:54:57Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
