Skip to content

Instantly share code, notes, and snippets.

@mskurnik
Last active December 10, 2023 21:25
Show Gist options
  • Save mskurnik/04fb6c4c9655f1902723d70040967c1d to your computer and use it in GitHub Desktop.
Save mskurnik/04fb6c4c9655f1902723d70040967c1d to your computer and use it in GitHub Desktop.
Ambient Weather to XASTIR APRS WX Station
#!/bin/bash
# Original script written by S55MA and S56IUL, May 2016 - https://s55ma.radioamater.si/2016/05/03/la-crosse-ws2300-ws2307$
# Modified by u/MJCS July 2020
# Run the following in the background to gather data:
# rtl_433 -f 915M -R 20 -R 145 -R 78 -F json:weather.json
# ----- Change Log -----
# 2020-11-08 1839 Optimized code
# 2020-11-08 1745 Converted mm to in for rain guage
# 2020-11-08 1700 Added decode and calculation of rain guage for last hour and day
host="127.0.0.1"
port="1234"
# Start the Ncat server
chkncat=$(netstat -ant | grep $host:$port | grep -c LISTEN)
if [ "$chkncat" -ge "1" ]
then
echo "ncat already running, nothing to do"
else
nohup ncat -k -l --broker $host $port &>/dev/null &
fi
# Start while loop
while true; do
echo "start `date`"
# Date Calculations
currentDate=$(date +"%Y-%m-%d %T")
lastHour=$(date --date "$currentDate - 24 hours" +'%Y-%m-%d %T')
lastDay=$(date --date "$currentDate - 1 hour" +'%Y-%m-%d %T')
# Last Entry
data=$(tail -n 1 weather.json)
# Calculate values
winddirection=$(echo $data | jq .wind_dir_deg | awk '{print int($1)}')
windspeed=$(echo $data | jq .wind_avg_m_s | awk '{print int($1)}')
temp=$(echo $data | jq '.temperature_C * 1.8 + 32' | awk '{print int($1)}')
rainCurrent=$(echo $data | jq '.rain_mm' | awk '{print int($1)}')
rain1h=$(jq '[inputs] | reverse | map(select(.time<="'"$lastHour"'"))[0] | .rain_mm' weather.json | awk '{print int($1)}')
rain24h=$(jq '[inputs] | reverse | map(select(.time<="'"$lastDay"'"))[0] | .rain_mm' weather.json | awk '{print int($1)}')
rain1h=$(echo `echo "scale=2; ($rainCurrent - $rain1h) * (1 / 25.4)" | bc` | awk '{print int($1)}')
rain24h=$(echo `echo "scale=2; ($rainCurrent - $rain24h) * (1 / 25.4)" | bc` | awk '{print int($1)}')
airpressure="0000"
relhumidity=$(echo $data | jq .humidity)
# Make sure these are 3 digits
temp=$(printf "%03d" $temp)
rain1h=$(printf "%03d" $rain1h)
rain24h=$(printf "%03d" $rain24h)
windspeed=$(printf "%03d" $windspeed)
winddirection=$(printf "%03d" $winddirection)
# Combine variables to forge Xastir string
xastir="c${winddirection}s${windspeed}t${temp}r${rain1h}p${rain24h}b${airpressure}h${relhumidity}xDvs"
printf "%s\n" "$xastir" | ncat --send-only $host $port
echo "$xastir"
sleep 3
echo "stop `date`"
done
@mskurnik
Copy link
Author

mskurnik commented Nov 9, 2020

Updated script with ability to calculate rain level

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment