GPS

Compatible Devices – USB & Serial (UART)

I’ve successfully used the following three GPS devices:

USB GPS receiver
GlobalSat BU-353-S4 (USB)

GPS for UART interface
Adafruit GPS HAT (UART)

USB & Serial GPS receiver
52Pi EZ-0048 (USB or UART)



Most GPS devices present the data in serial RS232 format. Those GPS devices that connect via USB typically have a chip on-board (for example a PL-2303) which converts the data and presents it on a serial RS232 port – as such, the GPS device is likely to be reported as a USB to serial converter rather than a native GPS unit.

  • USB devices will be accessible on     /dev/ttyUSB0
  • UART devices will connect via the GPIO header, typically to pins:

    • Pin 1 – 3.3v
    • Pin 6 – 0v (Ground)
    • Pin 8 – UART0 Transmit (TX)
    • Pin 10 – UART0 Receive (RX)

     
    The device will be accessible at     /dev/ttyS0

    Note: In earlier versions of Raspbian Jessie, released before the Pi 3B hardware was available, the UART device would have been accessible at /dev/ttyAMA0. The Pi 3B introduced Bluetooth but the Bluetooth module required the use of the ‘full’ serial port which had previously been routed to the GPIO as /dev/ttyAMA0. As this was now reserved for the Bluetooth module, the ‘mini’ serial port which had previously been unused was routed to the GPIO header and called ttyS0.

Serial (UART) Set-up

If connecting the GPS unit via the GPIO header then the UART must first be configured by following the steps below. If the GPS unit is connected via USB then this section can be ignored.

sudo raspi-config

Select option 5 – Interfacing Options

Select Option P6 – Serial

Would you like a login shell to be accessible over serial?
NO

Would you like the serial port hardware to be enabled?
YES

That’s it, all done!

Reboot the Pi

sudo shutdown -r now

Once rebooted, you can test if the GPS unit is working by watching for data on the serial port. First, configure the serial port characteristics:

stty -F /dev/serial0 raw 9600 cs8 clocal -cstopb
  • -F /dev/serial0 = The device/port
  • raw = Raw data
  • 9600 = Data speed (baud), 9,600 bits per second
  • cs8 = Character size 8-bits
  • clocal = No modem control
  • -cstopb = One stop bits per character

Then view the data coming from the GPS unit:

cat /dev/ttyS0

You should get something like this:

In the above example:

  • $GPZDA = Date and Time – Hours 01, Minutes 22, Seconds 14.000, Day 4th, Month December, Year 2017, No_local_timezone_set, Checksum
  • $GPRMC = Recommended minimum data for GPS
  • $GPGGA = 3D Fix Information – Time of fix, Latitude, North or South, Longitude, East or West, Fix type, Number of satellites being tracked, Accuracy, Altitude above mean sea level, Meters, Height of mean sea level above WGS84, Meters, empty, empty, checksum
  • $GPGSA = Type of GPS fix and Number of satellites used – Auto select 2D/3D or manual, 1=no-fix 2=2D-fix 3=3D-fix, … , … , … upto 12 satellites being used … , precision, Horizontal precision, Vertical precision, Checksum
  • $GPGSV = Detailed Satellite Data
  • $GPTXT = Custom data sent from the GPS unit. Can be anything the creator wanted such as version numbers, antenna status

If your data lacks latitude and longitude information and in general has alot of commas with little data, then the GPS unit does not have a fix. It can take anywhere from 30 seconds to 30 minutes to establish a fix – to make it as quick and accurate as possible, ensure the antenna is outside or by a window with a clear view of a good proportion of the sky.

USB Set-up

If connecting the GPS unit via USB, then the only thing to be done is to ensure is has been recognised. If the GPS unit is connected via the GPIO header then this section can be ignored.

Before connecting the GPS unit, take a look at the USB devices already connected and the devices visible in the /dev directory:

lsusb

ls /dev



Connect the GPS unit and then check again, you should see the device present as highlighted in RED.


