|
| 1 | +import busio, board, time, rtc |
| 2 | +import adafruit_gps |
| 3 | + |
| 4 | +uart = busio.UART(board.TX, board.RX, baudrate = 9600, timeout=3000) |
| 5 | + |
| 6 | +gps = adafruit_gps.GPS(uart, debug=False) |
| 7 | +gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 8 | +gps.send_command(b'PMTK220,1000') |
| 9 | + |
| 10 | +print("Set GPS as time source") |
| 11 | +rtc.set_time_source(gps) |
| 12 | + |
| 13 | +last_print = time.monotonic() |
| 14 | +while True: |
| 15 | + |
| 16 | + gps.update() |
| 17 | + # Every second print out current time from GPS, RTC and time.localtime() |
| 18 | + current = time.monotonic() |
| 19 | + if current - last_print >= 1.0: |
| 20 | + last_print = current |
| 21 | + print('Fix timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format( |
| 22 | + gps.timestamp_utc.tm_mon, # Grab parts of the time from the |
| 23 | + gps.timestamp_utc.tm_mday, # struct_time object that holds |
| 24 | + gps.timestamp_utc.tm_year, # the fix time. Note you might |
| 25 | + gps.timestamp_utc.tm_hour, # not get all data like year, day, |
| 26 | + gps.timestamp_utc.tm_min, # month! |
| 27 | + gps.timestamp_utc.tm_sec)) |
| 28 | + print('RTC timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format( |
| 29 | + rtc.RTC.datetime.tm_mon, |
| 30 | + rtc.RTC.datetime.tm_mday, |
| 31 | + rtc.RTC.datetime.tm_year, |
| 32 | + rtc.RTC.datetime.tm_hour, |
| 33 | + rtc.RTC.datetime.tm_min, |
| 34 | + rtc.RTC.datetime.tm_sec)) |
| 35 | + local_time = time.localtime() |
| 36 | + print("Local time: {:02}/{:02}/{} {:02}:{:02}:{:02}".format( |
| 37 | + local_time.tm_mon, |
| 38 | + local_time.tm_mday, |
| 39 | + local_time.tm_year, |
| 40 | + local_time.tm_hour, |
| 41 | + local_time.tm_min, |
| 42 | + local_time.tm_sec)) |
0 commit comments