I bought a few Raspberry Pis with great ambition of doing something awesome with them. Beyond showing my co-workers how cool I was for having them, the Raspberry Pis pretty much just sat on my desk collecting dust. A month or two after getting them I realized that the Pis would fit nicely into our RallyRecorder project as a GPS tracker/recorder. Previously we had this running on a netbook with a USB GPS receiver using GPSd, a combination of command-line PHP and shell scripts, and MySQL. With the reduced specs on the Raspberry Pi (700 MHz, 256 MB RAM), I wanted to go with something with a little less overhead. Wisely or not, I chose Python and SQLite. Not having worked with either of the before, it should also provide a nice challenge.
After I started looking around the web for info about getting GPSd working with Python, I quickly found that any information was out of date, out of version, or just didn’t compile. It didn’t help that I started learning Python last night. Hey, go big or go home, right?
First we’ll quickly go through how to get all of the software prereqs. This is written for the Raspberry Pi but should work on most Linux distros too.
- Raspberry Pi with Debian Wheezy installed (or something running Linux)
- GPSd compatible GPS Receiver (I used this one)
Getting the Software
If you have a USB GPS receiver, don’t plug it in yet.
From the command-line, enter the following command to install Python, GPSd, and the Python modules to bring them together.
If you have a USB receiver, you can plug it in now. For Bluetooth and serial receivers, you’re on your own for this step. Plugging in the USB receiver should start GPSd automatically. To make sure that GPSd is playing nice, you can open cgps to see what data it is receiving.
After your GPS receiver has a satellite fix, it should look something like this:
Reading GPS data with Python
Now that GPSd is communicating successfully with your receiver and you have the right software installed, we can read in the GPS data through Python. The code below uses threading to get every GPS update (otherwise the buffer fills up) and when requested (lines 31-54) fetches the most recent set of data. You can replace lines 31-54 with whatever you want to do with the data. I plan on storing unique points in a SQLite database (maybe in a follow-up post).
To use this code, copy/paste all of it in to a text editor of you choosing and save it as gpsdData.py. You can then run it with the following command:
I am by no means an expert in well… anything. But this does work with GPSd 3.6, Python 2.7.3, and Debian Wheezy. I am all ears with ways to make this better and more efficient. Please let me know in the comments.
Some resources that helped me put this all together: