Skip to content

Commit 700c912

Browse files
author
p.koevesdi
committed
added: other tools, added logging function to syslog
1 parent 8029508 commit 700c912

File tree

3 files changed

+212
-33
lines changed

3 files changed

+212
-33
lines changed

gps2mqtt.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/sh
2+
############
3+
# GPS 2 MQTT
4+
5+
temp_dir=$(mktemp -d)
6+
7+
mkfifo "$temp_dir/out" "$temp_dir/err"
8+
<"$temp_dir/out" logger -p user.notice -t "$(basename "$0")" &
9+
<"$temp_dir/err" logger -p user.error -t "$(basename "$0")" &
10+
exec >"$temp_dir/out" 2>"$temp_dir/err"
11+
12+
thresdist=10 # sending threshold in m distance
13+
14+
backpipe="$temp_dir/mqttgps_backpipe"
15+
pidfile="$temp_dir/mqttgps_pidfile"
16+
oldlat=0
17+
oldlong=0
18+
19+
Calc() awk 'BEGIN{printf "%0.10f", '$*'}'
20+
21+
ctrl_c() {
22+
kill $(cat "$pidfile")
23+
rm -rf $temp_dir
24+
[ "$?" -eq "0" ] && {
25+
echo "Exit success."
26+
exit 0
27+
} || exit 1
28+
}
29+
30+
listen() {
31+
32+
echo "started."
33+
34+
while true; do
35+
#echo "$message"
36+
if [ $(echo "$message" | cut -d, -f 1) == "$dataset" ] &&
37+
[ $(echo "$message" | cut -d, -f 7) -ge 1 ] &&
38+
[ $(Checksum $(echo "$message" | cut -d$ -f2 | cut -d* -f1)) == $((0x$(echo "$message" | cut -d* -f2))) ]; then
39+
40+
lat=$(echo "$message" | cut -d, -f 3)
41+
lat=$(Calc "($(echo $lat | cut -c1-2)+$(echo $lat | cut -c3-)/60)")
42+
if [ $(echo "$message" | cut -d, -f 4) == "S" ]; then lat=-$lat; fi
43+
44+
long=$(echo "$message" | cut -d, -f 5)
45+
long=$(Calc "($(echo $long | cut -c1-3)+$(echo $long | cut -c4-)/60)")
46+
if [ $(echo "$message" | cut -d, -f 6) == "W" ]; then long=-$long; fi
47+
48+
# nach https://www.movable-type.co.uk/scripts/latlong.html:
49+
#a=$(Calc "sin(($lat-$oldlat)/2)^2+sin(($long-$oldlong)/2)^2*cos($lat)*cos($oldlat)")
50+
#dist=$(Calc "6371000*2*atan2(sqrt($a),sqrt(1-$a))");
51+
# Vereinfachung Pythagoras, ok fuer kleine Abstaende:
52+
dist=$(Calc "6371000*sqrt(($lat-$oldlat)^2+($long-$oldlong)^2)/180*3.1415926")
53+
54+
#printf "\033[1K\rold: %.8f %.8f, new: %.8f %.8f, dist: %.8f" "$oldlat" "$oldlong" "$lat" "$long" "$dist"
55+
56+
if [ $(awk "BEGIN{if ($dist>$thresdist) print 1;else print 0}") == 1 ]; then
57+
zeit=$(echo "$message" | cut -d, -f 2)
58+
alt=$(echo "$message" | cut -d, -f 10)
59+
gs=$(echo "$message" | cut -d, -f 12)
60+
# datum=$(echo "$message" | cut -d, -f 10)
61+
mosquitto_pub -h localhost -t womo/RUT955/gps -m "$zeit,$lat,$long,$alt,$gs"
62+
oldlat=$lat
63+
oldlong=$long
64+
fi
65+
sleep 1
66+
fi
67+
68+
done <$backpipe
69+
}
70+
71+
usage() {
72+
echo "GPS 2 Mqtt"
73+
echo "send position of GPS messages of type GPRMC via mqtt"
74+
echo "Configure parameters inside script."
75+
}
76+
77+
case "$1" in
78+
--h)
79+
usage
80+
exit 1
81+
;;
82+
*)
83+
trap ctrl_c INT TERM QUIT
84+
listen
85+
;;
86+
esac

mqtt.sh

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
###################
33
# MQTT Shell Logger
44

