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:
This command will give you something like this:
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:
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:
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:
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:
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