-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcputemp2mqtt
executable file
·125 lines (107 loc) · 3.39 KB
/
cputemp2mqtt
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
USAGE="Usage: $0 [-v] {-f config_file |
{SAMPLING_PERIOD REPORT_PERIOD
[args_for_mosquitto_pub]}
}
-v : verbose mode -- echo debug messages to stderr
-f config_file : read configuration from file
SAMPLING_PERIOD : how often (in seconds) should CPU temperature be read
REPORT_PERIOD : report average temperature every n-th sample
args_for_mosquitto_pub (examples):
-h hostname
-p port-number
-t message-topic
-u username
-P password
... (see man mosquitto_pub)
Avoid using the -u and -P arguments because it leaks secrets through the
process name. Use ~/.config/mosquitto_pub instead.
"
print_debug()
{
if [ "$DEBUG" = true ] ; then
echo "$@" >&2
fi
}
sample_and_report()
{
# Periodically sample CPU temperature and report the average to stdout.
# Fail immediately if mosquitto_pub fails.
# sleep ensures mosquitto_pub will already be done with whatever it's
# trying to do and echo then fails because of SIGPIPE.
# TODO this is ugly...
# Home Assistant does not seem to mind the 0 length message, it shows as if
# it was 0'C and recorder seems to treat it as unavailable.
sleep 1
echo ""
samples=""
sample_count=0
while true ; do
# take a sample
temperature=$(cat /sys/class/thermal/thermal_zone0/temp)
# store one decimal place -- divide by 100 instead of 1000
temperature=$((temperature/100))
# compute average
samples="$samples $temperature"
sample_count=$((sample_count+1))
print_debug "New sample: $((temperature/10))'C; array: $samples"
if [ "$sample_count" -eq "$REPORT_PERIOD" ] ; then
average=0
for sample in $samples ; do
average=$((average+sample))
done
average=$((average/sample_count))
average_string="$((average/10)).$((average%10))"
echo "$average_string"
print_debug "New average: $average_string"
samples=""
sample_count=0
fi
sleep "$SAMPLING_PERIOD"
done
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "$USAGE"
exit 0
fi
DEBUG=false
if [ "$1" = "-v" ] ; then
DEBUG=true
print_debug "Verbose mode is on"
shift
fi
CONFIG_FROM_FILE=false
if [ "$1" = "-f" ] ; then
shift
CONFIG_FILE="$1"
shift
if [ ! -f "$CONFIG_FILE" ] ; then
echo "Config file $CONFIG_FILE does not exist" >&2
exit 255
fi
print_debug "Reading configuration from $CONFIG_FILE"
# shellcheck source=cputemp2mqtt.conf
. "$CONFIG_FILE"
CONFIG_FROM_FILE=true
fi
if [ "$CONFIG_FROM_FILE" = false ] ; then
if [ "$#" -lt 2 ] ; then
echo "Too little arguments" >&2
echo "$USAGE" >&2
exit 255
fi
SAMPLING_PERIOD="$1"
shift
REPORT_PERIOD="$1"
shift
MOSQUITTO_PARAMS="$*"
fi
print_debug "SAMPLING_PERIOD: $SAMPLING_PERIOD"
print_debug "REPORT_PERIOD: $REPORT_PERIOD"
print_debug "MOSQUITTO_PARAMS: $MOSQUITTO_PARAMS"
trap 'trap - EXIT; exit 0' TERM INT HUP
trap 'trap - EXIT; exit 255' EXIT
# shellcheck disable=SC2086 # Intended splitting of MOSQUITTO_PARAMS
sample_and_report | mosquitto_pub --stdin-line $MOSQUITTO_PARAMS
# TODO why does mosquitto_pub use 100% CPU after this error?
# Connection error: Connection Refused: not authorised.