|
| 1 | +# Simple GPS module demonstration. |
| 2 | +# Will print NMEA sentences received from the GPS, great for testing connection |
| 3 | +# Uses the GPS only to send some commands, then reads directly from UART |
| 4 | +import time |
| 5 | +import board |
| 6 | +import busio |
| 7 | + |
| 8 | +import adafruit_gps |
| 9 | + |
| 10 | + |
| 11 | +# Define RX and TX pins for the board's serial port connected to the GPS. |
| 12 | +# These are the defaults you should use for the GPS FeatherWing. |
| 13 | +# For other boards set RX = GPS module TX, and TX = GPS module RX pins. |
| 14 | +#RX = board.RX |
| 15 | +#TX = board.TX |
| 16 | + |
| 17 | +# Create a serial connection for the GPS connection using default speed and |
| 18 | +# a slightly higher timeout (GPS modules typically update once a second). |
| 19 | +uart = busio.UART(TX, RX, baudrate=9600, timeout=3000) |
| 20 | + |
| 21 | +# for a computer, use the pyserial library for uart access |
| 22 | +#import serial |
| 23 | +#uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3000) |
| 24 | + |
| 25 | +# Create a GPS module instance. |
| 26 | +gps = adafruit_gps.GPS(uart) |
| 27 | + |
| 28 | +# Initialize the GPS module by changing what data it sends and at what rate. |
| 29 | +# These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and |
| 30 | +# PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust |
| 31 | +# the GPS module behavior: |
| 32 | +# https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf |
| 33 | + |
| 34 | +# Turn on the basic GGA and RMC info (what you typically want) |
| 35 | +gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 36 | +# Turn on just minimum info (RMC only, location): |
| 37 | +#gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 38 | +# Turn off everything: |
| 39 | +#gps.send_command(b'PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 40 | +# Tuen on everything (not all of it is parsed!) |
| 41 | +#gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 42 | + |
| 43 | +# Set update rate to once a second (1hz) which is what you typically want. |
| 44 | +gps.send_command(b'PMTK220,1000') |
| 45 | +# Or decrease to once every two seconds by doubling the millisecond value. |
| 46 | +# Be sure to also increase your UART timeout above! |
| 47 | +#gps.send_command(b'PMTK220,2000') |
| 48 | +# You can also speed up the rate, but don't go too fast or else you can lose |
| 49 | +# data during parsing. This would be twice a second (2hz, 500ms delay): |
| 50 | +#gps.send_command(b'PMTK220,500') |
| 51 | + |
| 52 | +# Main loop runs forever printing data as it comes in |
| 53 | +timestamp = time.monotonic() |
| 54 | +while True: |
| 55 | + data = uart.read(32) # read up to 32 bytes |
| 56 | + # print(data) # this is a bytearray type |
| 57 | + |
| 58 | + if data is not None: |
| 59 | + # convert bytearray to string |
| 60 | + data_string = ''.join([chr(b) for b in data]) |
| 61 | + print(data_string, end="") |
| 62 | + |
| 63 | + if time.monotonic() - timestamp > 5: |
| 64 | + # every 5 seconds... |
| 65 | + gps.send_command(b'PMTK605') # request firmware version |
| 66 | + timestamp = time.monotonic() |
0 commit comments