-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy paththermostat.sh
86 lines (73 loc) · 2.84 KB
/
thermostat.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
84
85
86
#!/bin/sh
log() {
logger -s -t "thermostat" "$*"
}
# read config
source $BIN_PATH/thermostat/thermostat.cfg
# init vars
for i in `seq 1 $channels`;
do
eval actTemp$i=0.0
eval targetTemp$i=0.0
eval currentMode$i=0
eval current=\$actTempTopic$i
topics="$topics -t $current"
eval current=\$targetTempTopic$i
topics="$topics -t $current"
done
log "Starting thermostat with $channels channels"
log "listening to topics: $topics"
# turn off all relays
for i in `seq 1 $channels`;
do
echo 0 > /proc/power/relay$i
done
listen(){
$BIN_PATH/mosquitto_sub -I $clientID -h $mqtthost -v $topics | while read line; do
topic=`echo $line| cut -d" " -f1`
val=`echo $line| cut -d" " -f2`
for i in `seq 1 $channels`;
do
eval current=\$actTempTopic$i
if [ "$topic" == $current ]; then
eval actTemp$i=$val
fi
eval current=\$targetTempTopic$i
if [ "$topic" == $current ]; then
eval targetTemp$i=$val
fi
eval hyster=\$hyster$i
eval targetTemp=\$targetTemp$i
eval actTemp=\$actTemp$i
eval currentMode=\$currentMode$i
eval roomName=\$roomName$i
withHysterLow=$(awk -vn1="$targetTemp" -vn2="$hyster" 'BEGIN{print n1-n2}')
withHysterHigh=$(awk -vn1="$targetTemp" -vn2="$hyster" 'BEGIN{print n1+n2}')
belowHystLow=$(awk -vn1="$actTemp" -vn2="$withHysterLow" 'BEGIN{print (n1<n2)?1:0 }')
aboveHystHigh=$(awk -vn1="$actTemp" -vn2="$withHysterHigh" 'BEGIN{print (n1>n2)?1:0 }')
belowHystHigh=$(awk -vn1="$actTemp" -vn2="$withHysterHigh" 'BEGIN{print (n1<=n2)?1:0 }')
debug=""
# currently not heating
if [ "$currentMode" -eq 0 ];then
if [ "$belowHystLow" -eq 1 ];then
debug=`printf "Channel %s (%s) - ActTemp: %s TargetTemp: %s Too cold, start heating." "$i" "$roomName" "$actTemp" "$targetTemp"`
eval currentMode$i=1
echo 1 > /proc/power/relay$i
else
debug=`printf "Channel %s (%s) - ActTemp: %s TargetTemp: %s Warm enough, keep heating off." "$i" "$roomName" "$actTemp" "$targetTemp"`
fi
# currently heating
elif [ "$currentMode" -eq 1 ];then
if [ "$aboveHystHigh" -eq 1 ];then
debug=`printf "Channel %s (%s) - ActTemp: %s TargetTemp: %s Warm enough, stop heating." "$i" "$roomName" "$actTemp" "$targetTemp"`
eval currentMode$i=0
echo 0 > /proc/power/relay$i
elif [ "$belowHystHigh" -eq 1 ];then
debug=`printf "Channel %s (%s) - ActTemp: %s TargetTemp: %s Too cold, continue heating." "$i" "$roomName" "$actTemp" "$targetTemp"`
fi
fi
$BIN_PATH/mosquitto_pub -h $mqtthost -t $debugTopic -m "$debug"
done
done
}
listen