Skip to content

Instantly share code, notes, and snippets.

@mskurnik
Last active December 22, 2024 06:56
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 gauge
# 2020-11-08 1700 Added decode and calculation of rain gauge for last hour and day
# 2024-12-21 2255 Optimized and refactored code. Fixed some typos.
host="127.0.0.1"
port="1234"
# Start the Ncat server
if ! netstat -ant | grep -q "$host:$port.*LISTEN"; then
nohup ncat -k -l --broker $host $port &>/dev/null &
else
echo "ncat already running, nothing to do"
fi
# Start while loop
while true; do
echo "start `date`"
# Date Calculations
currentDate=$(date +"%Y-%m-%d %T")
lastHour=$(date --date "-1 hour" +"%Y-%m-%d %T")
lastDay=$(date --date "-1 day" +"%Y-%m-%d %T")
# Last Entry
data=$(tail -n 1 weather.json)
# Calculate values
winddirection=$(jq .wind_dir_deg <<< "$data" | awk '{print int($1)}')
windspeed=$(jq .wind_avg_m_s <<< "$data" | awk '{print int($1)}')
temp=$(jq '.temperature_C * 1.8 + 32' <<< "$data" | awk '{print int($1)}')
rainCurrent=$(jq .rain_mm <<< "$data" | awk '{print int($1)}')
rain1h=$(jq --arg lastHour "$lastHour" 'reverse | map(select(.time <= $lastHour))[0].rain_mm' weather.json | awk '{print int($1)}')
rain24h=$(jq --arg lastDay "$lastDay" 'reverse | map(select(.time <= $lastDay))[0].rain_mm' weather.json | awk '{print int($1)}')
rain1h=$(echo "scale=2; ($rainCurrent - $rain1h) * (1 / 25.4)" | bc | awk '{print int($1)}')
rain24h=$(echo "scale=2; ($rainCurrent - $rain24h) * (1 / 25.4)" | bc | awk '{print int($1)}')
airpressure="0000"
relhumidity=$(jq .humidity <<< "$data")
# 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

Looks like XASTIR only likes certain values at the end of the string that is sent to it over TCP. xDvs is required at the end vs xAmb. I can't find a list of values that are supported.

@mskurnik
Copy link
Author

mskurnik commented Nov 9, 2020

Updated script with ability to calculate rain level

@mskurnik
Copy link
Author

Optimized and refactored code. Fixed some typos.

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