Reading our GPS data

We need our GPS sensor to get a fix on the GPS satellites before we can read anything useful from it. You will know if this has happened as the small red FIX LED will start to flash once every 15 seconds instead of once every second. If you are running this indoors or are having difficulty getting a satellite lock, you can use the Adfruit SMA to μFL adaptor cable and Adafruit GPS Antenna, and put the antenna near or outside a window to get a satellite lock.

Now that we have a GPS satellite lock we can, at this point, just read the data coming from our GPS sensor by running this:

sudo cat /dev/ttyS0

If you all is well, you should something like this scrolling up your SSH terminal window:

Press Ctrl + C to exit the stream of GPS data. So our sensor is working and receiving data, but we need to be able to make use of this data in our programs. To do this, we now need to install some pieces of software. We will need to install some system packages and also another Python library. The system packages we will need are the gpsd package and the gps-clients package. Install them by typing this:

sudo apt-get install gpsd gpsd-clients python-gps -y

You may have noticed that the Python package is a Python 2 and not a Python 3 package. To date, there does not appear to be a functioning Python 3 version of this package, so we will make use of Python 2 for this project. Now that we have our required software, type the following command to create and move into our chapter working directory:

mkdir ~/WearableTech/Chapter9 && cd ~/WearableTech/Chapter9

Before we can read the GPS data, we need to adjust the gpsd package slightly. The gpsd package we installed a couple of steps ago installs a systemd service on our Raspberry Pi, which defaults to a local socket; however, we want to manually run our GPS software, and tell it which socket to listen to manually. Stop and disable the gpsd systemd service by typing this:

sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket

We can now manually run gpsd and point it to our GPS sensor by typing this:

sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock

After running this command, we can use the CGPS program to view the live data from our sensor. Type cgps -s, and you should see something similar to this:

Press Ctrl + C to stop CGPS from running; now, we are going to try writing a small Python program to display data from the GPS sensor. Start a new Python file by typing nano gpsTest.py and type the following code into it:

import gps

# taken from https://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi?view=all#using-your-gps

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
try:
report = session.next()
# Wait for a 'TPV' report and display the current time
# To see all report data, uncomment the line below
#print report
if report['class'] == 'TPV':
if hasattr(report, 'time'):
print report.time
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"

You can then save the program by pressing Ctrl + O, followed by Enter, and exit Nano by pressing Ctrl + X. Now, run your program by typing python ./gpsTest.py. You should see something like this scroll up your Terminal window:

Stop the program from running by pressing Ctrl + C. We now know that we can read our GPS data into Python, so we will move onto the next section where you will learn how to create files ready to be uploaded into Google Maps or Google Earth.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.184.214