GPS on Orange Pi 2G IOT

Recently I got myself a GPS module to play with such as this. Not that I knew much about but felt like something nice to play with. First thing first is connecting the GPS module to the GPIO in Orange Pi 2G. In my case I decided to connect the GPS to ttyS2, asides from VCC (2.8V) and GND. This correspond to pins 1, 6, 8 and 10.
Vcc -> pin 1
GND -> pin 6
TxD -> pin 8
RxD -> pin 10

Pin 1 would be the one just below a small white arrow to the left of the SD card. Below two pictures of GPS connected to Orange Pi GPIO pins.


Another important thing is to cross TxD and RxD cables, that means connecting GPS TxD to Orange Pi RxD and GPS RxD to Orange Pi TxD.
Now that the GPS module is connected to the Orange Pi power it up and log into it. We will now check if the GPS mocule is working, in order to do this we will connect via minicom.

user@computer:$ minicom -D /dev/ttyS2 -b 9600
Welcome to minicom 2.7

OPTIONS: I18n
Compiled on Apr 26 2017, 00:45:18.
Port /dev/ttyS2, 19:10:00

Press CTRL-A Z for help on special keys

$GPRMC,191013.00,A,4252.31265,N,7833.00897,W,0.180,,040518,,,A*64
$GPVTG,,T,,M,0.180,N,0.333,K,A*29
$GPGGA,191013.00,4252.31265,N,7833.00897,W,1,06,2.16,296.4,M,51.3,M,,*44
$GPGSA,A,3,22,16,10,08,26,27,,,,,,,3.81,2.16,3.14*04
$GPGSV,3,1,10,01,19,244,,08,60,313,24,10,49,079,28,16,36,164,36*74
$GPGSV,3,2,10,18,42,250,22,20,07,038,,21,06,061,,22,09,195,30*7A
$GPGSV,3,3,10,26,09,158,30,27,69,072,27*70
$GPGLL,4252.31265,N,7833.7897,W,191013.00,A,A*7C

Output is pretty ugly as can be seen but you can see the coordinates in GPGLL entry. In order to have something more readable we install below packages.

user@computer:$ sudo dpkg -l | grep -i gps
ii gpsd 3.11-3 armhf Global Positioning System - daemon
ii gpsd-clients 3.11-3 armhf Global Positioning System - clients
ii libgps21:armhf 3.11-3 armhf Global Positioning System - library
ii python-gps 3.11-3 armhf Global Positioning System - Python libraries

Now copy below script and execute in order to start gpsd and listen on ttyS2.

#!/bin/bash 

sudo gpsd /dev/ttyS2 -F /var/run/gpsd.sock

Now I created below python script which prints creates Google Maps URL based on GPS coordinates.

import gps

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
        report = session.next()
        # Wait for a 'TPV' report and display current time
        if report['class'] == 'TPV':
            if hasattr(report, 'time') and hasattr(report, 'lat') and hasattr(report, 'lon'):
                print report.time
                print "Latitude: "+str(report.lat)+", Longitude: "+str(report.lon)
                print "Google Maps URL: https://maps.google.com/?q="+str(report.lat)+","+str(report.lon)+"\n"
    except KeyError:
        pass
    except KeyboardInterrupt:
        quit()
    except StopIteration:
        session = None
        print "GPSD has terminated"

And we proceed executing.

user@computer:$ python gps.py
2018-05-04T19:40:02.000Z
Latitude: 42.872081491, Longitude: -78.549973218
Google Maps URL: https://maps.google.com/?q=42.872081491,-78.549973218

2018-05-04T19:40:03.000Z
Latitude: 42.872081044, Longitude: -78.549972807
Google Maps URL: https://maps.google.com/?q=42.872081044,-78.549972807

2018-05-04T19:40:04.000Z
Latitude: 42.872081426, Longitude: -78.549970823
Google Maps URL: https://maps.google.com/?q=42.872081426,-78.549970823

2018-05-04T19:40:05.000Z
Latitude: 42.872081634, Longitude: -78.549968307
Google Maps URL: https://maps.google.com/?q=42.872081634,-78.549968307

2018-05-04T19:40:06.000Z
Latitude: 42.872082718, Longitude: -78.549965683
Google Maps URL: https://maps.google.com/?q=42.872082718,-78.549965683

And opening the URL on Google Maps.

Thanks for reading.

Leave a Reply

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

AlphaOmega Captcha Geographica  –  What City Do You See?