[SOLVED] HC-SR04 and percentage value

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

[SOLVED] HC-SR04 and percentage value

#1 Post by frenesik » 26 Nov 2017, 15:28

Hello,

So I add an HC-SR04 module to tell me the tank level. I want to express this value in percent example:

HC-SR04 %
7 100
40 0

Under arduino via val = map (val, 7, 40, 100, 0); I get there but through the rules I do not know how to express this value, if someone had a tutorial or a little help thank you.

I am French and translated via google translation, I hope you will understand.
Last edited by frenesik on 27 Nov 2017, 08:53, edited 1 time in total.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: HC-SR04 and percentage value

#2 Post by grovkillen » 26 Nov 2017, 17:29

You need to put a equation to your value:

Code: Select all

121-3*%value%
https://www.mathsisfun.com/equation_of_line.html

y=m*x+b
Last edited by grovkillen on 27 Nov 2017, 08:58, edited 1 time in total.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

Re: HC-SR04 and percentage value

#3 Post by frenesik » 27 Nov 2017, 08:53

Re,
I found a lua script for domoticz that does very well. THX

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: [SOLVED] HC-SR04 and percentage value

#4 Post by grovkillen » 27 Nov 2017, 08:59

Good, but it's sometimes easier to just have the ESP Easy-unit report the correct value. I want to have as little intelligence on server-side as possible.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

Re: [SOLVED] HC-SR04 and percentage value

#5 Post by frenesik » 28 Nov 2017, 13:25

Thank you to you friend, but let's say in addition it had to remove a margin of height of the sensor compared to the level of tank.

maluko
Normal user
Posts: 112
Joined: 02 Sep 2015, 00:49

Re: HC-SR04 and percentage value

#6 Post by maluko » 28 Nov 2017, 15:13

frenesik wrote: 27 Nov 2017, 08:53 Re,
I found a lua script for domoticz that does very well. THX
Can you share the lua script?

thanks

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: [SOLVED] HC-SR04 and percentage value

#7 Post by grovkillen » 28 Nov 2017, 15:57

frenesik wrote: 28 Nov 2017, 13:25 Thank you to you friend, but let's say in addition it had to remove a margin of height of the sensor compared to the level of tank.
Still, easy in equations.

Example:

Code: Select all

121-3*(%value%-5)
Where "5" is the margin removed.

No need for a server in my opinion. :) Let's make the nodes report the correct value in the first place.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

Re: HC-SR04 and percentage value

#8 Post by frenesik » 28 Nov 2017, 16:36

maluko wrote: 28 Nov 2017, 15:13
frenesik wrote: 27 Nov 2017, 08:53 Re,
I found a lua script for domoticz that does very well. THX
Can you share the lua script?

thanks

Code: Select all

-- Nom du capteur HC-SR04
depth_sensor = 'Niveau'
-- Capteur à creer nom et id à modifier
pct_sensor = 'Niveau cuve'
pct_sensor_id = 1389
-- Hauteur total moins la marge (23 - 9 = 12)
tank_height = 12

commandArray = {}

if (devicechanged[depth_sensor]) then
   --Soustraire marge du capteur
   depth = ( otherdevices_svalues[depth_sensor] - 9 )
   pct = ( tank_height - depth ) / ( tank_height / 100 )
 commandArray[1] = {['UpdateDevice'] = pct_sensor_id .. "|0|" .. pct}
end
return commandArray
Image
Last edited by frenesik on 28 Nov 2017, 16:38, edited 1 time in total.

frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

Re: [SOLVED] HC-SR04 and percentage value

#9 Post by frenesik » 28 Nov 2017, 16:37

grovkillen wrote: 28 Nov 2017, 15:57
frenesik wrote: 28 Nov 2017, 13:25 Thank you to you friend, but let's say in addition it had to remove a margin of height of the sensor compared to the level of tank.
Still, easy in equations.

Example:

Code: Select all

121-3*(%value%-5)
Where "5" is the margin removed.

No need for a server in my opinion. :) Let's make the nodes report the correct value in the first place.

Thanks to you for your perseverance lol, I tried I do not find the same values ​​compared to the script I modify the margin: x

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: [SOLVED] HC-SR04 and percentage value

#10 Post by grovkillen » 28 Nov 2017, 17:25

This formula/equation in your device value would do the exact same thing:

Code: Select all

100-100/12*(%value%-9)
Or typed like this:

Code: Select all

(12+9-%value%)/12*100
Given that tank height is 12, and offset is 9.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

frenesik
Normal user
Posts: 10
Joined: 21 Oct 2017, 11:00

Re: [SOLVED] HC-SR04 and percentage value

#11 Post by frenesik » 28 Nov 2017, 17:36

grovkillen wrote: 28 Nov 2017, 17:25 This formula/equation in your device value would do the exact same thing:

Code: Select all

100-100/12*(%value%-9)
Or typed like this:

Code: Select all

(12+9-%value%)/12*100
Given that tank height is 12, and offset is 9.
Oh a big thank you to you friend, it works niquel good I have not understood everything of the formula but it can be great when displaying on an OLED if you want to display it in% Thank you to you super your effort;)

maluko
Normal user
Posts: 112
Joined: 02 Sep 2015, 00:49

Re: [SOLVED] HC-SR04 and percentage value

#12 Post by maluko » 29 Nov 2017, 15:10

carefull with the water vapor because i had this sensor with mysensors working fine but after 2 months stop give good values, it was the oxid by the water inside the sonar piece.

gojonny@tiscali.it
New user
Posts: 2
Joined: 04 Dec 2018, 11:23

Re: [SOLVED] HC-SR04 and percentage value

#13 Post by gojonny@tiscali.it » 04 Dec 2018, 11:33

Hello, I have tried the examples you used but I can not manage, I have a water tank when the tank is full the distance is 43cm and I would like the displayed value to be 100% when the cistern is empty measuring 70cm and I would that the measure displayed was 0% I tried several rules but I have not yet succeeded, do you think since you have arrived at the solution which formula you use? thanks in advance Giovanni

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: [SOLVED] HC-SR04 and percentage value

#14 Post by grovkillen » 04 Dec 2018, 12:34

If 70 = 0% and 43 = 100% you simply add this as a formula for the value:

(70-%value%)/(70-43)*100
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

gojonny@tiscali.it
New user
Posts: 2
Joined: 04 Dec 2018, 11:23

Re: [SOLVED] HC-SR04 and percentage value

#15 Post by gojonny@tiscali.it » 05 Dec 2018, 13:29

perfect thanks, considering the examples above I would never have arrived !!! hello thanks again

Post Reply

Who is online

Users browsing this forum: No registered users and 139 guests