fsoguy
Posts: 9
Joined: Mon Oct 06, 2014 12:59 am
Location: Michigan, USA

NMEA GPS Info (USB/Serial) via Python 2.x

Wed Nov 19, 2014 9:55 pm

I have a few days off work and was bored so I decided to look into receiving data from my Pharos GPS-500. It only took a little while and now I seem to have the basics down. I've seen a few posts around that ask how to interface with a GPS, and there are several possible solutions. This one seemed the easiest to me. The first thing I did was plug in my GPS-500 and write a quick script to see if I was getting data. After that I decided to try and find a way to decipher the NMEA sentences. Google showed me the way. Here's how I am doing it using Python 2.x. P.S. Kudos to red_mamba who wrote these python modules: pyRestDb and pyNmea-RaspberryPi. I did not search for rmamba until after I posted this, where I came across his other GPS work. Fun stuff indeed.

This python script will show each character received, simply to prove that the GPS device is outputting data:

Code: Select all

#!/usr/bin/env python

import sys
import serial

def init():
	ser = serial.Serial()
	ser.baudrate = 4800
	ser.port = '/dev/ttyUSB0'

	ser.open()

	if not ser.isOpen():
		print("Unable to open serial port!")
		raise SystemExit
		
	while True:
		gps = ser.read(1)
		print gps

try:
	init()
except KeyboardInterrupt:		
	print "Done."
Here's the type of output you should see:
$
G
P
G
G
A
,
1
9
1
If you see random characters such as below then your baud rate is probably wrong.
à

f
x
ø
x
I believe these are the only prerequisites other than Python 2.x:
python-serial, python-flask, python-requests, pyRestDb, pyNmea-RaspberryPi

Code: Select all

sudo apt-get install python-serial
sudo apt-get install python-flask
sudo apt-get install python-requests
sudo mkdir pyrestdb
cd pyrestdb
sudo wget https://github.com/rmamba/pyRestDb/archive/master.zip
sudo unzip master.zip
cd ..
sudo mkdir pynmea
cd pynmea
sudo wget https://github.com/rmamba/pyNmea-RaspberryPi/archive/master.zip
sudo unzip master.zip
Now we have subfolders for each package and they are unzipped and ready to run.

I used 2 terminal windows for the next part.

Run the pyrestdb module in 1st terminal:

Code: Select all

sudo python /path-to-pyrestdb/pyRestDb.py
Now in the pyrestdb terminal, you should see:
* Running on http://0.0.0.0:666/
Run the pynmea gpsd module in 2nd terminal:

Code: Select all

sudo python /path-to-pynmea/gpsd.py start
Once you start the gpsy.py script, you will see the output from the pyrestdb terminal refreshing every second like this:
* Running on http://0.0.0.0:666/
127.0.0.1 - - [19/Nov/2014 15:00:32] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:32] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:34] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:35] "POST /post/GPS HTTP/1.1" 200 -
This means that the data is being sent and the summary is served up at http://raspberry-Pi-IP-Add:666

If you visit http://raspberry-Pi-IP-Add:666 you will see a summary page of the GPS output,with all kinds of info including a Google maps link to your current location, satellites tracked, lat, long, speed, etc.

To be able to query specific info, you can use this schema:
Which will output a page to your browser that only contains the # of satellites tracked.

To check other info, just use the output page or documentation to figure out what it's called. Here's a short list of examples:
I don't have a project to apply this to right now, but as you can see the data is right there ready to be queried at the specific links.

This python solution uses about 13% CPU on my stock B+ Pi.

gauravsharma0190
Posts: 125
Joined: Tue Oct 28, 2014 6:36 am

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Thu Nov 27, 2014 6:19 am

hello sir
i am using wiring pi library for serial comm.
but itdidnt work .
then i moved to python...
when i used "import serial".....it tells no module
wht's is the problem and how to fix it.

gordon77
Posts: 5449
Joined: Sun Aug 05, 2012 3:12 pm

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Thu Nov 27, 2014 6:35 am

You probably need to install pyserial

Sudo apt-get install python-serial

Carl_Panek
Posts: 4
Joined: Tue May 05, 2015 12:51 pm

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Tue May 05, 2015 2:19 pm

Has anyone gotten a NAVATION GPS 168 to work on an RPI under raspbian?

Carl_Panek
Posts: 4
Joined: Tue May 05, 2015 12:51 pm

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Tue May 05, 2015 4:06 pm

Nope - other USB GPS receivers work just fine on my system.

