Water Level Meter - SR04M-2

Hello,
i have the above code for monitor how much water in my container using the ultrasonic sensor .
height of the container is 68 cm . when the container have about 80% of water the gauge gives me -1450% and wrong numbers . can anyone help plz

[{"id":"e86239c2.ac7248","type":"tab","label":"Water Level Monitoring","disabled":false,"info":""},{"id":"2a29abc4.be4324","type":"exec","z":"e86239c2.ac7248","command":"sudo python /home/pi/ultrasonic.py","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":400,"y":180,"wires":[["9971de04.727c6"],[],[]]},{"id":"1bbcb84e.e21008","type":"ui_text","z":"e86239c2.ac7248","group":"99602dee.56223","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":400,"wires":[]},{"id":"9971de04.727c6","type":"function","z":"e86239c2.ac7248","name":"","func":"var distance = parseInt(msg.payload);\nvar EmptyDist = 66 - distance;\nmsg.payload = parseInt((EmptyDist/66)*100);\n//msg.payload =(100- parseInt((temp/125)*100));\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":180,"wires":[["1bbcb84e.e21008","b1617d6f.12bf9"]]},{"id":"b1617d6f.12bf9","type":"ui_gauge","z":"e86239c2.ac7248","name":"","group":"99602dee.56223","order":1,"width":0,"height":0,"gtype":"wave","title":"gauge","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","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","z":"","name":"Tank1","tab":"94549.2567dab8","order":1,"disp":true,"width":"6","collapse":false},{"id":"94549.2567dab8","type":"ui_tab","z":"","name":"Water Level","icon":"dashboard","disabled":false,"hidden":false}]

Please put a debug node on the output of the exec node so we can see what data is going into the function node.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

The code of your ultrasonic.py might be useful too.

this the code of ultrasonic.py
and for the debug is the container empty the debug will return 68

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

1/13/2022, 3:21:11 PMnode: 33be6e43.ccbbd2msg.payload : string[6]

"63.49↵"

1/13/2022, 3:21:37 PMnode: 33be6e43.ccbbd2msg.payload : string[6]

"63.01↵"

Did eithor of those values cause the error? if not, what value causes the error?

the value is correct when the container is empty . while the container have 50 or 80 % of water wrong values appeared such as -1456%

But do you know what the actual payload value is returned from the exec node that causes the -1456% reading?

1/13/2022, 6:07:41 PMnode: 33be6e43.ccbbd2msg.payload : string[5]

string[5]

52.1

and the value after the function 58% while the container have at least 90% water

1029.5 value from the debug

Ok, so you are getting some false readings. Why not add a switch node after the exec node to only pass in valid readings. i.e. only pass on readings under a certian value.

or you could change the pythn code to see if a reading was out of wack and delay for a bit and take another reading.

is there anything wrong with the python code ?

or do u have any suggestion for another code to reach my goal and check my container level

I think the sensor sometimes sends back screwy readings so you can address that in the python code or in NR by examining the value and tossing out readings that are out of range.

If you dig thru the data sheets and/or google, you might find information about invalid readings.

i tried to put an object which is 5 cm far from it and the result was 1024
even if i changed the sensor same result .
anw thanks for your help

If you are using the Non Waterpfroof version (rather than the JSR04) then you will end up (quickly) with issues as water condenses on the Sender and receiver units.

Also - presumably you are running this from a Pi - make sure you have the 3.3v version of the sensor - there are two of them a 5v and a 3.3v

Lastly if you go for the Waterproof sensor then it has a dead spot due to its design of about 20cm from the sensor unit itself

In my case using the waterproof units (from an ESP8266 running Tasmota) i get the readinds in NR and throw away anything that is more than 10> greater than the average of the last 10 readings

Craig

Craig

1 Like

hello,
yes iam running the waterproof ultrasonic sr04m-2
can you send me the code ur using with nr

Yes as i said - i run mine through Tasmota on an ESP8266 and then use MQTT to pass the results to NR.

Craig

Hello, I am using the same sensor for my watertank and had initially a lot of reading errors especially when the tank was nearly full.

