rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Mon Jan 18, 2016 9:00 pm

TY tons!

That explains a lot, of course.
Much appreciated - I am starting to like bash more and more. Would like ot even more if it could extract values from a python script, but, can't ask for everything!

Again, TY much,
Rainer

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Tue Jan 19, 2016 4:31 am

rfeyer wrote: ...
These are the notes I have so far on the grep command:
roomtemp=$(cat /sys/bus/w1/devicecs/28-0000072f9386/w1_slave | grep -E -o ".{0,0}=.{0,5}" | cut -c 3-)
Hi.

Another way to get the temperature is to use the two 'commands' grep and awk

grep allows you to get only the line containing the temperature:
cat /sys/bus/w1/devices/28-0000072f9386/w1_slave | grep "t="
returns the line:
"27 01 55 00 7f ff 0c 0c 8e t=18437"

awk normally separates a line into fields based on spaces, but you can change the default behavior and make any text into the field separator. If you use 't=' as the field separator, the temperature value will be the second field.

So sending "27 01 55 00 7f ff 0c 0c 8e t=18437" to awk like this:
echo "27 01 55 00 7f ff 0c 0c 8e t=18437" | awk '{FS = "t=" ;$0=$0; print $2}'
returns '18437'

As the pipe character '|' passes the output of one command to the input of the next, you can do this:
cat /sys/bus/w1/devices/28-0000072f9386/w1_slave | grep "t=" | awk '{FS = "t=" ;$0=$0; print $2}'
which outputs just the temperature value 18437
or
cat /sys/bus/w1/devices/28-0000072f9386/w1_slave | grep "t=" | awk '{FS = "t=" ;$0=$0; print $2/1000}'
returns 18.437

Note that $0=$0 in awk is just a way to reset it's calculation to use the new separator value
$0 is a variable holding the string that awk will act on. variables $1 and so on, hold the fields split out of $0 using either the default separator or a separator set by the FS instruction

Regards

anita2R
PS I don't have access to a Pi running 1-wire temperature sensors where I am now, so I can't test my solution ... hoping I have got the syntax correct!

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

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Tue Jan 19, 2016 7:09 am

Everyone has their favourite method :)

I tend to use sed (never like awk) where you can do the whole lot (cat, grep, cut) in one operation

Code: Select all

[email protected] ~ $ sed -n "s/^.*t=//p" /sys/bus/w1/devices/28-00044a1f53ff/w1_slave 
17375
-n means: Don't show the input data

"s/^.*t=//p" means Substitute anything that matches (^) From start of line, (.*) any characters (t=) and ends with "t=", with () nothing, ie. delete it, then Print what is left on that line.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Tue Jan 19, 2016 11:23 am

TY both very much!
Again, like bash more and more and will try to do some reading both on awk and sed.
Anita, I actually have been working with your example of reading multiple sensors. since I like working with each sensor individually, I am learning to alter a variable inside a bash function (sort of like passing a variable in other languages). Not finished yet, but am close. this way I can use a single function to manipulate each sensor separately.
Further, I am using grep[ so far to simply look for the word 'YES' in the two lines which should be enough to say 'the temp is correct'.
Love this forum! TY both again!
Rainer

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Tue Jan 19, 2016 11:58 pm

Anita et al,

first - if this is hijacking a post, please let me know and I will start a new one.

I have modified Anita's code for temp logging in an effort to learn bash as well as getting several variables of temps and their sensor ID's which I then wish to enter in a ROW into a spreadsheet.
That said, I modified the code as below, getting error codes as shown below the modified code. Can someone correct the code please?

code:
===========

Code: Select all

#!/bin/bash
# a script to read and save temperature readings from all the group 28 1-wire temperature sensors
#
# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
# iterate through all the devices
for file in $FILES; do
   
     for i in 1 2 3 4 5; do
   
      # get the 2 lines of the response from the device
      GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
      GETDATA1=`echo "$GETDATA" | grep crc`
      GETDATA2=`echo "$GETDATA" | grep t=`
      # get temperature
      TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
      #
        # test if crc is 'YES' and temperature is not -62 or +85
        if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000"  ]
           then
               # crc is 'YES' and temperature is not -62 or +85 - so save result
               echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> /home/pi/Projects/FlaskMenu/bash/$file
		#trial = "echo Trial $file = $TEMP"  -  this was wrong, causing error
		trial="$file $TEMP"
            break
           else
              sleep 1
               # this is a retry so log the failure - record date/time & device ID
               # echo `date +"%d-%m-%Y %H:%M:%S"`" - ""$file" >> /home/pi/Projects/FlaskMenu/bash/err.log
        fi
 done
    done
exit 0
================
and the error message:
==================
./trial-Copy.sh: line 23: trial: command not found

========
addendum: I figured it out - made the change to the line 'trial = .....

larsn
Posts: 1
Joined: Fri Jun 16, 2017 10:33 am

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $

Tue Jun 20, 2017 10:39 am

Dear tonyhuges,

I have been very successful in reading ds18b20 from command line, e.g. from a script handling the value as well as setting a timestamp, on one of my Raspberry Pi's, mainly with your lines. I like writing scripts.

Now I have purchased a temperature-/humidity sensor AM2301/DHT21, and wonder if you have tried to do the same with this sensor, or AM2302/DHT22.

Best regards
LarsN

jessey
Posts: 1
Joined: Thu Dec 28, 2017 8:49 am

Re: TUTORIAL: DS18B20 temp sensor. No C or Python!!! Cost: $3.05

Sat Mar 31, 2018 9:46 pm

Hi, this sounds like a good tutorial but I can't see the wiring diagram?
Anyone here know what's up with that?

Return to “Automation, sensing and robotics”