A global positioning system (GPS) module is a device used to determine its location on earth in terms of latitude and longitude. Since Raspberry Pi is a complete computer in itself with a stable operating system therefore connecting a GPS device to it is just like connecting it to any other computer. But getting it to work, then pulling out your chosen GPS-related data in python and performing some predefined tasks with the GPS data is something very different and interesting.
Circuit and working
There are a variety of GPS modules available in the market but the one with a built-in patch antenna on top (POT) gives the advantage of data reception even indoors. For GPS module to work outdoors, you may have to find a clear sky to receive data. The module can either be connected with the USB cable or with just four wires connected directly to the GPIO of Raspberry Pi as shown in the table.
For simplicity, here we are using the USB cable (refer Fig. 1). After inserting the USB cable look in the /dev/ttyUSB* directory to see which device gets recognised. Most of the times, it becomes /dev/ttyUSB0 but in case we connect it with the GPIO pins, it becomes /dev/ttyAMA0.
Update and install the GPS libraries using below-given commands, also shown in Fig. 2.
Now load the gps device with the command below, also shown in Fig. 3.
Write below command and all the GPS data will start pouring in as shown in Fig. 4. If data does not appear, take the GPS module near the window and try again.
Or you can also use the command below for the same.
The ‘gpsd’ also has a beautiful graphics data output with all the satellite connections as shown in Fig. 5.
Write below-mentioned command for the graphic data output:
Screenshot of GPS data is shown in Fig. 6.
Square icon in Fig. 5 indicates the WAAS/EGNOS satellite, circles indicate ordinary GPS satellites. Filled icons were used in the last fix of locations.
GPS data takes some time to get synchronised. Therefore wait for some time and if nothing appears, try the steps below:
The ‘gpsd’ even has a built-in self telnet daemon at port 2947 with JSON. To use it, first install it in Raspberry Pi using:
Now telnet to local host at port 2947 using below-mentioned command:
Now to see data from the receiver, enter the command:
The data appears like this:
Now here is a Python script which exploits every GPS data in your own way. The script has some issue related to the absolute path. Therefore open it in /usr/sbin directory of your Raspberry Pi where the ‘gpsd’ program file is located.
Create the file:
Paste the code below in that file:
Please take care that the indentations of the pasted code are the same as mentioned above as Python is sensitive to it.
Download Source Code: Click Here
Now run the code by using the command below and the GPS data appears as shown in Fig. 7.
To set our Raspberry Pi time synchronised with GPS time NMEA satellite protocol, we can install ‘ntp’ program as:
Now run the command below and the time of the Raspberry Pi will now be synchronised with Pacific Time standard.
The author is an avid user of open source software. Professionally, he is a thermal power expert and works as an additional general manager at NTPC Limited