-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi-status-mqtt
43 lines (35 loc) · 1.5 KB
/
wifi-status-mqtt
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
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=50
STOP=50
start_service() {
procd_open_instance wifi-sta-to-mqtt
procd_set_param command /bin/sh $initscript run_service
# respawn automatically if something died, be careful if you have an alternative process supervisor
# if process dies sooner than respawn_threshold, it is considered crashed and after 5 retries the service is stopped
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
procd_set_param limits core="unlimited" # If you need to set ulimit for your process
#procd_set_param netdev dev # likewise, except if dev's ifindex changes.
procd_set_param stdout 1 # forward stdout of the command to logd
procd_set_param stderr 1 # same for stderr
procd_close_instance
}
run_service () {
MQTT_SERVER="192.168.88.2"
MQTT_ID="OpenWRT-Presence-Event"
MQTT_TOPIC="haklab/wifi"
iw event | \
while read LINE; do
if echo $LINE | grep -q -E "(new|del) station"; then
EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'`
MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'`
TOTAL=`iw dev wlan0 station dump| grep ^Station | wc -l`
#echo "Mac: $MAC did $EVENT"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID -t "$MQTT_TOPIC/mac-${MAC//:/-}/event" -m "$EVENT"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID -t "$MQTT_TOPIC/landevices" -m "$TOTAL"
fi
done
}
if [ "$1" == "run_service" ]; then
run_service
fi