-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensormaster.sh
executable file
·83 lines (70 loc) · 2.18 KB
/
sensormaster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
MQTTHOST="localhost"
MQTTPORT="1883"
MQTTUSER=""
MQTTPASS=""
MQTTTOPICBASE="app/dewpoint"
MQTTTOPICIN="$MQTTTOPICBASE/in"
MQTTREADER_OUTTOPIC="app/dewpoint/sensor/in"
SENSORMASTERCONFIG="[{\"name\":\"id3-116\",\"id\":\"keller1\"},{\"name\":\"id1-231\",\"id\":\"aussen1stock\"}]"
if [ -f "/etc/opendew/opendew.cfg" ]; then
. "/etc/opendew/opendew.cfg"
fi
if [ -f "/etc/opendew/lib.sh" ]; then
. "/etc/opendew/lib.sh"
fi
if [ -f "$HOME/.opendew/opendew.cfg" ]; then
. "$HOME/.opendew/opendew.cfg"
fi
SCRIPT_DIR=$(dirname -- "$0")
if [ -f "$SCRIPT_DIR/lib.sh" ]; then
. "$SCRIPT_DIR/lib.sh"
fi
get_sensor_name() {
KEY="$1"
NAME=$(echo "$SENSORMASTERCONFIG" | jq -r --argjson k "$KEY" '.[] | select(.name==$k).id')
if [ "$NAME" = "null" ]; then
echo ""
else
echo "$NAME"
fi
}
read_sensor_in() {
#echo "in: $1"
N=$(echo "$1" | jq '.name')
echo ".name = $N"
T=$(echo "$1" | jq -r '.temperature_C')
H=$(echo "$1" | jq -r '.humidity')
ID=$(get_sensor_name "$N")
D=$(calcdew "$T" "$H")
ts=$(get_timestamp_string)
MSG="{\"ts\":\"$ts\",\"name\":\"$ID\",\"temperature_C\":$T,\"humidity\":$H,\"dew\":$D}"
mosquitto_pub -h "$MQTTHOST" -p "$MQTTPORT" -t "$MQTTTOPICIN" -u "$MQTTUSER" -P "$MQTTPASS" -m "$MSG"
}
calcdew() {
#fH = (log10(fFeuchteAussen) - 2) / 0.4343 + (17.62 * fTempAussen) / (243.12 + fTempAussen);
#fTPktAussen = 243.12 * fH / (17.62 - fH);
THUM=$2
TTEMPC=$1
if [ "$(type -t "custom_sensor_calcdew")" = 'custom_sensor_calcdew' ]; then
DEWTEMP=$(custom_sensor_calcdew "$THUM" "$TTEMPC")
echo "$DEWTEMP"
else
DEWTEMP=$(awk -v HUM="$THUM" -v TEMPC="$TTEMPC" 'BEGIN { fh = (log(HUM)/log(10)-2)/0.4343 + (17.62 * TEMPC) / (243.12 + TEMPC); dew = 243.12 * fh / (17.62 - fh); print dew }')
echo "$DEWTEMP"
fi
}
mainfunc() {
while true
do
mosquitto_sub -h "$MQTTHOST" -p "$MQTTPORT" -u "$MQTTUSER" -P "$MQTTPASS" -t "$MQTTREADER_OUTTOPIC" | ( IFS='' ; while read line ;
do
check_main_process
out=$(read_sensor_in $line)
echo "$out"
done ;
)
done
}
mainfunc
echo "sensormaster exiting"