Full form of GPS is global positioning system. GPS is one of the very superb inventions that help us very much. GPS finds the position by making satellites as reference. It is accurate up to few meters. Now a days almost everyone in the world are using GPS for navigation.
Here I am sharing my experience with a GPS module. GPS modules are available in almost all electronic shops. The picture of a GPS module is shown below. The working of almost all the modules are the same. All these modules emit the GPS coordinates in a standard format (NMEA). Recently I have checked the signals from a GPS device present in a marine vessel and it is exactly the same as my GPS device.
What is NMEA ?
NMEA is a standard data format supported by almost all GPS manufacturers. NMEA stands for National Marine Electronics Association. The purpose of NMEA is to ensure a standard data format so that the users can mix and match hardware & software. For example, the program that we develop here will work perfectly for any other GPS device that supports NMEA.
Implementation
The GPS unit that I bought has an RS232 as well as a TTL interface. We can connect this GPS device to any of the Computer or Raspberry Pi or any Micro-controller very easily. I have connected and did the experiments using GPS on Raspberry Pi, Laptop and Micro-controller.
Here I am explaining a sample python program that will find the coordinates using this GPS module. I used a python library called pynmea for reading and parsing the data returned by the GPS module.
First, power up the GPS module. The input power supply depends on the type of GPS module that you are using. The module I used supports 12V and 5V input voltage. The next step is to connect the GPS module to your Computer/Raspberry Pi/Micro-controller using the RS232 (serial port). If your laptop or PC is not having the RS232 port, don’t worry, you can use an RS232 to USB converter cable. Check the serial port in which this device is connected (This can be found from the device manager). Before we start writing the program, ensure that the pynmea library is installed in your computer.
First we will check whether the device is working properly. So we will write a small python program to read the data from the serial port.
__author__ = 'Amal G Jose' import serial ser = serial.Serial() ser.baudrate = 9600 ser.port = 'COM5' ser.open() while True: print ser.readline()
In my case the device was connected to COM5 port. Change the port accordingly and execute this program. If you are getting values in the console, then you are good to go.
Then execute the following program to parse the data and find the coordinates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Amal G Jose' | |
import time | |
import serial | |
import string | |
from pynmea import nmea | |
ser = serial.Serial() | |
# Reading serial data from COM5 port. Change this port according to your settings. | |
ser.port = "COM5" | |
ser.baudrate = 9600 | |
ser.timeout = 1 | |
ser.open() | |
gpgga = nmea.GPGGA() | |
while True: | |
data = ser.readline() | |
if data[0:6] == '$GPGGA': | |
##method for parsing the sentence | |
gpgga.parse(data) | |
lats = gpgga.latitude | |
print("Latitude values : " + str(lats)) | |
lat_dir = gpgga.lat_direction | |
print("Latitude direction : " + str(lat_dir)) | |
longitude = gpgga.longitude | |
print("Longitude values : " + str(longitude)) | |
long_dir = gpgga.lon_direction | |
print("Longitude direction : " + str(long_dir)) | |
time_stamp = gpgga.timestamp | |
print("GPS time stamp : " + str(time_stamp)) | |
alt = gpgga.antenna_altitude | |
print("Antenna altitude : " + str(alt)) | |
lat_deg = lats[0:2] | |
lat_mins = lats[2:4] | |
lat_secs = round(float(lats[5:])*60/10000, 2) | |
latitude_string = lat_deg + u'\N{DEGREE SIGN}' + lat_mins + string.printable[68] + str(lat_secs) + string.printable[63] | |
print("Latitude : " + str(latitude_string)) | |
lon_deg = longitude[0:3] | |
lon_mins = longitude[3:5] | |
lon_secs = round(float(longitude[6:])*60/10000, 2) | |
lon_str = lon_deg + u'\N{DEGREE SIGN}' + lon_mins + string.printable[68] + str(lon_secs) + string.printable[63] | |
print("Longitude : " + str(lon_str)) |
I was trying your code for reading data from a GPS receiver.
I have a GPS receiver that plugs into a USB port.
It’s a GlobalSat USB Dongle.
This USB dongle is a really small, compact unit.
I modified your code with the following lines to get access to the port:
ser = serial.Serial(
port=’/dev/ttyUSB0′,
baudrate = 4800,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
The data seems to be parsing, but I’m getting an error at the end.
Traceback (most recent call last):
File “read_and_parse.py”, line 52, in
print “Latitude : ” + str(latitude_string)
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xb0′ in position 2: ordinal not in range(128)
I think the lines of code are:
latitude_string = lat_deg + u’\N{DEGREE SIGN}’ + lat_mins + string.printable[68] + str(lat_secs) + string.printable[63]
print “Latitude : ” + str(latitude_string)
I fixed some errors.
You were taking a string of a string.
Here’s a new listing.
__author__ = ‘Amal G Jose’
#modified 20180110
import time
import serial
import string
from pynmea import nmea
ser = serial.Serial(
port=’/dev/ttyUSB0′,
baudrate = 4800,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
gpgga = nmea.GPGGA()
while True:
data = ser.readline()
if data[0:6] == ‘$GPGGA’:
gpgga.parse(data)
lat = gpgga.latitude
print “Latitude values : ” + str(lat)
lat_dir = gpgga.lat_direction
print “Latitude direction : ” + str(lat_dir)
lon = gpgga.longitude
print “Longitude values : ” + str(lon)
lon_dir = gpgga.lon_direction
print “Longitude direction : ” + str(lon_dir)
time_stamp = gpgga.timestamp
print “GPS time stamp : ” + str(time_stamp)
alt = gpgga.antenna_altitude
print “Antenna altitude : ” + str(alt)
# extract latitude
lat_deg = lat[0:2]
lat_min = lat[2:4]
lat_sec_chars = lat[5:]
lat_sec = round(float(lat[5:])*60/10000, 2)
print “Latitude degrees = ” + lat_deg
print “Latitude minutes = ” + lat_min
print “Latitude seconds characters = ” + lat_sec_chars
print “Latitude seconds = ” + str(lat_sec)
lat_string = (lat_deg +
u’\N{DEGREE SIGN}’ +
lat_min +
string.printable[68] +
str(lat_sec) +
string.printable[63])
print “Latitude : ” + lat_string
# extract longitude
lon_deg = lon[0:3]
lon_min = lon[3:5]
lon_sec_chars = lon[6:]
lon_sec = round(float(lon[6:])*60/10000, 2)
print “Longitude degrees = ” + lon_deg
print “Longitude minutes = ” + lon_min
print “Longitude seconds characters = ” + lon_sec_chars
print “Longitude seconds = ” + str(lon_sec)
lon_string = (lon_deg +
u’\N{DEGREE SIGN}’ +
lon_min +
string.printable[68] +
str(lon_sec) +
string.printable[63])
print “Longitude : ” + lon_string
hi, please could you tell me if i want to connect gps module with raspberry pi using UART connection. what changes needs to be done for this code.