gps_simpletest.py
1 # Simple GPS module demonstration. 2 # Will wait for a fix and print a message every second with the current location 3 # and other details. 4 import time 5 import board 6 import busio 7 8 import adafruit_gps 9 10 # Create a serial connection for the GPS connection using default speed and 11 # a slightly higher timeout (GPS modules typically update once a second). 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 uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10) 15 16 # for a computer, use the pyserial library for uart access 17 # import serial 18 # uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10) 19 20 # If using I2C, we'll create an I2C interface to talk to using default pins 21 # i2c = board.I2C() 22 23 # Create a GPS module instance. 24 gps = adafruit_gps.GPS(uart, debug=False) # Use UART/pyserial 25 # gps = adafruit_gps.GPS_GtopI2C(i2c, debug=False) # Use I2C interface 26 27 # Initialize the GPS module by changing what data it sends and at what rate. 28 # These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and 29 # PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust 30 # the GPS module behavior: 31 # https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf 32 33 # Turn on the basic GGA and RMC info (what you typically want) 34 gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") 35 # Turn on just minimum info (RMC only, location): 36 # gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') 37 # Turn off everything: 38 # gps.send_command(b'PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') 39 # Tuen on everything (not all of it is parsed!) 40 # gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0') 41 42 # Set update rate to once a second (1hz) which is what you typically want. 43 gps.send_command(b"PMTK220,1000") 44 # Or decrease to once every two seconds by doubling the millisecond value. 45 # Be sure to also increase your UART timeout above! 46 # gps.send_command(b'PMTK220,2000') 47 # You can also speed up the rate, but don't go too fast or else you can lose 48 # data during parsing. This would be twice a second (2hz, 500ms delay): 49 # gps.send_command(b'PMTK220,500') 50 51 # Main loop runs forever printing the location, etc. every second. 52 last_print = time.monotonic() 53 while True: 54 # Make sure to call gps.update() every loop iteration and at least twice 55 # as fast as data comes from the GPS unit (usually every second). 56 # This returns a bool that's true if it parsed new data (you can ignore it 57 # though if you don't care and instead look at the has_fix property). 58 gps.update() 59 # Every second print out current location details if there's a fix. 60 current = time.monotonic() 61 if current - last_print >= 1.0: 62 last_print = current 63 if not gps.has_fix: 64 # Try again if we don't have a fix yet. 65 print("Waiting for fix...") 66 continue 67 # We have a fix! (gps.has_fix is true) 68 # Print out details about the fix like location, date, etc. 69 print("=" * 40) # Print a separator line. 70 print( 71 "Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}".format( 72 gps.timestamp_utc.tm_mon, # Grab parts of the time from the 73 gps.timestamp_utc.tm_mday, # struct_time object that holds 74 gps.timestamp_utc.tm_year, # the fix time. Note you might 75 gps.timestamp_utc.tm_hour, # not get all data like year, day, 76 gps.timestamp_utc.tm_min, # month! 77 gps.timestamp_utc.tm_sec, 78 ) 79 ) 80 print("Latitude: {0:.6f} degrees".format(gps.latitude)) 81 print("Longitude: {0:.6f} degrees".format(gps.longitude)) 82 print("Fix quality: {}".format(gps.fix_quality)) 83 # Some attributes beyond latitude, longitude and timestamp are optional 84 # and might not be present. Check if they're None before trying to use! 85 if gps.satellites is not None: 86 print("# satellites: {}".format(gps.satellites)) 87 if gps.altitude_m is not None: 88 print("Altitude: {} meters".format(gps.altitude_m)) 89 if gps.speed_knots is not None: 90 print("Speed: {} knots".format(gps.speed_knots)) 91 if gps.track_angle_deg is not None: 92 print("Track angle: {} degrees".format(gps.track_angle_deg)) 93 if gps.horizontal_dilution is not None: 94 print("Horizontal dilution: {}".format(gps.horizontal_dilution)) 95 if gps.height_geoid is not None: 96 print("Height geo ID: {} meters".format(gps.height_geoid))