Using your GPS

Now that your GPS is up and running, and gpsd is playing nice with it, it's time to do something with the data!

The easiest way to get started is using a bit of Python code to access gpsd.  First, install the gps module from PyPI:

Download: file
pip3 install gps

Next, use nano or the text editor of your choice to save the following code in a file called gpstest.py:

Download: file
import gps

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
        report = session.next()
        # Wait for a 'TPV' report and display the current time
        # To see all report data, uncomment the line below
        # print(report)
        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                print(report.time)
    except KeyError:
        pass
    except KeyboardInterrupt:
        quit()
    except StopIteration:
        session = None
        print("GPSD has terminated")
This should give you something similar to the following (with an update every second or so):

Looking for position data rather than just the timestamp? Essentially, all you have to do is parse the 'report' data following the example above.

To see what data is available, you can uncomment the print(report) line, and then just look at the different values and class names and pick and choose whatever you want.

For example, you could use the following code to get the current speed using the TPV class:

Download: file
    if report['class'] == 'TPV':
	    if hasattr(report, 'speed'):
		    print(report.speed * gps.MPS_TO_KPH)
That's it! It's pretty painless, and now it's up to you to figure out what you want to do with you latitude, longitude, date and time, speed, altitude, etc.!
This guide was first published on Jan 24, 2013. It was last updated on Jan 24, 2013. This page (Using your GPS) was last updated on May 24, 2019.