#!/bin/bash

# This script reads NMEA sentances from a GPS device on the serial port and 
# filters out all but the sentence "GPGGA" and the 3rd, 5th, 8th and 9th
# fields which correspond to Lat, Lon, number of satellites, and altitude

# set serial port to 4800 baud rate
#  stty -F/dev/ttyS0 4800
stty -F /dev/ttyUSB0 4800

count=0

# begin infinate loop to read the data

while true; do

count=$(( $count + 1 ))

#Read GPS data from serial port and throw away lines that don't begin with GPGGA
#  GPGGAstring="`head -n26 /dev/ttyS0 | grep GPGGA`" 
GPGGAstring="`head -n26 /dev/ttyUSB0 | grep GPGGA`" 

# Pick out individual numbers for Time, Lat, Long, Satellites, and Altitude
GPStimeStamp="`echo $GPGGAstring | cut -d"," -f2`"
Latitude="`echo $GPGGAstring | cut -d"," -f3`"
Longitude="`echo $GPGGAstring | cut -d"," -f5`"
NumberOfSatellites="`echo $GPGGAstring | cut -d"," -f8`"
Altitude="`echo $GPGGAstring | cut -d"," -f10`"

# Trim Lat and Long to a charactor size that is compatible with APRS servers
Latitude=$(expr substr "${Latitude}" 1 7)
Longitude=$(expr substr "${Longitude}" 1 8)

# Add the North and West to the end of the numbers
Latitude=`echo $Latitude"N"`
Longitude=`echo $Longitude"W"`

# Print data to screen
echo "Count = " $count
date
echo "Latitude = " $Latitude
echo "Longitude = " $Longitude
echo "Altitude = " $Altitude
echo "Satellites = " $NumberOfSatellites
echo $GPGGAstring  

######### This section converts altitude from meters to feet ##########

Altitude_convert=$( echo "$Altitude" | tr -d '.' )
Altitude_convert=$(($Altitude_convert * 328))
Altitude_convert=$(echo "$Altitude_convert" | sed 's/...$//' )
feet=$Altitude_convert


echo "feet = " $feet


echo
echo

if [ $count -gt 14 ]  ; then   # change the number to change reporting time to APRS servers

############### APRS internet reporting script ########################

count=0

echo
echo "REPORT POSITION TO APRS SERVERS ON THE INTERNET!"
echo

OBJ_NAME="callsign "		# MUST be EXACTLY 9 characters between the quotation marks (add spaces if you have to)
SYMBOL="k"
NODE_STATUS_CHAR="/"        # / = primary symbol table   \ = alternate symbol table
# COMMENT="  Satellites in view = $NumberOfSatellites  ;  Altitude = $Altitude Meters  "  # charactors 46-80
COMMENT="  Satellites in view = $NumberOfSatellites  ;  Altitude = $feet Feet  "  # charactors 46-80
LAT=$Latitude
LONG=$Longitude
APRS_CALL=K5ABC
APRS_PASS=xxxxx
TIMESTAMP=`date -u +%d%H%M`z		# Format = DDHHMMz
OBJECT=";${OBJ_NAME}*${TIMESTAMP}${LAT}${NODE_STATUS_CHAR}${LONG}${SYMBOL}${COMMENT}"
BTEXT="$APRS_CALL>APVR30:${OBJECT}"
LOGIN="User $APRS_CALL pass $APRS_PASS vers IRLP-interface 1"
NC=/bin/nc
echo -e "${LOGIN}\n${BTEXT}" | $NC -w 10 rotate.aprs2.net 14580    # &>/dev/null


fi

done