5+
temp_dir=$(mktemp -d)
6+
7+
mkfifo "$temp_dir/out" "$temp_dir/err"
8+
<"$temp_dir/out" logger -p user.notice -t "$(basename "$0")" &
9+
<"$temp_dir/err" logger -p user.error -t "$(basename "$0")" &
10+
exec >"$temp_dir/out" 2>"$temp_dir/err"
11+
512
outputroot="/mnt/mmcblk0p1" # Scope to put and rotate the logs in, thus oldest file inside this might get deleted, even if not lyed out by this script!
613
outputsubdirs=1 # whether to create subdirectories for each topic
714
filenameprefix="log_"
@@ -12,60 +19,74 @@ minfreespace=2097 # in KiB
1219
#maxfilesize=1048 # in B (for testing)
1320
maxfilesize=1048576 # in B
1421

15-
backpipe="/tmp/mqttlogger_backpipe"
16-
pidfile="/tmp/mqttlogger_pidfile"
22+
backpipe="$temp_dir/mqttlogger_backpipe"
23+
pidfile="$temp_dir/mqttlogger_pidfile"
1724

1825
ctrl_c() {
19-
echo "Cleaning up..."
20-
kill $(cat $pidfile) 2>/dev/null
21-
rm -f $backpipe $pidfile
22-
[ "$?" -eq "0" ] && { echo "Exit success"; exit 0; } || exit 1
26+
kill $(cat "$pidfile")
27+
rm -rf $temp_dir
28+
[ "$?" -eq "0" ] && {
29+
echo "Exit success."
30+
exit 0
31+
} || exit 1
2332
}
2433

