User avatar
Troels
Posts: 12
Joined: Wed Oct 10, 2012 11:25 am

DS18B20 2 decimals

Wed Jan 08, 2014 6:53 am

Hi

I'm using this code i a sh file:
ude_temp="$( cat /sys/bus/w1/devices/28-0000056xxxba/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}')"

echo $ude_temp

This will echo the temperatur like this (left row):

25.0625 --> 25.06
10.125 --> 10.13
0.5 --> 0.50
0 --> 0.00
-0.5 --> -0.5
-10.125 --> -10.13
-25.0625 --> -25.06

But how can I change the output to 2 decimals (right row)?

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: DS18B20 2 decimals

Wed Jan 08, 2014 7:53 am

Try this

Code: Select all

cat /sys/bus/w1/devices/28-0000057b716b/w1_slave | sed -n 's/^.*t=\([0-9]*\)\([0-9][0-9]\)[0-9]$/\1.\2/p'

User avatar
Troels
Posts: 12
Joined: Wed Oct 10, 2012 11:25 am

Re: DS18B20 2 decimals

Wed Jan 08, 2014 8:18 am

rpdom wrote:Try this

Code: Select all

cat /sys/bus/w1/devices/28-0000057b716b/w1_slave | sed -n 's/^.*t=\([0-9]*\)\([0-9][0-9]\)[0-9]$/\1.\2/p'
I have tested it now, but it only work if t= to a 4 digital number like 8375, not -8375, 83, 8 and so on... Any way to fix that?

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: DS18B20 2 decimals

Wed Jan 08, 2014 8:27 am

Ah, ok. I had not tested it with numbers with less than 4 digits or negative. I will look into it.

I assumed that t= will be at least 0000 for 0°C. Negatives should be easy, just change the first [0-9] to [-0-9].

edit: With the assistance of a bag of frozen peas I have been able to check and see that the reading does indeed start to drop trailing digits between 1°C and -1°C. That might be an issue with the w1-therm driver. Really it ought to pad out to three decimal places for consistency. There should be a way to work around it, but it might be a bit of a bodge.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: DS18B20 2 decimals

Wed Jan 08, 2014 9:41 am

You could use perl. Something like:-

Code: Select all

ude_temp="$( cat /sys/bus/w1/devices/28-0000056xxxba/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | perl -ne 'printf "%.3f", $_/1000;')"
Perl has a printf function similar to the C programming language.

Code: Select all

perl -ne 'printf "%.3f", $_/1000;'
reads real number from standard input in to $_. Divide by 1000 and then print out the number with three digits after the decimal place.

User avatar
Troels
Posts: 12
Joined: Wed Oct 10, 2012 11:25 am

Re: DS18B20 2 decimals

Wed Jan 08, 2014 10:03 am

AndyD wrote:You could use perl. Something like:-

Code: Select all

ude_temp="$( cat /sys/bus/w1/devices/28-0000056xxxba/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | perl -ne 'printf "%.3f", $_/1000;')"
Perl has a printf function similar to the C programming language.

Code: Select all

perl -ne 'printf "%.3f", $_/1000;'
reads real number from standard input in to $_. Divide by 1000 and then print out the number with three digits after the decimal place.
That works nicely! Had not thought of using perl, thank you very much.

Thank you for spending time on my problem, both of you :-)

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: DS18B20 2 decimals

Wed Jan 08, 2014 10:27 am

No problems. You could replace the two sed commands with perl as well if you wanted.

User avatar
Troels
Posts: 12
Joined: Wed Oct 10, 2012 11:25 am

Re: DS18B20 2 decimals

Wed Jan 08, 2014 2:14 pm

AndyD wrote:No problems. You could replace the two sed commands with perl as well if you wanted.
Would it be more efficient / simple?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: DS18B20 2 decimals

Wed Jan 08, 2014 9:44 pm

Troels wrote:Would it be more efficient / simple?
Possibly a little more efficient, as you could do the same with one execution of perl, rather than executing sed twice and then perl. To be honest though if you aren't measuring temperature at a high frequency it won't really matter. If it works and you are happy with it, leave it as it is.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: DS18B20 2 decimals

Thu Jan 09, 2014 8:16 am

It'll work well in perl, or you could use one invocation of sed

Code: Select all

ude_temp="$( sed -n "s/^.*t=\(-*\)\([0-9]*\).*$/\10000\2/;s/^\(-*\)0*\([0-9]*[0-9]\)\([0-9][0-9]\)[0-9]$/\1\2.\3/p" < /sys/bus/w1/devices/28-0000056xxxba/w1_slave )"
I think that will work for negative values and small values too. It might display "-0.00" for readings between -0.001 and -0.009, but I don't think the DS18B20 will give any readings in that range.

Return to “Other programming languages”