Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
External GPS and time synchronization
#1
Im using this dongle:
https://www.amazon.com/GlobalSat-BU-353-...B008200LHW

ive tried many options and the following is working great for me.

follow my steps and it should work on any usb gps dongles that support linux os.

first
Code:
sudo nano GPS-TIME.py

then copy/paste into GPS-TIME.py (DONT FORGET TO SET YOUR LOCAL TIME ZONE at "TimeOffset =" in this script)

Code:
#!/usr/bin/python
import serial
import os
from decimal import *
getcontext().prec = 4

# enter your time zone offset

TimeOffset = +2

gpsfound = 2
settime = 0
count = 120

if os.path.exists('/dev/ttyUSB0') == True:
  gpsfound = 0
  ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
else:
  print "No GPS RX found on USB0"
if os.path.exists('/dev/ttyUSB1') == True and gpsfound == 2:
  gpsfound = 1
  ser = serial.Serial('/dev/ttyUSB1',4800,timeout = 10)
else:
  if gpsfound == 2:
      print "No GPS RX found on USB1"
     
if gpsfound < 2: 
  Months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
  MDays = [31,28,31,30,31,30,31,31,30,31,30,31]
  Days = ['Thu','Fri','Sat','Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun','Mon','Tue','Wed']
  count1 = 1
  locked = 0
  found = 0
  while locked == 0 and count1 <= count:
      gps = ser.readline()
      if gps[1 : 6] == "GPGGA":
        if found == 0:
            print "GPS RX found on USB" + str(gpsfound)
            print "Waiting for lock...."
            found = 1
        if int(gps[43]) > 0 and gps[42] != "*":
            locked = 1
      count1 +=1
  if count1 < 120:
      print "Locked"
  else:
      print "Signal Unlocked, aborting time setting"

  count2 = 1
  cday = 0
  Leap = 0
  while settime == 0 and count1 <= count and count2 <= count:
      gps = ser.readline()
      if gps[1 : 6] == "GPRMC":
        gps2 = gps.split(',',12)
        fulldate = gps2[9]
        fulltime = gps2[1]
        clock = ":" + fulltime[2:4] + ":" + fulltime[4:6]
        Hour = int(fulltime[0:2])
        Hour = Hour + TimeOffset
        if Hour > 23:
            Hour = Hour - 24
            cday = 1
        if Hour < 0:
            Hour = Hour + 24
            cday = -1
        if Hour < 10:
            Hour = "0" + str(Hour)
        else:
            Hour = str(Hour)
        Year = int(fulldate[4:6])
        Mon = int(fulldate[2:4])
        Day = int(fulldate[0:2])
        if cday == 1:
            Day = Day + 1
            if Day > MDays[Mon-1]:
              Mon = Mon + 1
              Day = 1
              if Mon > 12:
                  Year = Year + 1
                  Mon = 1
        if cday == -1:
            Day = Day - 1
            if Day < 1:
              Mon = Mon - 1
              Day = MDays[Mon-1]
              if Mon < 1:
                  Year = Year - 1
                  Mon = 12
                  Day = MDays[Mon-1]

        Month = Months[Mon-1]
        Day = str(Day)
        Years = "20" + str(Year)
        x = 0
        YDays = 0
        while x < Mon - 1:
            YDays = YDays + MDays[x]
            if (2000 + Year)/4 == (2000+ Decimal(Year))/4 and x == 1:
              YDays = YDays + 1
              Leap = 1
            x +=1
        YDays = YDays + int(Day)
        Weekday = (YDays - ((YDays/7)*7))+(Year - 16) + int(Decimal(0.5) + (Decimal(Year)-Decimal(15))/Decimal(4))
        path = "sudo date -s '" + Days[Weekday] + " " + Month + " " + str(Day) + " " + Hour + clock + " " + Years + "'"
        print path
        os.system (path)
        settime = 1
      count2 +=1

if settime == 1:
  print "Time set with Time Offset to UTC " + str(TimeOffset) + " Hour(s)"
else:
  print "Unable to set time"

then make it executable by
Code:
sudo chmod +x /home/pi/GPS-TIME.py

then open your rc.local with
Code:
sudo nano /etc/rc.local

and insert the following line before exit0
Code:
sudo /home/pi/GPS-TIME.py &

and finally
Code:
sudo reboot


if your usb gps dongle is connected to your raspberry and no internet acces is available, then your time should be sync via usb gps dongle.

test:
turn off wifi on your rpi, unplug ethernet cable .. turn off your rpi and wait for 5 minutes or more.
with gps dongle connected turn your pi on and see if your time is correct. if you dont get the correct time then opebn your terminal and type
Code:
sudo /home/pi/GPS-TIME.py

it sometimes happens (mostly on cold start) that gps dongle doesnt yet have the time information when script is runned on boot, thus time sync doesnt happen.
i made a crontab that runs this scripts again one minute after rpi boots and then again 1 minute after.
you could most certantly modify this script so it would keep rerunning until gps time is sync, but i really dont know how to do this
Reply
#2
Post moved to not be lost.

Some more info:
https://www.raspberrypi.org/forums/viewt...hp?t=56993
Reply
#3
oh you found the original post. was looking and couldnt find "original owner" of the script I use
Reply
#4
Thanks for this, I'll give this a try the next time I pull the car out of the garage.
Hope it works!
Reply
#5
so based on your experience which option is less CPU intensive, RTC or GPS?
Reply
#6
I really don't know which would be more cpu intensive. Probably non as they both only run at boot and not the whole time. (but usb gps dongle does require more power than rtc, 0.2amps idling in my case)

As I said before I'm using usb gps for time sync because some of the rtc needed gpio pins are used for screen brightness sensor.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)