Below is an example python script which will save GPS data (time, Lon, Lat, speed and sats in view) to a file.
The gpsd client libraries will be used to get the data from GPSD. We will be using the TPV class to get time, latitude, longitude and speed.
We can get the number of satellites in view by getting the length of the satellites object.
This page shows how to get gpsd up an running on a Raspberry Pi
Every time the script is run, it will create a new file beginning with the current date and time.
In this example, I am writing in a csv format, where each GPS attribute is separated by a comma.
#! /usr/bin/python from gps import * import time, inspect f = open(time.strftime("%Y%m%d-%H%M%S")+'_GSPData.csv','w') gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE) print 'GPStime utc\t\t\tlatitude\tlongitude\tspeed\tsats in view' # '\t' = TAB to try and output the data in columns. f.write("GPStime utc,latitude,longitude,speed,sats in view\n") try: while True: report = gpsd.next() # if report['class'] == 'TPV': GPStime = str(getattr(report,'time','')) lat = str(getattr(report,'lat',0.0)) lon = str(getattr(report,'lon',0.0)) speed = str(getattr(report,'speed','nan')) sats = str(len(gpsd.satellites)) print GPStime,"\t", print lat,"\t", print lon,"\t", print speed,"\t", print sats,"\t" f.write(GPStime + ',' + lat +',' + lon + ',' + speed + ',' + sats + '\n') time.sleep(1) except (KeyboardInterrupt, SystemExit): #when you press ctrl+c print "Done.\nExiting." f.close()
Hello Mark, Thanks for the script. What is the 1+ for in the first of these two lines copied from the script :
lat = str(1+getattr(report,'lat',0.0))
lon = str(getattr(report,'lon',0.0))
Regards,
Neil
Well spotted!. that was me testing, adding 1 to latitude value. I have removed the extra"1"
Mark love you're GPS tutorials, how about taking it to the next level push a button to create a new track, push another button to create a waypoint, all the data (track logs, waypoints) saved in the GPX or KML format so the data can be imported directly into Google earth or other programs.
Thanks in advance.
Nobody reads the comments?