I'm using the sensor on an ESP running Tasmota but the code you shared is following the same approach. The ESP and the sensor is powered by a 12v Solar System with battery .

Tasmota provides you all the data you need out of the box .

Just connect the sensor to the pins and configure as below

The issue with this sensor is that it gives a WRONG/Incorrect reading when the distance from sensor to water is less the 21cm (it might be as well less than 23cm depending on tolerance of your sensor!)

I'm using this 3D printed case for my sensor and have it mounted above the maximum water level (~22cm above water).

The 3D print was rough in the inside of the cone and I used a 250 sanding paper to make it very smooth which improved the reading quality significantly (without sanding I still got false readings)

Now my readings are very stable for a while. The stepwise reduction is when my daily irrigation system is running. Than you see a spike at 01/05 where it goes to maximum, here we had a lot of rain which filled the tank to 100%. Than we had a few rainy days where no irrigation was running upto 01/12 where the irrigation started again.

Below are the RAW-Data from Tasmota for the sensor(no filter), and you can see that the readings are very stable

Below is my flow in which I use a filter to eliminate false readings

[{"id":"50e1ebdab3029328","type":"tab","label":"Tank","disabled":false,"info":"","env":[]},{"id":"9bd1a940ed96ee45","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1540,"y":120,"wires":[]},{"id":"1c665f48aef699cf","type":"tank-volume","z":"50e1ebdab3029328","name":"","tankType":"cone_top","inputField":"payload","outputField":"payload","measurement":"above","inputUnit1":"cm","inputUnit2":"l","outputUnit":"l","topLimit":0,"bottomLimit":0,"diameter":0,"length":0,"width":0,"height":0,"length2":0,"width2":0,"height2":0,"coneHeight":"50","cylinderHeight":"119","diameterTop":"30","diameterBottom":"183","customTable":[],"x":1320,"y":80,"wires":[["9bd1a940ed96ee45","410fb8767fb446ed","9def827cb68ebe5e"]]},{"id":"410fb8767fb446ed","type":"ui_level","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":1,"width":3,"height":3,"name":"","label":"","colorHi":"#000fe6","colorWarn":"#00ff59","colorNormal":"#d8964b","colorOff":"#ff0000","min":0,"max":"3440","segWarn":"2000","segHigh":"3000","unit":"L","layout":"sv","channelA":"","channelB":"","decimals":0,"animations":"soft","shape":"3","colorschema":"fixed","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":false,"hideValue":false,"tickmode":"auto","peakmode":false,"property":"payload.filledVolume","peaktime":3000,"x":1530,"y":40,"wires":[]},{"id":"5d6fd1cd656d0b67","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank/STATUS10","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":190,"y":80,"wires":[["93aa3619443bfaa9"]]},{"id":"578af7c09a4fe7b3","type":"inject","z":"50e1ebdab3029328","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"300","crontab":"","once":true,"onceDelay":"60","topic":"cmnd/WaterTank/Status","payload":"10","payloadType":"str","x":180,"y":240,"wires":[["2294e2bba31a121a","86a1d90c5e385057"]]},{"id":"9898ad5a44ead344","type":"link out","z":"50e1ebdab3029328","name":"","links":["212504ad.d93e5c","83260501.ed4468"],"x":995,"y":240,"wires":[]},{"id":"16c8130f72b9d599","type":"ui_button","z":"50e1ebdab3029328","name":"","group":"e1c153f31de947f3","order":8,"width":0,"height":0,"passthru":false,"label":"Check","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"0","payloadType":"str","topic":"cmnd/WaterTank/Status","topicType":"str","x":250,"y":200,"wires":[["86a1d90c5e385057","2294e2bba31a121a"]]},{"id":"86a1d90c5e385057","type":"change","z":"50e1ebdab3029328","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"WaterTank/status","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":200,"wires":[["9898ad5a44ead344"]]},{"id":"2294e2bba31a121a","type":"delay","z":"50e1ebdab3029328","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":600,"y":240,"wires":[["9898ad5a44ead344"]]},{"id":"93aa3619443bfaa9","type":"json","z":"50e1ebdab3029328","name":"","property":"payload","action":"","pretty":false,"x":410,"y":80,"wires":[["ba43f34573f6edfc","9c6df35259e4d27e"]]},{"id":"ba43f34573f6edfc","type":"change","z":"50e1ebdab3029328","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.StatusSNS.SR04.Distance","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":80,"wires":[["a728bb6baefea3db","6c56968812dd883f"]]},{"id":"6c56968812dd883f","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":5,"width":0,"height":0,"name":"","label":"Level","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":40,"wires":[]},{"id":"fa78d4cad29652fe","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":6,"width":0,"height":0,"name":"","label":"IP","format":"{{msg.payload.StatusNET.IPAddress}}","layout":"row-spread","className":"","x":590,"y":340,"wires":[]},{"id":"528fd9013e1ee4c7","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank/STATUS5","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":190,"y":340,"wires":[["efdc7d4357309cd4"]]},{"id":"efdc7d4357309cd4","type":"json","z":"50e1ebdab3029328","name":"","property":"payload","action":"","pretty":false,"x":410,"y":340,"wires":[["fa78d4cad29652fe","c0b99e6c6d6e985b"]]},{"id":"c0b99e6c6d6e985b","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":380,"wires":[]},{"id":"ab7b642f31ea4dd8","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank2/PIR1","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":200,"y":440,"wires":[["434ee9908e7e3965"]]},{"id":"434ee9908e7e3965","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":440,"wires":[]},{"id":"9c6df35259e4d27e","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":7,"width":0,"height":0,"name":"time","label":"time","format":"{{msg.payload.StatusSNS.Time}}","layout":"row-spread","className":"","x":590,"y":120,"wires":[]},{"id":"9def827cb68ebe5e","type":"influxdb out","z":"50e1ebdab3029328","influxdb":"a5b54fc0.d8c14","name":"WaterTank","measurement":"WaterTank","precision":"","retentionPolicy":"","database":"","retentionPolicyV18Flux":"","org":"","bucket":"","x":1550,"y":80,"wires":[]},{"id":"a728bb6baefea3db","type":"function","z":"50e1ebdab3029328","name":"< 23 ?","func":"let val=parseFloat(msg.payload);\n\nif (val<=23) val=23;\nmsg.payload=val\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":80,"wires":[["64ea5cc51a0ec318"]]},{"id":"64ea5cc51a0ec318","type":"switch","z":"50e1ebdab3029328","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"23","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":970,"y":80,"wires":[["ba9034baeb0aaa00"],["b8a224846d219c50"]]},{"id":"b8a224846d219c50","type":"change","z":"50e1ebdab3029328","name":"get old","rules":[{"t":"set","p":"payload","pt":"msg","to":"Old_Value","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":100,"wires":[["1c665f48aef699cf"]]},{"id":"ba9034baeb0aaa00","type":"change","z":"50e1ebdab3029328","name":"set old","rules":[{"t":"set","p":"Old_Value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":60,"wires":[["1c665f48aef699cf"]]},{"id":"c8169110aa3c9ea7","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":2,"width":3,"height":1},{"id":"c2e2d590ddd99e21","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":3,"width":3,"height":1},{"id":"8a0453646a4267f1","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":4,"width":3,"height":1},{"id":"e1c153f31de947f3","type":"ui_group","name":"Water1","tab":"51cd696686651003","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ab8bdac8.bb6898","type":"mqtt-broker","name":"MQTT-org","broker":"192.168.2.199","port":"1883","clientid":"Node-Red","usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"a5b54fc0.d8c14","type":"influxdb","hostname":"192.168.2.202","port":"8086","protocol":"http","database":"demo","name":"","usetls":false,"tls":"","influxdbVersion":"1.x","url":"http://192.168.2.195:8086","rejectUnauthorized":false},{"id":"51cd696686651003","type":"ui_tab","name":"Tank","icon":"dashboard","order":27,"disabled":false,"hidden":false}]

as said, the 3D printed mount did do the trick after it was smooth at the inside !!

good luck

thank you i will try to figure it out