Advertisements

Python program to find the distance between two GPS locations

GPS is a very important technology that we use in our daily life for navigation, finding distance between two places, estimating the time for journey etc. Now Internet of things is getting hotter in the market and lot of people including college students are doing POCs with ideas related to IoT. If we are developing an application that includes GPS, finding the distance between two places is a common requirement. The program given below finds the straight distance between two GPS coordinates. Here the distance is calculated by assuming earth as a circle with radius 6371 kilometers. If you want the distance in miles, use 3956 miles as the radius of earth.

Advertisements

A Small experiment using GPS module

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 upto some 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 electronic shops. The picture of a gps module is shown below.

gps1The gps unit that I bought is having an RS232 as well as a TTL interface. We can connect this device to a computer, raspberry pi or any micro-controller very easily. I connected and did the experiments using this device 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. This depends on the type of gps module that you are using. The module I used supports 12V and 5V input voltage. Then connect the gps module to your computer using the RS232. 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.