Skip to content

Instantly share code, notes, and snippets.

@RickKimball
Last active December 9, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickKimball/926a6e638d6fea994384af4d8487078f to your computer and use it in GitHub Desktop.
Save RickKimball/926a6e638d6fea994384af4d8487078f to your computer and use it in GitHub Desktop.
#!/bin/bash
# read the w1-temp values
# put each line1 and 2 into variables
# parse and see if it is ready
# if so read the raw value and round it to 2 decimals
read_sensor()
{
SENSOR_FILE=$1
local __celsius_raw=$2
local __celsius_dec=$3
local __rc=$4
local indx=0;
while IFS='' read -r temp; do
case $indx in
0) line1=$temp;;
1) line2=$temp;;
esac
indx=$((indx+1))
done < $SENSOR_FILE
ready=${line1/YES/,ready}
if [ "${ready#*,}" == "ready" ]
then
local celsius_raw=${line2#*t=}
local celsius_dec=$(echo "scale=2; ((${celsius_raw}+5)/1000.0)" | bc)
eval $__rc="1"
eval $__celsius_raw="'$celsius_raw'"
eval $__celsius_dec="'$celsius_dec'"
else
eval $__rc="0"
fi
}
# loop through all the sensors
SENSOR_FILES=$(echo /sys/bus/w1/devices/28-*/w1_slave)
while true; do
for sensor_file in ${SENSOR_FILES}; do
read_sensor $sensor_file c d rc
if [ "$rc" == "1" ]; then
echo "Raw=${c}"
echo "Celsius=${d}"
else
>&2 echo -n "."
fi
done
sleep 0.2
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment