84 lines
2.0 KiB
Plaintext
84 lines
2.0 KiB
Plaintext
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
#
|
||
|
|
# Copyright (c) 2018 leopold@shdx.org
|
||
|
|
# Author: Der_Leopold
|
||
|
|
#
|
||
|
|
# This script tries to read sensor values from a 1wire bus and writes the results
|
||
|
|
# and a timestamp to a file. It is intended to be executed periodically, for
|
||
|
|
# example via crontab.
|
||
|
|
#
|
||
|
|
|
||
|
|
# Retry up to n times if the reading fails
|
||
|
|
RETRY=3
|
||
|
|
|
||
|
|
# Number of seconds to wait between retries
|
||
|
|
DELAY=3
|
||
|
|
|
||
|
|
# Specify the temperature range where you expect the sensors will be operating
|
||
|
|
# This is simply used as a safeguard to detect implausible sensor readings
|
||
|
|
MINTEMP=-30
|
||
|
|
MAXTEMP=40
|
||
|
|
|
||
|
|
# Interface that is connected to the 1wire bus
|
||
|
|
USETTY=/dev/ttyUSB0
|
||
|
|
|
||
|
|
# Location of the digittemp config file with data about the sensors
|
||
|
|
DIGITEMPCONF="/etc/digitemp.conf"
|
||
|
|
|
||
|
|
# Command to use for reading the sensors
|
||
|
|
CMD="sudo /usr/bin/digitemp_DS9097"
|
||
|
|
CMDOPTS=" -q -s $USETTY -c $DIGITEMPCONF"
|
||
|
|
|
||
|
|
# Define sensors that should be queried
|
||
|
|
# Format: [<Label>]=<Id>
|
||
|
|
declare -A SENSORS=( [0]=LuftTemp [1]=WasserTemp [2]=BodenTemp )
|
||
|
|
|
||
|
|
# Location where the readings should be stored
|
||
|
|
OUTFILE="output"
|
||
|
|
|
||
|
|
|
||
|
|
#
|
||
|
|
# no configuration/changes should be necessary below this line
|
||
|
|
#
|
||
|
|
|
||
|
|
cd "$(dirname "$0")";
|
||
|
|
touch "$OUTFILE" 2> /dev/null
|
||
|
|
echo "Timestamp="`date +%s` > "$OUTFILE"
|
||
|
|
|
||
|
|
if [ ! -w $OUTFILE ]; then
|
||
|
|
echo "Error: output file '$OUTFILE' not writeable"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
function read_sensor () {
|
||
|
|
OUTPUT=`$CMD $CMDOPTS -t $SENSORID`
|
||
|
|
VALUE=`echo "$OUTPUT" | awk -F "C: " '{print $2}' | awk -F " F:" '{print $1}'`
|
||
|
|
CNT=$(($CNT+1))
|
||
|
|
}
|
||
|
|
|
||
|
|
function check_output () {
|
||
|
|
if [[ $OUTPUT =~ fail ]] || [[ $OUTPUT =~ owAcquire ]] || [ ${VALUE%.*} -lt $MINTEMP ] || [ ${VALUE%.*} -gt $MAXTEMP ]; then
|
||
|
|
if [ $CNT -le $RETRY ]; then
|
||
|
|
sleep $DELAY
|
||
|
|
read_sensor
|
||
|
|
check_output
|
||
|
|
else
|
||
|
|
echo "${SENSORS[$SENSORID]}=Error" > "$OUTFILE"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
function write_results () {
|
||
|
|
echo "${SENSORS[$SENSORID]}=${VALUE}" >> "$OUTFILE"
|
||
|
|
}
|
||
|
|
|
||
|
|
for SENSORID in "${!SENSORS[@]}"
|
||
|
|
do
|
||
|
|
CNT=0
|
||
|
|
read_sensor
|
||
|
|
check_output
|
||
|
|
write_results
|
||
|
|
sleep $DELAY
|
||
|
|
done
|