Raspberry Pi Pico W with GPS processed data

Example code of connecting the Raspberry Pi Pico W to a GPS module. Checking the checksum and extract some gps information.

Connections

Description Colour Pico W GPS module
Power 3v Orange pin36 3V3(OUT) 3.3v
Ground Black pin3 GND GND
Pico W Transmit Yellow pin6 GP4 UART1_TX GPS RX pin
Pico W Receive Green pin7 GP5 UART1_RX GPS TX pin
   iconSerial.png    volts33.png

Photo

Raspberry Pi Pico W with GPS processed data
Raspberry Pi Pico W with GPS processed data
Raspberry Pi Pico W with GPS processed data
Raspberry Pi Pico W with GPS processed data

Sketch

# Raspberry Pi Pico W - GPS module - processed
# Copyright (C) 2023 https://www.roboticboat.uk
# e3897b8c-d8b6-46c9-b16b-cfcd25f0ee4a
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# These Terms shall be governed and construed in accordance with the laws of
# England and Wales, without regard to its conflict of law provisions.
#
# User Interface https://thonny.org/
#
# The objective of the code snippest is to show GPS statements being processed
# without relying on a library, where for beginners, code gets buried
from machine import Pin,UART
import time
import sys
# Initialise the Serial port
uart = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
# Initialise the Serial port message protocol
uart.init(bits=8, parity=None, stop=1)
# Define the LED pin
led = Pin("LED", Pin.OUT)
# Initialise variables
gpsmessage = ""
gpstime = ""
gpslatitude = ""
gpsNS = ""
gpslongitude = ""
gpsEW = ""
gpsSatellites = ""
try:
# Infinite loop, like on the Arudino
while True:
# If data waiting to be read
if uart.any():
# Read data on the Serial port. We don't need to wait for a full line
data = uart.read()
# Have to wrap the decoder in error capturing as the starting
# GPS characters might not be ASCII until Serial aligns
# Ideally data is checked first and error capturing not used
try:
# Decode the bytes into a str class
asciiCharacters = data.decode("utf-8")
except:
continue
# Loop over the characters of the message
for nextChar in asciiCharacters:
# Do we see a '$' character?
if nextChar == "$":
# Start of the next message, so also last of the previous
# message. So we can process to received GPS statement
# Update the User
print(gpsmessage)
# Look out for GGA messages
# Can process the message - 3rd, 4th & 5th characters
if (gpsmessage[2:5] == "GGA"):
# Split the message using commas into a list of items
items = gpsmessage.split(",")
# Number of items. This should be 15 items
nitems = len(items)
# Checksum is the last item. It will also contain * '\r' and '\n'
# Is the checksum item the correct length
if len(items[nitems-1]) == 5:
# Read the checksum from the message
# Ignore the * \r and \n
checksum = items[nitems-1][1:3]
# Convert the double HEX checksum to an integer
cksum = int(checksum,16)
# Loop over the message characters
# Stop before the last * checksum and '\r' '\n'
for gpschar in gpsmessage[:len(gpsmessage)-5]:
# The ord() function returns an integer
# representing the Unicode character
cksum ^= ord(gpschar)
# Is the checksum correct? Needs to equal zero
if (cksum == 0):
# Extract gps information
gpstime = items[1]
gpslatitude = items[2]
gpsNS = items[3]
gpslongitude = items[4]
gpsEW = items[5]
gpsSatellites = items[7]
# Update the User
print(gpstime)
print(gpslatitude)
print(gpsNS)
print(gpslongitude)
print(gpsEW)
print(gpsSatellites)
print("")
# Toggle the LED light on/off
led.toggle()
# Initialise the next new message
gpsmessage = ""
else:
# Append the next character
gpsmessage += nextChar
# Interrupt handing
# Capture the CTRL + C event
except (KeyboardInterrupt, SystemExit):
# Update the user
print ("Well done. Finished\n")
view raw picoW_GPS3.py hosted with ❤ by GitHub
RoboticBoat.uk