/ examples / featherwing_gps_simpletest.py
featherwing_gps_simpletest.py
 1  """
 2  This example will connect to the GPS at the default 9600 baudrate and
 3  update once per second. Initialization is automatically handled and there
 4  are some additional features such as MPH and KPH calculations.
 5  """
 6  import time
 7  from adafruit_featherwing import gps_featherwing
 8  
 9  # Create a GPS featherwing instance.
10  gps = gps_featherwing.GPSFeatherWing()
11  
12  # Main loop runs forever printing the location, etc. every second.
13  last_print = time.monotonic()
14  while True:
15      # Make sure to call gps.update() every loop iteration and at least twice
16      # as fast as data comes from the GPS unit (usually every second).
17      # This returns a bool that's true if it parsed new data (you can ignore it
18      # though if you don't care and instead look at the has_fix property).
19      gps.update()
20      # Every second print out current location details if there's a fix.
21      current = time.monotonic()
22      if current - last_print >= 1.0:
23          last_print = current
24          if not gps.has_fix:
25              # Try again if we don't have a fix yet.
26              print("Waiting for fix...")
27              continue
28          # Print out details about the fix like location, date, etc.
29          print("=" * 40)  # Print a separator line.
30          print(
31              "Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}".format(
32                  gps.timestamp.tm_mon,  # Grab parts of the time from the
33                  gps.timestamp.tm_mday,  # struct_time object that holds
34                  gps.timestamp.tm_year,  # the fix time.  Note you might
35                  gps.timestamp.tm_hour,  # not get all data like year, day,
36                  gps.timestamp.tm_min,  # month!
37                  gps.timestamp.tm_sec,
38              )
39          )
40          print("Latitude: {0:.6f} degrees".format(gps.latitude))
41          print("Longitude: {0:.6f} degrees".format(gps.longitude))
42          print("Fix quality: {}".format(gps.fix_quality))
43          # Some attributes beyond latitude, longitude and timestamp are optional
44          # and might not be present.  Check if they're None before trying to use!
45          if gps.satellites is not None:
46              print("# satellites: {}".format(gps.satellites))
47          if gps.altitude is not None:
48              print("Altitude: {} meters".format(gps.altitude))
49          if gps.speed_knots is not None:
50              print("Speed (Knots): {} knots".format(gps.speed_knots))
51          if gps.speed_mph is not None:
52              print("Speed (Miles Per Hour): {} MPH".format(gps.speed_mph))
53          if gps.speed_kph is not None:
54              print("Speed (KM Per Hour): {} KPH".format(gps.speed_kph))
55          if gps.track_angle is not None:
56              print("Track angle: {} degrees".format(gps.track_angle))
57          if gps.horizontal_dilution is not None:
58              print("Horizontal dilution: {}".format(gps.horizontal_dilution))
59          if gps.height_geoid is not None:
60              print("Height geo ID: {} meters".format(gps.height_geoid))