10-10-2019, 02:51 PM
(This post was last modified: 10-10-2019, 03:29 PM by Daniel_BlueWave.)
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
then copy/paste into GPS-TIME.py (DONT FORGET TO SET YOUR LOCAL TIME ZONE at "TimeOffset =" in this script)
then make it executable by
then open your rc.local with
and insert the following line before exit0
and finally
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
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
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
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)
#!/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
sudo chmod +x /home/pi/GPS-TIME.py
then open your rc.local with
sudo nano /etc/rc.local
and insert the following line before exit0
sudo /home/pi/GPS-TIME.py &
and finally
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
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