Initial import of onewire-to-usb bridge
[onewire] / readtemp.sh
CommitLineData
bba33fe1
JM
1#!/bin/bash
2
3DEV=/dev/ttyUSB0
4TIMEOUT=5
5LOCK=/var/tmp/readtemp.lock
6
7SYSDEV=/sys/bus/usb/devices
8for i in $SYSDEV/*
9do
10if [ -f "$i/interface" ]
11then
12 if grep -q "CP2102 USB to UART" "$i/interface"
13 then
14 DEV=/dev/$(basename `ls -d $i/ttyUSB* `)
15 # dont break here, keep going so we get the last one
16 # break
17 fi
18fi
19done
20
21if ! lockfile -r 3 -l 5 $LOCK
22then
23echo "Failed to obtain lock"
24exit;
25fi
26
27if ! stty --file=$DEV | grep -q 'speed 19200'
28then
29stty --file=$DEV 19200
30sleep 1
31fi
32
33if ! stty --file=$DEV | grep -q '-echo'
34then
35stty --file=$DEV -echo
36sleep 1
37fi
38
39echo -n "t" >> $DEV
40
41MODE=plain
42if [ $# -ge 1 ] && [ "$1" == "-c" ]; then
43MODE=cacti
44fi
45
46while read -t $TIMEOUT -u 3 CODE ID TEM OTH; do
47# last one
48 if [ "$CODE" == "END" ]; then
49 break;
50 fi
51
52# not a valid line
53 if [ "$CODE" != "TEMP" ]; then
54 echo "ERROR CODE=$CODE ID=$ID TEM=$TEM"
55 continue;
56 fi
57
58# truncate for paranoia
59 TEM=${TEM:0:4}
60
61# calculate degrees from the hex code
62 DEGC=`echo "16 i $TEM A i 4 k 0.0625 * p" | dc`
63# display the result
64if [ $MODE == "cacti" ]; then
65 printf "id:%s temp:%s\n", "$ID" "$DEGC"
66else
67 printf "%s %s degC\n" "$ID" "$DEGC";
68fi
69done 3< $DEV
70
71rm -f $LOCK