Adding GPS to the Raspberry Pi
You want to connect a serial GPS module to a Raspberry Pi and access the data using Python.
A 3.3V serial GPS module can be connected directly to Raspberry Pi’s RXD connection. However, to be able to use the serial port, you need to disable the console logging feature, so first follow Recipe 8.7.
Figure 11-14 shows how the module is wired. The RXD of the Raspberry Pi connects to Tx of the GPS module. The only other connections are for GND and 5V, so we can easily just use three female-to-female headers.
GPS messages require some decoding. Fortunately, there is a good suite of tools to help us do this. Install the following packages:
$ sudo apt-get install gpsd $ sudo apt-get install gpsd-clients |
The most important of these is gpsd. This is a tool that reads GPS data from a serial or USB connection as well as other sources and makes it available for client programs to use by providing a local web service on port 2748.
To start the gpsd service running, issue the following command:
$ sudo gpsd /dev/ttyAMA0 |
You can see if it is working by entering the command:
$ cgps -s |
The -s is optional; it just suppresses the display of the raw data (see Figure 11-15).
The third package that we installed (python-gps) is, as you might expect, a Python library for accessing the GPS data in a nice, convenient form. We will use python_gps with a short test program to display just the latitude, longitude, and time.
Open an editor (nano or IDLE) and paste in the following code. Do not call the file gps.py, or it will conflict with the Python GPS library. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called gps_test.py.
from gps import * session = gps() session.stream(WATCH_ENABLE|WATCH_NEWSTYLE) while True: report = session.next() if report.keys()[0] == ‘epx’ : lat = float(report[‘lat’]) |
Run the program, and you should see some trace like this:
$ python gps_test.py lat=53.710257 lon=-2.664245 time=2013-08-01T08:06:24.960Z lat=53.710258 lon=-2.664252 time=2013-08-01T08:06:25.960Z lat=53.710258 lon=-2.664252 time=2013-08-01T08:06:25.960Z lat=53.710248 lon=-2.664243 time=2013-08-01T08:06:26.960Z lat=53.710248 lon=-2.664243 time=2013-08-01T08:06:26.960Z lat=53.710248 lon=-2.664250 time=2013-08-01T08:06:27.960Z |
Discussion
The program creates a session and then establishes a stream of data to be read. The GPS will repeatedly spit out messages in different formats. The if command selects just the messages we want, those that contain the positional information. The parts of the message are stored in a dictionary from which the fields can be accessed and displayed.
Besides using the GPS data with Python, you can also use the xgps tool to display the GPS data (Figure 11-16). Just enter the command:
$ xgps |
This utility requires a display, so you should run it from the Raspberry Pi itself or using VNC (Recipe 2.8).
See Also
You can use the same approach using a USB GPS module.
Find out more about gpsd.