gps_datalogging.py
1 # Simple GPS datalogging demonstration. 2 # This example uses the GPS library and to read raw NMEA sentences 3 # over I2C or UART from the GPS unit and dumps them to a file on an SD card 4 # (recommended), microcontroller internal storage (be careful as only a few 5 # kilobytes are available), or to a filesystem. 6 # If you are using a microcontroller, before writing to internal storage you 7 # MUST carefully follow the steps in this guide to enable writes to the 8 # internal filesystem: 9 # https://learn.adafruit.com/adafruit-ultimate-gps-featherwing/circuitpython-library 10 import board 11 import busio 12 import adafruit_gps 13 14 # Path to the file to log GPS data. By default this will be appended to 15 # which means new lines are added at the end and all old data is kept. 16 # Change this path to point at internal storage (like '/gps.txt') or SD 17 # card mounted storage ('/sd/gps.txt') as desired. 18 LOG_FILE = "gps.txt" # Example for writing to internal path 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 # If writing to SD card on a microcontroller customize and uncomment these 26 # lines to import the necessary library and initialize the SD card: 27 # NOT for use with a single board computer like Raspberry Pi! 28 """ 29 import adafruit_sdcard 30 import digitalio 31 import storage 32 33 SD_CS_PIN = board.D10 # CS for SD card using Adalogger Featherwing 34 spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 35 sd_cs = digitalio.DigitalInOut(SD_CS_PIN) 36 sdcard = adafruit_sdcard.SDCard(spi, sd_cs) 37 vfs = storage.VfsFat(sdcard) 38 storage.mount(vfs, '/sd') # Mount SD card under '/sd' path in filesystem. 39 LOG_FILE = '/sd/gps.txt' # Example for writing to SD card path /sd/gps.txt 40 """ 41 42 # Create a serial connection for the GPS connection using default speed and 43 # a slightly higher timeout (GPS modules typically update once a second). 44 # These are the defaults you should use for the GPS FeatherWing. 45 # For other boards set RX = GPS module TX, and TX = GPS module RX pins. 46 uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10) 47 48 # If using a USB/Serial converter, use pyserial and update the serial 49 # port name to match the serial connection for the GPS! 50 # import serial 51 # uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10) 52 53 # If using I2C, we'll create an I2C interface to talk to using default pins 54 # i2c = board.I2C() 55 56 # Create a GPS module instance. 57 gps = adafruit_gps.GPS(uart) # Use UART/pyserial 58 # gps = adafruit_gps.GPS_GtopI2C(i2c) # Use I2C interface 59 60 # Main loop just reads data from the GPS module and writes it back out to 61 # the output file while also printing to serial output. 62 with open(LOG_FILE, LOG_MODE) as outfile: 63 while True: 64 sentence = gps.readline() 65 if not sentence: 66 continue 67 print(str(sentence, "ascii").strip()) 68 outfile.write(sentence) 69 outfile.flush()