User avatar
red_mamba
Posts: 40
Joined: Wed Feb 22, 2012 8:49 am

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Tue Nov 10, 2015 10:48 am

fsoguy wrote:I have a few days off work and was bored so I decided to look into receiving data from my Pharos GPS-500. It only took a little while and now I seem to have the basics down. I've seen a few posts around that ask how to interface with a GPS, and there are several possible solutions. This one seemed the easiest to me. The first thing I did was plug in my GPS-500 and write a quick script to see if I was getting data. After that I decided to try and find a way to decipher the NMEA sentences. Google showed me the way. Here's how I am doing it using Python 2.x. P.S. Kudos to red_mamba who wrote these python modules: pyRestDb and pyNmea-RaspberryPi. I did not search for rmamba until after I posted this, where I came across his other GPS work. Fun stuff indeed.

This python script will show each character received, simply to prove that the GPS device is outputting data:

Code: Select all

#!/usr/bin/env python

import sys
import serial

def init():
	ser = serial.Serial()
	ser.baudrate = 4800
	ser.port = '/dev/ttyUSB0'

	ser.open()

	if not ser.isOpen():
		print("Unable to open serial port!")
		raise SystemExit
		
	while True:
		gps = ser.read(1)
		print gps

try:
	init()
except KeyboardInterrupt:		
	print "Done."
Here's the type of output you should see:
$
G
P
G
G
A
,
1
9
1
If you see random characters such as below then your baud rate is probably wrong.
à

f
x
ø
x
I believe these are the only prerequisites other than Python 2.x:
python-serial, python-flask, python-requests, pyRestDb, pyNmea-RaspberryPi

Code: Select all

sudo apt-get install python-serial
sudo apt-get install python-flask
sudo apt-get install python-requests
sudo mkdir pyrestdb
cd pyrestdb
sudo wget https://github.com/rmamba/pyRestDb/archive/master.zip
sudo unzip master.zip
cd ..
sudo mkdir pynmea
cd pynmea
sudo wget https://github.com/rmamba/pyNmea-RaspberryPi/archive/master.zip
sudo unzip master.zip
Now we have subfolders for each package and they are unzipped and ready to run.

I used 2 terminal windows for the next part.

Run the pyrestdb module in 1st terminal:

Code: Select all

sudo python /path-to-pyrestdb/pyRestDb.py
Now in the pyrestdb terminal, you should see:
* Running on http://0.0.0.0:666/
Run the pynmea gpsd module in 2nd terminal:

Code: Select all

sudo python /path-to-pynmea/gpsd.py start
Once you start the gpsy.py script, you will see the output from the pyrestdb terminal refreshing every second like this:
* Running on http://0.0.0.0:666/
127.0.0.1 - - [19/Nov/2014 15:00:32] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:32] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:34] "POST /post/GPS HTTP/1.1" 200 -
127.0.0.1 - - [19/Nov/2014 15:00:35] "POST /post/GPS HTTP/1.1" 200 -
This means that the data is being sent and the summary is served up at http://raspberry-Pi-IP-Add:666

If you visit http://raspberry-Pi-IP-Add:666 you will see a summary page of the GPS output,with all kinds of info including a Google maps link to your current location, satellites tracked, lat, long, speed, etc.

To be able to query specific info, you can use this schema:
Which will output a page to your browser that only contains the # of satellites tracked.

To check other info, just use the output page or documentation to figure out what it's called. Here's a short list of examples:
I don't have a project to apply this to right now, but as you can see the data is right there ready to be queried at the specific links.

This python solution uses about 13% CPU on my stock B+ Pi.
yay, I'm famous. Now if only the money would start flowing in :)

Good job! If anyone needs help with anything feel free to msg me.
I'm happy to help ppl out with advice and guidance ;)

scotty101
Posts: 4200
Joined: Fri Jun 08, 2012 6:03 pm

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Tue Nov 10, 2015 10:58 am

Of course gpsd is a much more robust solution that supports many different GPS receivers and there are many GUIs that can make use of its data.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

venkyskar
Posts: 18
Joined: Mon Mar 28, 2016 10:56 am

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Wed Mar 22, 2017 8:31 am

What if the GPS has TTL connector and if I connect it to the GPIO will the above code be successful in reading the coordinates.

User avatar
red_mamba
Posts: 40
Joined: Wed Feb 22, 2012 8:49 am

Re: NMEA GPS Info (USB/Serial) via Python 2.x

Wed Mar 22, 2017 5:18 pm

serial TTL?
you can use cheap USB to TTL converter in that case and code should still work.

Return to “Beginners”