Skip to content

Instantly share code, notes, and snippets.

@ruzickap
Created April 20, 2014 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruzickap/11106415 to your computer and use it in GitHub Desktop.
Save ruzickap/11106415 to your computer and use it in GitHub Desktop.
OpenWrt - External USB Thermometers
mkdir -p /data/mydigitemp /www3/mydigitemp
#The graphs can be accessed: http://192.168.1.1/myadmin/mydigitemp
ln -s /www3/mydigitemp /www3/myadmin/mydigitemp
#Create RRDtool database to store the values every 10 minutes (600 seconds) for 10 years (525600 * 600 seconds)
rrdtool create /data/mydigitemp/mydigitemp.rrd --step 600 \
DS:temp0:GAUGE:1000:-273:5000 DS:temp1:GAUGE:1000:-273:5000 RRA:AVERAGE:0.5:1:525600 \
RRA:MIN:0.5:1:525600 RRA:MAX:0.5:1:525600
#Script getting the data from termometers
cat > /data/mydigitemp/mydigitemp.sh << \EOF
#!/bin/sh
TEMP0_ROUNDED=1000
TEMP1_ROUNDED=1000
#Sometimes the values are not in the right "range" and needs to be read few times
while [ $TEMP0_ROUNDED -gt 50 ] || [ $TEMP0_ROUNDED -lt 5 ] ; do
TEMP0=$(/usr/bin/digitemp_DS9097 -c/etc/digitemp.conf -t0 -q -s/dev/ttyUSB0 -o"%.2C")
TEMP0_ROUNDED=`echo $TEMP0 | awk '{print int($1+0.5)}'`
done
while [ $TEMP1_ROUNDED -gt 50 ] || [ $TEMP1_ROUNDED -lt 5 ] ; do
TEMP1=$(/usr/bin/digitemp_DS9097 -c/etc/digitemp.conf -t1 -q -s/dev/ttyUSB0 -o"%.2C")
TEMP1_ROUNDED=`echo $TEMP1 | awk '{print int($1+0.5)}'`
done
/usr/bin/rrdtool update /data/mydigitemp/mydigitemp.rrd $(date +%s):$TEMP0:$TEMP1
EOF
cat > /data/mydigitemp/mydigitemp-graph.sh << EOF
#!/bin/sh
RRD_FILE="/data/mydigitemp/mydigitemp.rrd"
DST_FILE="/www3/mydigitemp/$1.png"
RRD_PARAMETERS='
$DST_FILE --end=$(date +%s) --vertical-label "Temperature .C" --width 1024 --height 600 --lower-limit 0
DEF:temp0=$RRD_FILE:temp0:AVERAGE
DEF:temp1=$RRD_FILE:temp1:AVERAGE
LINE1:temp0#CF00FF:"10 minutes average inside\\n"
LINE2:temp1#FF3C00:"10 minutes average outside\\n"
COMMENT:" \\n"
GPRINT:temp0:MIN:"Minimum inside\\: %4.1lf .C "
GPRINT:temp1:MIN:"Minimum outside\\: %4.1lf .C "
GPRINT:temp0:MAX:"Maximum inside\\: %4.1lf .C "
GPRINT:temp1:MAX:"Maximum outside\\: %4.1lf .C\\n"
GPRINT:temp0:AVERAGE:"Average inside\\: %4.1lf .C "
GPRINT:temp1:AVERAGE:"Average outside\\: %4.1lf .C "
GPRINT:temp0:LAST:"Current inside\\: %4.1lf .C "
GPRINT:temp1:LAST:"Current outside\\: %4.1lf .C"
> /dev/null
'
case $1 in
daily)
eval /usr/bin/rrdtool graph --start="end-2days" --title \'Daily graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
weekly)
eval /usr/bin/rrdtool graph --start="end-2week" --title \'Weekly graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
monthly)
eval /usr/bin/rrdtool graph --start="end-2month" --title \'Monthly graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
yearly)
eval /usr/bin/rrdtool graph --start="end-1year" --title \'Yearly graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
2years)
eval /usr/bin/rrdtool graph --start="end-2years" --title \'2 Years graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
5years)
eval /usr/bin/rrdtool graph --start="end-5years" --title \'5 Years graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
10years)
eval /usr/bin/rrdtool graph --start="end-10years" --title \'10 Years graph [`date +"%F %H:%M"`]\' $RRD_PARAMETERS
;;
*)
echo "Please specify $0 [daily|weekly|monthly|yearly|2years|5years|10years]"
;;
esac
EOF
chmod a+x /data/mydigitemp/*.sh
#Add cron entries to put the temperatures into the database and create graphs
cat >> /etc/crontabs/root << \EOF
*/10 * * * * test -x /data/mydigitemp/mydigitemp.sh && /data/mydigitemp/mydigitemp.sh
0 * * * * /data/mydigitemp/mydigitemp-graph.sh daily
1 0 * * * /data/mydigitemp/mydigitemp-graph.sh weekly
2 0 * * 0 /data/mydigitemp/mydigitemp-graph.sh monthly
3 0 1 * * /data/mydigitemp/mydigitemp-graph.sh yearly
4 0 1 1 * /data/mydigitemp/mydigitemp-graph.sh 2years
5 0 1 1 * /data/mydigitemp/mydigitemp-graph.sh 5years
6 0 1 1 * /data/mydigitemp/mydigitemp-graph.sh 10years
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment