Skip to content

Instantly share code, notes, and snippets.

@dustinlbarnett

dustinlbarnett/gpstime.sh

Last active Aug 29, 2015
Embed
What would you like to do?
Set system clock using GPS hardware
#!/bin/bash
# This script attempts to read the current time from the gpsd daemon by using the gpspipe command to grab gpsdsentences.
# Before attempting to get time, it checks if there is a gps fix to ensure invalid time isn't sent to the system clock.
# gpspipe output is formatted and sent to the date program to update the date/time.
#
# Dependencies:
# gpsd
# gpsd-clients
ATTEMPT=0
GPSFIX=0
TIMESET=0
MODE=0
while [ $TIMESET -lt 1 ]
do
while [ $ATTEMPT -lt 12 ]
do
if [ $GPSFIX -lt 1 ]
then
while [ $MODE -lt 2 ]
do
MODE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"mode":([0-9]).*/\1/' | head -1`
GPSFIX=$MODE
let "ATTEMPT=ATTEMPT+1"
TIMESET=10
sleep 5
done
else
echo "GPS fixed, getting time..."
GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1`
echo "date -s $GPSDATE"
date -s $GPSDATE
TIMESET=1
ATTEMPT=100
fi
done
done
if [ $ATTEMPT = 100 ]
then
echo "Success"
else
echo "Unable to get time from GPS device."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.