nsweeting
Posts: 10
Joined: Sun Dec 23, 2012 3:56 pm

GPS time python module

Sat Jan 05, 2013 9:09 pm

Just thought I'd throw this piece of code out there. Feel free to download at:

http://code.google.com/p/gpstime/

Info:

Meant for use with SOC devices that either lack a real time clock or internet connection (such as a raspberry pi), gpstime will work with your GPS through the GPSd (http://gpsd.berlios.de/) utility to access the current time, and will apply that to your system clock.

Usage examples include remote data logging and chartplotters. I use this module to set the time on my raspberry pi which I use as a chartplotter with OpenCPN. In order to ensure that the tide tables are displaying the correct information, the system time must be set correctly.

The gpstime module requires the gps python modules included with GPSd to acess the NMEA stream. I have also included these modules with the gpstime download.

In order to function properly, your GPS must be connected and GPSd must be running (type 'gpsd' in the terminal).

BeJay
Posts: 32
Joined: Mon Aug 06, 2012 1:52 pm
Location: Western Australia

Re: GPS time python module

Sun Mar 17, 2013 3:30 pm

Hi nsweeting,

I just tried your code, but it seems to set the local time to UTC instead. Is there a way to correct this without adding a tz offset? I'm finding ntp over gpsd won't skew the clock to the correct time if it's too far out. This sounded like a great idea to work around this issue, but it's +8hrs out for me ;)

BeJay
Posts: 32
Joined: Mon Aug 06, 2012 1:52 pm
Location: Western Australia

Re: GPS time python module

Sun Mar 31, 2013 2:06 pm

Here is what I came up with to fix my issue of it only applying to local time....

Code: Select all

 #set pi clock with UTC 2013-03-16T00:37:55.000Z -> 16 MAR 2013 00:37:55
TIMEZ = 8	

if gpsd.utc != None and gpsd.utc != '':
		tzhour = int(gpsd.utc[11:13])+TIMEZ
		if (tzhour>23):
		   tzhour = (int(gpsd.utc[11:13])+TIMEZ)-24
		print str(tzhour)   
		gpstime = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + str(tzhour) + gpsd.utc[13:19]
		print 'Setting system time to GPS time...'
		os.system('sudo date --set="%s"' % gpstime)
		print gpstime          
This is working great in my project the CarGO

Cheers

Bj

BeJay
Posts: 32
Joined: Mon Aug 06, 2012 1:52 pm
Location: Western Australia

Re: GPS time python module

Sat Apr 06, 2013 10:07 am

There is actually a better and easier way to get the GPS to set UTC time. I didn't notice this until I had researched the "date" command for hours.... It's as simple as adding -u to the set string!

Hope it helps someone:

Code: Select all

#set pi clock with UTC 2013-03-16T00:37:55.000Z -> 16 MAR 2013 00:37:55
	if gpsd.utc != None and gpsd.utc != '':
		gpstime = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:13] + gpsd.utc[13:19]
		print 'Setting system time to GPS time...'
		# added -u to set clock to UTC time and not effect the timezone
		os.system('sudo date -u --set="%s"' % gpstime)

photosynthesis
Posts: 2
Joined: Tue Jun 10, 2014 12:34 am

Re: GPS time python module

Wed Jun 11, 2014 9:23 pm

I'm looking to use some kind of script like this to set both the system clock and a RTC I've added to the RPi. It will be used at an isolated tropical forest research site: all the other computers logging data need to be one the same time, even if it's not terribly accurate, so I would use the RPi+GPS for local network time.

However, I'm really a bare neophyte when it comes to coding and LINUX. I downloaded the file python files, but as there is no readme, I'm not sure how/where to install them in the system. Also, preferably I would use the GPS to set time on both the system clock (date) and the RTC (hwclock), and then use the RTC to set system time at reboot (we have frequent power outages, and I would like the RPi system time to be accurate during the wait time for the GPS to re-acquire; I've already taken care of the problem of a UPS that allows the RPi to safely shutdown and restart with power loss).

Any suggestions for the simplest way to do this?

Thanks.

User avatar
kj6oeo
Posts: 6
Joined: Thu Feb 19, 2015 3:26 pm
Location: Southern California, USA

Re: GPS time python module

Thu Feb 19, 2015 3:49 pm

This is all great. Thank you.

However I have some questions on the "gpsd.utc" function that is called.

Every time it is called, it is followed by '[x:x]' where x equals some number. Example "...gpsd.utc[0:4]..."

I have searched for an explanation on what the [numbers] do, but i can not seem to find one.

By chance, is there anyone out here that can shed some light on this?

Thank you

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: GPS time python module

Fri Feb 20, 2015 12:20 am

kj6oeo wrote:This is all great. Thank you.

However I have some questions on the "gpsd.utc" function that is called.

Every time it is called, it is followed by '[x:x]' where x equals some number. Example "...gpsd.utc[0:4]..."

I have searched for an explanation on what the [numbers] do, but i can not seem to find one.

By chance, is there anyone out here that can shed some light on this?

Thank you
The [x:x] is Python's indexing syntax. The two numbers represent the start and end of a range. In the case of a string we consider the string as an array of single-character elements, so the two numbers represent the start and end positions of a substring within that string.

Return to “General discussion”