Maker Pro
Raspberry Pi

How to read GPS data with Python on a Raspberry Pi

February 24, 2020 by Daniel Hertz
Share
banner

Learn how to use Python programming to gather Global Positioning System data and how to use it on your projects!

I recently wrote an article showing you how you can connect a common GPS module to your Raspberry Pi, and what it takes to configure the hardware so that you can determine the GPS position of the computer.

However, connecting the GPS module is just the first step and, because you have to manually type in commands, it’s not very useful in many projects. Therefore, in this article, I’ll explain how to use Python to obtain the positioning data from the GPS module and use it in your own projects.

Reading the Raw GPS Data From the Serial Port

Most GPS modules communicate with the Raspberry Pi via a simple serial connection. They send strings that contain GPS data and other status messages. These strings are called NMEA sentences. You can access those commands by directly reading the serial port that the GPS module is connected to by typing:

sudo cat /dev/serial0

This command will give you something like this:

GPS_Python_RaspberryPi_DH_MP_image3.png

Note that you’ll have to do this before you open a gpsd socket. Otherwise, the serial data is redirected to the socket. The GPGGA command contains the GPS fix data, which includes the position. If the GPS module can’t determine the position, it will most likely return empty fields (as shown in the image) or return zero.

To use the GPS data in Python, you can directly read the strings that the module sends to the Raspberry Pi’s serial port. However, you’ll have to do all the parsing and error handling yourself. I attached a short example program that reads the NMEA sentences directly from the serial port and prints the position to the console, at the end of this article. The output of this example program looks like this:

GPS_Python_RaspberryPi_DH_MP_image1.png

This approach works, and I recommend that you use it for non-critical applications. Furthermore, with this method, you don’t have to install any additional software. However, if you need your application to be more reliable and you don’t want to handle all the errors and other NMEA sentences, I recommend that you use a well-tested pre-built library that does all that for you.

Installing the gpsd-clients Module

If you followed the last article, then gpsd and the client framework should already be set up and running without a problem. If you didn’t, use the following command to install all necessary libraries and programs:

sudo apt-get install gpsd gpsd-clients

This should allow you to import the necessary modules when you open a Python console:

GPS_Python_RaspberryPi_DH_MP_image4.png

If you get an error when you import the GPS module, make sure that all necessary packages were correctly installed.

Using the Python Library to Parse GPS Data

The library we installed in the last step allows you to communicate with the GPS daemon that then communicates with the GPS receiver. gpsd uses JSON objects to communicate with its clients, so you’ll receive such JSON objects when you use the gpsd-clients library, which you can parse in your Python script.

In this tutorial, we’re not interested in sending requests to the GPS receiver. Instead, we’re only looking at responses. The responses contain classes and their names correspond to the NMEA message type. It’s important to note that these JSON objects can be incomplete if a value is undefined. This can happen when the GPS receiver doesn’t know your position. In such a case, the field is simply omitted. You can find a complete list of commands and more detailed descriptions in the official documentation.

Anyway, now we know that these responses are simple JSON objects where some fields may be omitted. Therefore, it’s a simple matter of reading and parsing the JSON fields to obtain the position data:

def getPositionData(gps):
	nx = gpsd.next()
	if nx['class'] == 'TPV':
    	    latitude = getattr(nx, 'lat', "Unknown")
                longitude = getattr(nx, 'lon', "Unknown")
    	    print "Your position: lon = " + str(longitude) + ", lat = " + str(latitude)

You can find the complete Python script at the end of this article. If you receive an error when executing the script, make sure that the gpsd server is running and that it’s linked to the correct serial port. If everything is working correctly, you should see something like this:

GPS_Python_RaspberryPi_DH_MP_image2.png

As you could see, it’s fairly easy to read values supplied by the GPS receiver. It’s not necessary to install any additional packages. However, I recommend that you use a library, such as the gpsd-client package, to communicate with the GPS receiver, as this makes it easier to handle many different commands and unexpected situations.

Reading GPS Data With Python Code

Author

Avatar
Daniel Hertz

Hi! I am a software engineer and owner of nerdhut.de who loves to experiment with electronics, gadgets and tech in general.

Related Content

Comments


You May Also Like