Getting GPSd to work with Python and Threading

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.

Required Hardware

  • 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.

sudo apt-get install python gpsd gpsd-clients

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).

#! /usr/bin/python
# Written by Dan Mandle September 2012
# License: GPL 2.0

import os
from gps import *
from time import *
import time
import threading

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):
	def __init__(self):
		global gpsd #bring it in scope
		gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
		self.current_value = None
		self.running = True #setting the thread running to true

	def run(self):
		global gpsd
		while gpsp.running: #this will continue to loop and grab EACH set of gpsd info to clear the buffer

if __name__ == '__main__':
	gpsp = GpsPoller() # create the thread
		gpsp.start() # start it up
		while True:
			#It may take a second or two to get good data
			#print gpsd.fix.latitude,', ',gpsd.fix.longitude,'	Time: ',gpsd.utc


			print ' GPS reading'
			print '----------------------------------------'
			print 'latitude    ' , gpsd.fix.latitude
			print 'longitude   ' , gpsd.fix.longitude
			print 'time utc    ' , gpsd.utc,' + ', gpsd.fix.time
			print 'altitude (m)' , gpsd.fix.altitude
			print 'eps         ' , gpsd.fix.eps
			print 'epx         ' , gpsd.fix.epx
			print 'epv         ' , gpsd.fix.epv
			print 'ept         ' , gpsd.fix.ept
			print 'speed (m/s) ' , gpsd.fix.speed
			print 'climb       ' , gpsd.fix.climb
			print 'track       ' , gpsd.fix.track
			print 'mode        ' , gpsd.fix.mode
			print 'sats        ' , gpsd.satellites

			time.sleep(5) #set to whatever

	except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
		print "\nKilling Thread..."
		gpsp.running = False
		gpsp.join() # wait for the thread to finish what it's doing
	print "Done.\nExiting."

To use this code, copy/paste all of it in to a text editor of you choosing and save it as 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:


  1. Pingback: GPS Python Module Error | Code and Programming

  2. Bruno

    I have the exact same environment as you , but I get this error:

    Traceback (most recent call last):
    File “”, line 29, in
    gpsp = GpsPoller() # create the thread
    File “”, line 19, in __init__
    gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
    NameError: global name ‘gps’ is not defined

    1. Jorge Alfaro

      Make sure you haven’t named your file “” otherwise python can’t import the library,

      You can also copy the library to the directory where you have your script to make sure it can be imported.

  3. JRI

    Thanks for your post – it helped me get a gpsd output in python. It made me realise that gps.fix doesn’t hold any useful data until you have called enough times to read in some helpful status lines from gpsd.

    Also, I managed to get it working with a QStarz bluetooth gps. I used “sudo apt-get install bluetooth bluez-utils –no-install-recommends” to install a minimal set of bluetooth drivers (no GUI support), then used the instructions at to set up the RFCOMM port and configure gpsd to use it from startup.

    The only catch I had was getting my bluetooth dongle working. To start with I could detect the USB dongle, but not bring up the interface with hcitool. I had to add “dwc_otg.speed=1” to my /boot/cmdline.txt file. That downgrades the speed of the USB bus from USB 2.0 to USB 1.1 – it seems there is a kernel bug with USB support. While changing the bus speed fixed the problem for me, other people report it causes problems with ethernet and other USB devices.

  4. Pingback: Raspberry Pi Portable 3G/4G Network Quality Logger Project – Part 1 | Anthony Agius

  5. DMD

    Thanks a lot for your post!!! :-) It helped me a lot, I finally get the longitude and latitude data from the GPS in python. At first I also got the same error message (“global name ‘gps’ is not defined”). It turned out I had another program in the same folder called, after I renamed it, the code worked perfectly.
    I am using a Garmin GPS18xUSB, no other code addressing the GPS device directly worked for me.

    1. Dan Mandle

      Thanks! I’m glad we can share! I’m ‘borrowing’ your code from your OBD-II reader for the Pi to create a car logger! Been busy, so I haven’t been able to work on it, but I’ll let you know when it’s ready! Also let me know if you’d be interested in collaborating!

  6. maximran

    I have a question, i want to use python for my project, and i already install the gpsd and already copy your coding. However, when i try to run it on idle3, it say there are no module name gps. (error at line 5). What should i do?

    1. Dan Mandle

      Unfortunately this is the only project I’ve done with Python, so I can’t help you. Hopefully someone who sees this and can help!

      1. maximran

        Ok, i get the problem. I am using the python 3 (idle3= python 3) and in python 3 there are no modules name GPS.
        now i am using idle (python) , but i encountered a different problem… it related to something with ” non-ASCII character ‘\xc2’ ”
        do you know anything about it?

        1. Kolumbus42

          Hello, there is no gps module for python3. But i solved the problem using memory mapped files (shared memory) between a gps requester working with python2 and the gps module and a thread which reads the current coordinates from the shared memory with python3. Both have a class design for reuse.

  7. icon

    Dude, you are my hero ! :)
    Since the info on Python and gpsd from PerryGeo (just google it) is so outdated, it did not work at all.
    Thanks to your little code example, now finally everything works exactly as expected. Fantastic !

  8. traveldoors

    your script is working good for me
    how can i simply log this data on a CSV file

    i have tried different thing but i am not good in programing
    must be simple , but didn t sucess

    thank you , for all your answer

  9. Devon

    Thank you! This post is awesome. The code actually works for me without any modification. I now need to have this data streamed on an actual web page…

  10. Me

    Thanks a lot for the code! It’s a great piece of code for figuring out threading in python as well as how to interface with gpsd. It worked pretty much right out of the box with a USB puck on my raspberry B+. One thing, i also ran into the “global name ‘gps’ is not defined” at first. Turns out you cannot call the code gps-py, rather name it or something.

  11. Robert

    hi all

    i am having a problem with latitude and longitude readings.
    It seems to only appear 0.0 for both readings.
    any help?

Leave a Reply