Install GPS Software (gpsd)

  • First, ensure that your operating system is up to date. See – Updating Raspbian
  • Second, we need to install a GPS Deamon called ‘gpsd’ which will allow us to easily interface with the GPS unit.
  • sudo apt-get install gpsd python-gps gpsd-clients

    pi@raspberrypi:~ $ sudo apt-get install gpsd python-gps gpsd-clients
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    The following extra packages will be installed:
      fontconfig fontconfig-config fonts-dejavu-core hicolor-icon-theme libatk1.0-0 libatk1.0-data libavahi-client3 libblas-common libblas3 libbluetooth3 libcairo2 libcups2 libdatrie1 libfontconfig1 libgdk-pixbuf2.0-0
      libgdk-pixbuf2.0-common libgfortran3 libgps21 libgraphite2-3 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libharfbuzz0b libjasper1 libjbig0 liblapack3 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpixman-1-0 libthai-data
      libthai0 libtiff5 libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxrandr2 libxrender1 python-cairo python-gobject-2 python-gtk2 python-numpy
    Suggested packages:
      cups-common librsvg2-common gvfs libjasper-runtime python-gobject-2-dbg python-gtk2-doc gfortran python-dev python-nose python-numpy-dbg python-numpy-doc
    The following NEW packages will be installed:
      fontconfig fontconfig-config fonts-dejavu-core gpsd gpsd-clients hicolor-icon-theme libatk1.0-0 libatk1.0-data libavahi-client3 libblas-common libblas3 libbluetooth3 libcairo2 libcups2 libdatrie1 libfontconfig1 libgdk-pixbuf2.0-0
      libgdk-pixbuf2.0-common libgfortran3 libgps21 libgraphite2-3 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libharfbuzz0b libjasper1 libjbig0 liblapack3 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpixman-1-0 libthai-data
      libthai0 libtiff5 libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxrandr2 libxrender1 python-cairo python-gobject-2 python-gps python-gtk2 python-numpy
    0 upgraded, 50 newly installed, 0 to remove and 0 not upgraded.
    Need to get 16.2 MB of archives.
    After this operation, 63.2 MB of additional disk space will be used.
    Do you want to continue? [Y/n] Y
    Get:1 http://archive.raspberrypi.org/debian/ jessie/main libbluetooth3 armhf 5.23-2+rpi2 [79.0 kB]
    Get:2 http://archive.raspberrypi.org/debian/ jessie/main libpixman-1-0 armhf 0.33.3+git20151011-7de61d8-rpi1 [156 kB]

    … 1 minute 12 seconds later on a Pi 3B …

    Setting up libgtk2.0-bin (2.24.25-3+deb8u1) …
    Processing triggers for libc-bin (2.19-18+deb8u9) …
    Processing triggers for systemd (215-17+deb8u7) …
    Processing triggers for python-support (1.0.15) …
    pi@raspberrypi:~ $

    Configure GPS Software (gpsd)

    To configure the GPS deamon to manage the GPS receiver for us. Edit the configuration file ‘/etc/default/gpsd’

    sudo nano /etc/default/gpsd

    For a serial (UART) GPS device, make the following changes: (additions in yellow)

    # Default settings for the gpsd init script and the hotplug wrapper.

    # Start the gpsd daemon automatically at boot time
    START_DAEMON=”true”

    # Use USB hotplugging to add new USB devices automatically to the daemon
    USBAUTO=”true”

    # Devices gpsd should collect to at boot time.
    # They need to be read/writeable, either by user gpsd or the group dialout.
    DEVICES="/dev/ttyS0"

    # Other options you want to pass to gpsd
    GPSD_OPTIONS="-n"

    GPSD_SOCKET="/var/run/gpsd.sock"

    For a USB GPS device, make the following changes: (additions in yellow)

    # Default settings for the gpsd init script and the hotplug wrapper.

    # Start the gpsd daemon automatically at boot time
    START_DAEMON=”true”

    # Use USB hotplugging to add new USB devices automatically to the daemon
    USBAUTO=”true”

    # Devices gpsd should collect to at boot time.
    # They need to be read/writeable, either by user gpsd or the group dialout.
    DEVICES="/dev/ttyUSB0"

    # Other options you want to pass to gpsd
    GPSD_OPTIONS="-n"

    GPSD_SOCKET="/var/run/gpsd.sock"

    Reboot and Test

    Reboot your Pi:

    sudo shutdown -r now

    Then test the GPS is working by running cgps:

    cgps

    you should get a screen similar to below with lines of GPS data scrolling beneath the box. (To exit, hit Ctrl-C)

    ┌───────────────────────────────────────────┐┌─────────────────────────────────┐
    │    Time:       2017-05-16T14:16:33.000Z   ││PRN:   Elev:  Azim:  SNR:  Used: │
    │    Latitude:    18.900182 N               ││  25    48    096    41      Y   │
    │    Longitude:    8.128378 W               ││  29    79    096    42      Y   │
    │    Altitude:   36.9 m                     ││  16    06    279    24      Y   │
    │    Speed:      0.0 kph                    ││   2    21    043    29      Y   │
    │    Heading:    0.0 deg (true)             ││   5    02    076    16      Y   │
    │    Climb:      0.0 m/min                  ││  12    06    097    33      Y   │
    │    Status:     3D FIX (5 secs)            ││  21    16    171    27      Y   │
    │    Longitude Err:   +/- 8 m               ││  23    08    333    22      Y   │
    │    Latitude Err:    +/- 10 m              ││  26    33    283    30      Y   │
    │    Altitude Err:    +/- 25 m              ││  31    66    253    31      N   │
    │    Course Err:      n/a                   ││  14    16    231    13      N   │
    │    Speed Err:       +/- 75 kph            ││ 123    22    139    00      N   │
    │    Time offset:     -1.623                ││                                 │
    │    Grid Square:     IO12ab                ││                                 │
    └───────────────────────────────────────────┘└─────────────────────────────────┘

    Alternatively you can run ‘gpsmon’ which is another tool similar to ‘cgps’ which displays the current GPS feed. (To exit, hit Ctrl-C)

    gpsmon

    tcp://localhost:2947
    ┌─────────── X ────── Y ────── Z ────────── North ──── East ───── Alt ─────────┐
    │Pos:  6151089  -442439  4996035 m       18.90023° -8.12828°       43 m        │
    │Vel:      0.0      0.0      0.0 m/s          0.0       0.0       0.0 climb m/s│
    │Time: 2017-05-16T14:34:28.000Z Leap: ??     Heading:   0.0°      0.0 speed m/s│
    │Fix:  9 = 29 31 26 25 21  2 16 23 14                 HDOP: 0.8  M1: 04  M2: 02│
    └─────────────────────── Packet type 2 (0x02) ─────────────────────────────────┘
    ┌ Measured Tracker ──────────┐┌ Firmware Version ──────────────────────────────┐
    │Ch PRN  Az El Stat  C/N ? SF││                                                │
    │ 0  25 102 40 00bf 33.3 T   │└─────── Packet Type 6 (0x06) ───────────────────┘
    │ 1  29  72 73 00bf 47.1 T   │┌ CPU Throughput ────────────────────────────────┐
    │ 2  16 283 13 00ad 24.5     ││Max: 0.000  Lat: 0.000  Time: 0.000   MS:  0    │
    │ 3   2  39 16 00ad 21.7     │└─────── Packet type 9 (0x09) ───────────────────┘
    │ 4   5  70  6 0000  0.0     │┌ Clock Status ──────────────────────────────────┐
    │ 5  12 102  0 0000  0.0     ││SVs:    Drift:        Bias:                     │
    │ 6  21 169 23 00ad 26.7     ││Est. GPS Time:            PPS offset:           │
    │ 7  23 327 10 00ad 24.8     │└─────── Packet type 7 (0x07) ───────────────────┘
    │ 8  26 286 41 00bf 29.5 T   │┌ Visible List ──────────────────────────────────┐
    │ 9  31 232 64 00ad 24.0     ││13 = 29 31 26 25 21 2 16 23 14 5 193 12 134     │
    │10  14 226 10 00ad 20.2     │└─────── Packet type 13 (0x0D) ──────────────────┘
    │11 136 169 30 0000  0.0     │┌ DGPS Status ───────────────────────────────────┐
    └─── Packet Type 4 (0x04) ───┘│                                                │
                                  └─────── Packet type 27 (0x1B) ──────────────────┘

    That is it – Congratulations! Your GPS is working.

    Using an external antenna on the adafruit

    The adafruit GPS HAT will automatically switch to using an external antenna if one is connected. To check which antenna it is using you must send it the command $PGCMD,33,1*6C which tells it to include the antenna status in the stream of GPS data. To send the command:

    • If the GPS daemon ‘gpsd’ is running you must first stop the daemon as it will have ownership of the device and you will receive an error similar to ‘/dev/serial0: Device or resource busy’.
      sudo killall gpsd
    • Configure the serial port so that we communicate using the correct format:
      stty -F /dev/serial0 raw 9600 cs8 clocal -cstopb
    • Issue the command:
      echo '$PGCMD,33,1*6C' > /dev/ttyS0
    • Watch the serial data for lines beginning with $PGTOP
      cat /dev/ttyS0

      $PGTOP,11,2*6E = Internal Antenna
      $PGTOP,11,3*6F = External Antenna

Leave a Reply

Your email address will not be published. Required fields are marked *