Close

Connecting GPS sensor to Pi Zero

A project log for Raspberry Pi Zero FPV camera and OSD

Pi Zero and camera weigh 13 grams making it an ideal setup for data logging, HD video recording and custom On Screen Display for FPV.

maksim-surguyMaksim Surguy 09/16/2016 at 23:340 Comments

There are many tutorials on connecting GPS to a Raspberry Pi available online so I followed some of them to connect a GPS sensor I had laying around to the Pi Zero.

The best resource I found for this was https://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi which has a full explainer on how to connect, configure and troubleshoot your GPS sensor.

I had to use 3.3V -> 5V logic converter to make sure there is not excess voltage from the sensor. When I got the GPS sensor to work on the Pi Zero, I was faced with a few choices on how to retrieve the data from it in my Python program:

- Run "gpsd" service in the background and grab nicely formatted data from it in Python application

- Read raw NMEA data from the serial interface directly and parse it in the Python application on the fly

I have tried both methods and because I do not have that much experience with Python and threading, I had plenty of obstacles with either one. The following code somewhat worked to retrieve altitude, position and ground speed from my sensor's serial data stream:

import serial
import pynmea2
import string 

...
sentence = serialStream.readline()
  if sentence.startswith('$GPGGA') :
    data = pynmea2.parse(sentence)
    gpsLocationText = "TIME: {time}, POS: {lat}, {lon}".format(time=data.timestamp, lat=data.latitude, lon=data.longitude)
    gpsAltitudeText = "ALT: {altitude}, {units}".format(altitude=data.altitude, units=data.altitude_units)
  if sentence.startswith("$GPVTG") :
    data = pynmea2.parse(sentence)
    gpsSpeedText = "Speed: {speed} Kmh".format(speed=data.spd_over_grnd_kmph)
  if sentence.startswith("$GPRMC") :
    data = pynmea2.parse(sentence)
    gpsSpeedText2 = "Speed: {speed} Kmh".format(speed=data.spd_over_grnd)
One problem I had with this code is that it needs to run in a continuous thread that is separate from the application in which my camera overlay is being constructed and I just don't know how to work with Python to do that. Perhaps you have some pointers on how to continuously read serial data while sending that data to main application?

What worked fine for me but made the RPi's CPU a lot more loaded was GPSD service with its Python bindings. I followed the tutorial here to get it to work: http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/.

Adding GPSD as a service made the CPU about 20% more loaded for some reason but the Pi Zero was still OK with this kind of load and had another 40-50% to spare.

Another big problem I encountered was running my Python application that uses GPSD on startup instead of running it by SSHing into the Pi. I have tried a lot of different solutions but still couldn't make the GPS coordinates show up if I just restarted the Pi and booted into my application.

Short term solution was for me to SSH into the Pi and start the Python application from the console:

Discussions