25-
listen(){
34+
listen() {
2635

36+
while true
37+
do
38+
curl -s mqtt://localhost:1883
39+
ret=$?
40+
[ $ret -eq 3 ] && unset tmp && unset ret && break
41+
[ -z $tmp ] && echo "waiting for mqtt broker" && tmp=1
42+
sleep 1
43+
done
44+
2745
[ ! -p "$backpipe" ] && mkfifo $backpipe
28-
(mosquitto_sub -v $*>$backpipe 2>/dev/null) &
29-
echo "$!" > $pidfile
46+
(mosquitto_sub $* 1>$backpipe) &
47+
echo "$!" >"$pidfile"
3048

31-
while read line <$backpipe
32-
do
49+
echo "connected."
50+
51+
while read line; do
3352

34-
#timestamp=$(date -Ins) # option ns not available on busybox
35-
ns=$(adjtimex | awk 'NR==12 {print $NF}') # get nanoseconds on busybox
36-
timestamp=$(date "+%Y-%m-%dT%H:%M:%S.$ns%z") # workaround for not having +%N or -Ins options in date
37-
#timestamp=$(date "+%Y-%m-%dT%H:%M:%S%z") # give up on nanoseconds
53+
# get nanoseconds on busybox
54+
# (workaround for not having +%N or -Ins options in date)
55+
us=$(adjtimex | awk 'NR==12 {print $NF}')
56+
timestamp=$(date "+%Y-%m-%dT%H:%M:%S.$us%z")
3857

3958
topic=${line%%" "*}
4059
message=${line#*" "}
4160
outputpath="$outputroot"$([ $outputsubdirs -ne 0 ] && echo "/$topic")
4261

4362
# make new directory if necessary
4463
# otherwise get last changed logfile inside the subfolder
45-
[ ! -d $outputpath ] && { mkdir -p $outputpath; unset filename; } || \
46-
filename=$(find $outputpath -maxdepth 1 -type f -name "$filenameprefix*.$fileextension" -print0 | xargs -0r ls -t | head -n1)
64+
[ ! -d $outputpath ] && {
65+
mkdir -p $outputpath
66+
unset filename
67+
} ||
68+
filename=$(find $outputpath -maxdepth 1 -type f -name "$filenameprefix*.$fileextension" -print0 | xargs -0r ls -t | head -n1)
4769

4870
# debugging:
49-
# echo "topic=$topic"
50-
# echo "message=$message"
51-
# echo "outputpath: $outputpath"
71+
# echo "received: $topic = $message"
72+
# echo "outputpath: $outputpath"
5273

5374
# new file if necessary:
54-
([ -z "$filename" ] || \
55-
(filesize=$(wc -c "$filename") && filesize=${filesize%%" "*} && [ "$filesize" -gt "$maxfilesize" ])) && \
56-
filename="$outputpath/$filenameprefix$(date $filenamesuffixformat).$fileextension"
75+
([ -z "$filename" ] ||
76+
(filesize=$(wc -c "$filename") && filesize=${filesize%%" "*} && [ "$filesize" -gt "$maxfilesize" ])) &&
77+
filename="$outputpath/$filenameprefix$(date $filenamesuffixformat).$fileextension"
5778

5879
# delete oldest, if necessary:
5980
avail=$(df "$outputroot" | awk 'NR == 2 { print $4 }') # get free disk space
6081
[ "$avail" -lt "$minfreespace" ] && rm "$(find $outputroot -type f -name "$filenameprefix*.$fileextension" -print0 | xargs -0r ls -tr | head -n1)"
6182

6283
# write log entry:
63-
echo "\"$timestamp\"$([ $outputsubdirs -eq 0 ] && echo ",\"$topic\""),\"$message\"">>"$filename"
84+
echo "$timestamp$([ $outputsubdirs -eq 0 ] && echo ",$topic"),$message" >>"$filename"
6485

65-
done
86+
done <$backpipe
6687
}
6788

68-
usage(){
89+
usage() {
6990
echo "Mqtt-Exec Logger Via Bash"
7091
echo "based on https://unix.stackexchange.com/a/274224"
7192
echo "Logger writes messages from subscribed topics into a file."
@@ -76,12 +97,11 @@ usage(){
7697

7798
case "$1" in
7899
--h)
79-
usage
80-
exit 1
81-
;;
100+
usage
101+
exit 1
102+
;;
82103
*)
83-
trap ctrl_c INT
84-
listen $*
85-
;;
104+
trap ctrl_c INT TERM QUIT
105+
listen -v -t womo/# $*
106+
;;
86107
esac
87-

ve2mqtt.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/sh
2+
############
3+
# GPS 2 MQTT
4+
5+
fh="/dev/ttyUSB0"
6+
stty -F $fh 19200 raw -echo -echoe -echok
7+
fields=" PID FW SER V I VPV PPV CS MPPT OR ERR LOAD H19 H20 H21 H22 H23 HSDS "
8+
backpipe="/tmp/mqttve_backpipe"
9+
pidfile="/tmp/mqttve_pidfile"
10+
11+
ctrl_c() {
12+
echo "Cleaning up..."
13+
kill $(cat $pidfile) 2>/dev/null
14+
rm -f $backpipe $pidfile
15+
[ "$?" -eq "0" ] && {
16+
echo "Exit success"
17+
exit 0
18+
} || exit 1
19+
}
20+
21+
listen() {
22+
23+
[ ! -p "$backpipe" ] && mkfifo $backpipe
24+
(cat $fh $* >$backpipe 2>/dev/null) &
25+
echo "$!" >$pidfile
26+
27+
while read name value; do
28+
29+
name=${name//[#+]/}
30+
value=${value//$'\r'/}
31+
case "$fields" in
32+
*" $name "*)
33+
# echo "found: $name"
34+
eval old_val=\$$name
35+
if [ "$value" != "$old_val" ]; then
36+
# echo "changed: $name: $old_val -> $value"
37+
mosquitto_pub -h localhost -t "womo/mppt/$name" -m "$value"
38+
eval $name="'$value'"
39+
if [ "$name" == "LOAD" ]; then
40+
if [ "$value" == "ON" ]; then
41+
gpio.sh set DOUT2
42+
elif [ "$value" == "OFF" ]; then
43+
gpio.sh clear DOUT2
44+
fi
45+
fi
46+
# else
47+
# echo "value unchanged: $old_val -> $value"
48+
fi
49+
;;
50+
*)
51+
# echo "not found: $name"
52+
;;
53+
esac
54+
55+
done <$backpipe
56+
}
57+
58+
usage() {
59+
echo "VE 2 Mqtt"
60+
echo "send Victron VE.direct messages via mqtt"
61+
echo "Configure parameters inside script."
62+
}
63+
64+
case "$1" in
65+
--h)
66+
usage
67+
exit 1
68+
;;
69+
*)
70+
trap ctrl_c INT
71+
listen $*
72+
;;
73+
esac

0 commit comments

Comments
 (0)