Page 1 of 1
DS18B20 2 decimals
Posted: Wed Jan 08, 2014 6:53 am
by Troels
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)?
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 7:53 am
by rpdom
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'
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 8:18 am
by Troels
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?
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 8:27 am
by rpdom
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.
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 9:41 am
by AndyD
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.
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 10:03 am
by Troels
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

Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 10:27 am
by AndyD
No problems. You could replace the two sed commands with perl as well if you wanted.
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 2:14 pm
by Troels
AndyD wrote:No problems. You could replace the two sed commands with perl as well if you wanted.
Would it be more efficient / simple?
Re: DS18B20 2 decimals
Posted: Wed Jan 08, 2014 9:44 pm
by AndyD
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.
Re: DS18B20 2 decimals
Posted: Thu Jan 09, 2014 8:16 am
by rpdom
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.