|
| 1 | +# Simple GPS datalogging demonstration. |
| 2 | +# This actually doesn't even use the GPS library and instead just reads raw |
| 3 | +# NMEA sentences from the GPS unit and dumps them to a file on an SD card |
| 4 | +# (recommended) or internal storage (be careful as only a few kilobytes to |
| 5 | +# megabytes are available). Before writing to internal storage you MUST |
| 6 | +# carefully follow the steps in this guide to enable writes to the internal |
| 7 | +# filesystem: |
| 8 | +# https://learn.adafruit.com/adafruit-ultimate-gps-featherwing/circuitpython-library |
| 9 | +import board |
| 10 | +import busio |
| 11 | + |
| 12 | + |
| 13 | +# Path to the file to log GPS data. By default this will be appended to |
| 14 | +# which means new lines are added at the end and all old data is kept. |
| 15 | +# Change this path to point at internal storage (like '/gps.txt') or SD |
| 16 | +# card mounted storage ('/sd/gps.txt') as desired. |
| 17 | +LOG_FILE = '/gps.txt' # Example for writing to internal path /gps.txt |
| 18 | +#LOG_FILE = '/sd/gps.txt' # Example for writing to SD card path /sd/gps.txt |
| 19 | + |
| 20 | +# File more for opening the log file. Mode 'ab' means append or add new lines |
| 21 | +# to the end of the file rather than erasing it and starting over. If you'd |
| 22 | +# like to erase the file and start clean each time use the value 'wb' instead. |
| 23 | +LOG_MODE = 'ab' |
| 24 | + |
| 25 | +# Define RX and TX pins for the board's serial port connected to the GPS. |
| 26 | +# These are the defaults you should use for the GPS FeatherWing. |
| 27 | +# For other boards set RX = GPS module TX, and TX = GPS module RX pins. |
| 28 | +RX = board.RX |
| 29 | +TX = board.TX |
| 30 | + |
| 31 | +# If writing to SD card customize and uncomment these lines to import the |
| 32 | +# necessary library and initialize the SD card: |
| 33 | +#SD_CS_PIN = board.SD_CS # CS for SD card (SD_CS is for Feather Adalogger) |
| 34 | +#import adafruit_sdcard |
| 35 | +#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 36 | +#sd_cs = digitalio.DigitalInOut(SD_CS_PIN) |
| 37 | +#sdcard = adafruit_sdcard.SDCard(spi, sd_cs) |
| 38 | +#vfs = storage.VfsFat(sdcard) |
| 39 | +#storage.mount(vfs, '/sd') # Mount SD card under '/sd' path in filesystem. |
| 40 | + |
| 41 | +# Create a serial connection for the GPS connection using default speed and |
| 42 | +# a slightly higher timeout (GPS modules typically update once a second). |
| 43 | +uart = busio.UART(TX, RX, baudrate=9600, timeout=3000) |
| 44 | + |
| 45 | +# Main loop just reads data from the GPS module and writes it back out to |
| 46 | +# the output file while also printing to serial output. |
| 47 | +with open(LOG_FILE, LOG_MODE) as outfile: |
| 48 | + while True: |
| 49 | + sentence = uart.readline() |
| 50 | + print(str(sentence, 'ascii').strip()) |
| 51 | + outfile.write(sentence) |
| 52 | + outfile.flush() |
0 commit comments