-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuttond
More file actions
executable file
·44 lines (35 loc) · 1.09 KB
/
buttond
File metadata and controls
executable file
·44 lines (35 loc) · 1.09 KB
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
#!/bin/bash
# Check button state and light up led when pressed
set -eu
BUTTON_PIN=${BUTTON_PIN:-0}
LIGHT_PIN=${LIGHT_PIN:-1}
MQTT_PASS=${MQTT_PASS:-changeservice}
# Configure pins
raspi-gpio set $BUTTON_PIN ip
raspi-gpio set $LIGHT_PIN op
# Force initial update
last_state=-1
mqtt_notify() {
# Allow for e.g. MQTT network errors to occur without exiting the program
set +e
echo -n 'mqtt: '
mosquitto_pub --capath /etc/ssl/certs/ -h mq.hackeriet.no -p 8883 -t 'hackeriet/space_state' -d -u humladoor -P $MQTT_PASS -r -m $1
set -e
}
while true; do
# awk snipped from https://ma2shita.medium.com/how-to-use-raspi-gpio-instead-of-gpio-of-wriring-pi-af2ab00eda57
btn_state=$(raspi-gpio get $BUTTON_PIN | awk -v RS=" " -F "=" -v k="level" '$1==k {print $2}')
# Only write to pin when needed
if [ x$btn_state != x$last_state ]; then
echo "Button state set to $btn_state"
last_state=$btn_state
if [ "$btn_state" -eq "1" ]; then
raspi-gpio set $LIGHT_PIN dh
mqtt_notify OPEN
else
raspi-gpio set $LIGHT_PIN dl
mqtt_notify CLOSED
fi
fi
sleep 0.1
done