Posts: 2
Joined: Mon Nov 07, 2016 4:27 am

GPS Sensor & Python

Mon Nov 07, 2016 8:58 am

Hi Folks
I am working on my first Raspberry Pi project. Its objective is to capture time-stamped GPS data. I am new to Python. As such I am looking for simplest hardware and software setup to maximize my probably of success.

I am looking for recommendations on the best way forward- specifically
1) Should I use a USB GPS sensor rather than one connected through the GPIO pins? I have been looking at this USB GPS sensor : , but if anyone can recommend a better option I would be appreciative.

2) Can someone recommend a step by step tutorial for the basic capturing of GPS data by Python ? From my reading, it sounds like the package 'gpsd' is useful to act as a layer between my applications and the actual GPS hardware.

3) Any other guidance would be appreciated. .I am using Python 3.4, and a Raspberry PI 3


Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: GPS Sensor & Python

Mon Nov 07, 2016 10:44 am

Hi there,

If you want the simplest hardware then you've probably found it. Plug in the GPS into USB and it should just work. If you use the GPIO then you have to fiddle with wires and remember to buy one that uses 3.3V instead of 5V.

When you plug it in then the OS will create a new serial port device. You need to know what it is, but it might be something like /dev/ttyUSB0

For the simplest software you could easily use PySerial to open the serial port and start reading characters. If you look for the $GPRMC sentence you can get the date, time, and position by picking the string apart at the commas.

I personally don't like gpsd. It likes to take over the operation of the GPS entirely, which might be what you want. If you use gpsd then you write your software to ask gpsd for information. You don't ask the GPS module itself.

I recommend that you plug in the GPS, figure out the serial port, and look at the incoming data stream to see what the data looks like (use gtkterm or screen). It's quite fun to watch the data scroll by, usually a new set of data every second. Then Google around for some tutorials on parsing NMEA (this is the name of the GPS data format).
My circuit's dead, there's something wrong.

User avatar
Posts: 4739
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: GPS Sensor & Python

Mon Nov 07, 2016 1:46 pm

pynmea2 will do the faff part for you.

Posts: 5133
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPS Sensor & Python

Mon Nov 07, 2016 2:50 pm

here's some python to read a gps on usb...

Code: Select all

    import serial
    import os
    from decimal import *
    getcontext().prec = 8
    fix = 0
    gps_con = 0

    while  True:
        # check for gps connected on USB0 or USB1
       if gps_con == 0 and os.path.exists('/dev/ttyUSB0') == True:
          ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
          gps_con = 1
          print "connected on USB0"
       elif gps_con == 0 and os.path.exists('/dev/ttyUSB1') == True:
          ser = serial.Serial('/dev/ttyUSB1',4800,timeout = 10)
          gps_con = 1
          print "connected on USB1"
       if gps_con == 1:
          gps = ser.readline()
          if gps[1 : 6] == "GPGGA":
             gps1 = gps.split(',',14)
          if gps[1:6] == "GPGSA":
             fix = int(gps[9:10])
          if gps[1 : 6] == "GPGGA" and len(gps) > 68 and (gps1[3] == "N" or gps1[3] == "S")and fix > 1:
             lat = int(gps[18:20]) + (Decimal(int(gps[20:22]))/(Decimal(60))) + (Decimal(int(gps[23:27]))/(Decimal(360000)))
             if gps[28:29] == "S":
                lat = 0 - lat
             lon = int(gps[30:33]) + (Decimal(int(gps[33:35]))/(Decimal(60))) + (Decimal(int(gps[36:40]))/(Decimal(360000)))
             if gps[41:42] == "W":
                lon = 0 - lon
             print "LAT:" ,lat
             print "LON:",lon
          if gps[1 : 6] == "GPRMC" and fix > 1:
             gps2 = gps.split(',',14)
             print "SPEED:",gps2[7]
             print "ANGLE:",gps2[8]
             print ""

Posts: 2
Joined: Mon Nov 07, 2016 4:27 am

Re: GPS Sensor & Python

Mon Nov 07, 2016 7:26 pm

Thanks for the suggestions guys- That has given me a great starting point.

Return to “Beginners”