Skip to content

Latest commit

 

History

History
165 lines (112 loc) · 4.43 KB

File metadata and controls

165 lines (112 loc) · 4.43 KB

Checking for GPS Signal

After rebooting in the previous step, you will need to SSH back into the Raspberry Pi. If you do not remember how to do this, see section 1 for details.

Before continuing, make sure that the blue LED on the NEO-6M is blinking. The blinking blue LED means that it is receiving GPS data. It takes a few minutes for the blinking to start after powering up the module. If after 5-8 mins it has not started blinking, move near a window or any place under the open sky.

When the blue LED is blinking, run the following command:

sudo cat /dev/ttyAMA0

You should see lots of data, similar to what is shown in the image below.

This means it's working. To stop this, press Ctrl + C .

These numbers contain a whole host of data, and we need to filter it to pull out the latitude and longitude. For that, we'll write a short Python program and use a special command, NMEAStreamReader().

Setup for Writing the Python Code:

Before we write the Python code to get the GPS data, we need to set up a few things.

By default, the Raspberry Pi uses a serial port for this “console” login, so if we want to use the serial port to get data from the GPS module, we will need to disable the console login.

There are two serial ports: serial0 and serial1. Serial0 will point to GPIO pins 14 and 15, so we have to use serial0. To see which port is connected with serial0, use the following command:

ls -l /dev

There are two possible outputs. You'll see that serial0 is either linked with ttyAMA0, in which case to disable the console you need to use the following commands:

sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service

or that serial0 is linked with ttyS0. Therefore, to disable the console you will need to use the following commands:

sudo systemctl stop serial-getty@ttyS0.service
sudo systemctl disable serial-getty@ttyS0.service

Let’s Write the Python Code:

Set Up

In order to write the programs, it's good practice to create a directory on the desktop of the Raspberry Pi.

So, let's get onto the desktop first.

cd Desktop

Then make a folder (or directory) for the GPS tracker

mkdir GPS_Tracker

Let's go into that directory.

cd GPS_Tracker

You'll notice that the text before where you type in the terminal identifies that you're working in the directory.

If you want to go backwards out of a directory, you can use the command

cd ..

Using the command

ls

will list what directories and files are contained in the directory you're in.

To prepare for the coding, we need to create a virtual environment on the Raspberry Pi, allowing us to ensure we can install the relevant libraries.

python -m venv gps-venv

Next, we will need to activate the virtual environment (venv). Use the following command:

source gps-venv/bin/activate

You will need to re-run this after every reboot.

We need to install a few Python libraries so that we have access to the right commands. It may take a few moments between each command for the libraries to install, so be patient:

pip install pynmea2 pip install pyserial

This demo was produced using version 4.8 of Pyrebase and 3.11.2 of Python. Therefore, a small workaround is needed as they are not completely compatible and may have issues. This can easily be achieved by enforcing a particular version of the cryptography library.

pip3 install --force-reinstall -v cryptography==42.0.8

Finally, install one more library:

pip install pyrebase4

Further adaptations may be needed as new versions are published.

Finally, we are ready to write the code!

You'll create the code in a text editor on the Raspberry Pi (in our case, nano).

nano gps_print.py

Once in the editor you can copy the gps_print.py code. Then save and exit.

import serial
import time
import string
import pynmea2

while True:
	port="/dev/ttyAMA0"
	ser=serial.Serial(port, baudrate=9600, timeout=0.5)
	dataout = pynmea2.NMEAStreamReader()
	newdata=ser.readline()
	n_data = newdata.decode('latin-1')
	if n_data[0:6] == "$GPRMC":
		newmsg=pynmea2.parse(n_data)
		lat=newmsg.latitude
		lng=newmsg.longitude
		gps = "Latitude=" + str(lat) + " and Longitude=" + str(lng)
		print(gps)

To run this Python code you'll use the command

python3 gps_print.py

and you will see an output like this: