-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgps2mqtt.sh
81 lines (66 loc) · 1.69 KB
/
gps2mqtt.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
#!/bin/sh
############
# GPS 2 MQTT
temp_dir=$(mktemp -d)
mkfifo "$temp_dir/out" "$temp_dir/err"
<"$temp_dir/out" logger -p user.notice -t "$(basename "$0")" &
<"$temp_dir/err" logger -p user.error -t "$(basename "$0")" &
exec >"$temp_dir/out" 2>"$temp_dir/err"
thresdist=10 # sending threshold in m distance
oldlat=0
oldlong=0
Calc() awk 'BEGIN{printf "%0.10f", '$*'}'
ctrl_c() {
rm -rf $temp_dir
[ "$?" -eq "0" ] && {
echo "Exit success."
exit 0
} || exit 1
}
listen() {
while true
do
curl -s mqtt://localhost:1883
ret=$?
[ $ret -eq 3 ] && unset tmp && unset ret && break
[ -z $tmp ] && echo "waiting for mqtt broker" && tmp=1
sleep 1
done
echo "started."
while true
do
status=$(gpsctl -s)
if [ $status -ge 1 ]
then
lat=$(gpsctl -i)
long=$(gpsctl -x)
# nach https://www.movable-type.co.uk/scripts/latlong.html:
#a=$(Calc "sin(($lat-$oldlat)/2)^2+sin(($long-$oldlong)/2)^2*cos($lat)*cos($oldlat)")
#dist=$(Calc "6371000*2*atan2(sqrt($a),sqrt(1-$a))");
# Vereinfachung Pythagoras, ok fuer kleine Abstaende:
dist=$(Calc "6371000*sqrt(($lat-$oldlat)^2+($long-$oldlong)^2)/180*3.1415926")
if [ $(awk "BEGIN{if ($dist>$thresdist) print 1;else print 0}") == 1 ]
then
mosquitto_pub -h localhost -t womo/RUT955/gps -m "$(gpsctl -e),$lat,$long,$(gpsctl -a),$(gpsctl -v),$(gpsctl -p),$(gpsctl -a),$status,$(gpsctl -u)"
oldlat=$lat
oldlong=$long
fi
fi
sleep 1
done
}
usage() {
echo "GPS 2 Mqtt"
echo "send gps parameters via mqtt"
echo "Configure parameters inside script."
}
case "$1" in
--h)
usage
exit 1
;;
*)
trap ctrl_c INT TERM QUIT
listen
;;
esac