Question 11.10: Adding GPS to the Raspberry Pi You want to connect a serial ......

Adding GPS to the Raspberry Pi

You want to connect a serial GPS module to a Raspberry Pi and access the data using Python.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

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’])
lon = float(report[‘lon’])
print(“lat=%f\tlon=%f\ttime=%s” % (lat, lon, report[‘time’]))
time.sleep(0.5)

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.

11.14
11.15

Related Answered Questions

Question: 11.13

Verified Answer:

Use an RTC (real-time clock) module. A very common...
Question: 11.8

Verified Answer:

Keypads are arranged in rows and columns, with a p...
Question: 11.2

Verified Answer:

Record the last state of the button and invert tha...
Question: 11.1

Verified Answer:

Connect a switch to a GPIO pin and use the RPi.GPI...
Question: 11.7

Verified Answer:

Use a rotary (quadrature encoder) connected to two...
Question: 11.9

Verified Answer:

Use a PIR (passive infrared) motion detector modul...
Question: 11.12

Verified Answer:

The solution to this is very similar to that of us...
Question: 11.11

Verified Answer:

There are at least two ways to solve this problem....