Setting the Raspberry Pi System Time using a GPS

[et_pb_section fb_built=”1″ _builder_version=”3.17.6″][et_pb_row _builder_version=”3.17.6″][et_pb_column type=”4_4″ _builder_version=”3.17.6″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.17.6″]

Having an accurate time source is important for some digital modes, and normally not a problem when your Raspberry Pi is connected to the internet.

But what about when you are at sea or in the middle of the forest or anywhere else with no internet connection?

I tried setting up the Raspberry Pi to use the GPS NMEA source as an NTP server following some instructions that others had some success with, but for some reason I couldn’t get mine to work.

So I thought I would figure out how to do it myself, but as usual these days it doesn’t take long to find someone who has already done it – https://wb4son.com/wpblog/?p=1635 have a read, it explains it all, below is the script.

 

[/et_pb_text][et_pb_text _builder_version=”3.17.6″]

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.17.6″][et_pb_column type=”4_4″ _builder_version=”3.17.6″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.17.6″]

 

#! /usr/bin/python

import os
import sys
import time
from gps import *

print ‘Set System Clock to GPS UTC time’

try:
    gpsd = gps(mode=WATCH_ENABLE)
except:
    print ‘ERROR: No GPS found, the time has not been set’
    sys.exit()

   while True:
       #wait until the next GPSD time tick
      gpsd.next()
      if gpsd.utc != None and gpsd.utc != ”:
     #gpsd.utc is formatted like”2015-04-01T17:32:04.000Z”
     #convert it to a form the date -u command will accept: “20140401 17:32:04”
     #use python slice notation [start:end] (where end desired end char + 1)
     # gpsd.utc[0:4] is “2015”
     # gpsd.utc[5:7] is “04”
     # gpsd.utc[8:10] is “01” 
     gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ‘ ‘ + gpsd.utc[11:19]
     os.system(‘sudo date -u –set=”%s”‘ % gpsutc)
     sys.exit()

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.17.6″][et_pb_column type=”4_4″ _builder_version=”3.17.6″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.17.6″]

Copy the above to a file and save it, I called my setclock.py

make it executable with this command

    chmod a+x set clock.py

 

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section][et_pb_section fb_built=”1″ _builder_version=”3.17.6″][et_pb_row _builder_version=”3.17.6″][et_pb_column type=”4_4″ _builder_version=”3.17.6″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.17.6″]

In order for this to work you must first install and configure gpsd

1. Install the relevant GPS software using the following command:

sudo apt -y install gpsd gpsd-clients python-gps

2. Now edit the configuration file:

sudo nano /etc/default/gpsd

3. The setting should look like this (update as necessary):

    START_DAEMON=”true”

    USBAUTO=”false”

    DEVICES=”/dev/ttyACM0″

    GPSD_OPTIONS=”-n”

4. Save the file and Reboot the Raspberry Pi

sudo reboot

5. You can check if the gpsd service is active using this command:

systemctl is-active gpsd

   and you can see the output from the GPS using

gpsmon -n

 

Once that’s all done you can run the python script to set the time

   ./setclock.py

 

if you want to test that its actually working simply set the time to something else, then run the script (note the desktop clock does not update right away so ignore that and use the command line)

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

One thought on “Setting the Raspberry Pi System Time using a GPS

  1. Hi Mark
    Really happy you got it working but I’m surprised the updated instructions didn’t work for you. Crony has replaced gpsd on Raspbian Stretch. I tried gps approach on two different Raspbian Stretch pis with no success.

Comments are closed.