#!/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

# NOTE... NOTE.... NOTE... THIS SCRIPT MODIFIED TO READ TTYUSB0 INSTEAD OF TTYS0

# 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"," -f9`"

# 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  
echo
echo

done