|
| 1 | +# Simple script using GPS timestamps as RTC time source |
| 2 | +# The GPS timestamps are available without a fix and keep the track of |
| 3 | +# time while there is powersource (ie coin cell battery) |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import rtc |
| 8 | +import busio |
| 9 | +import adafruit_gps |
| 10 | + |
| 11 | +uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=3000) |
| 12 | + |
| 13 | +gps = adafruit_gps.GPS(uart, debug=False) |
| 14 | +gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') |
| 15 | +gps.send_command(b'PMTK220,1000') |
| 16 | + |
| 17 | +print("Set GPS as time source") |
| 18 | +rtc.set_time_source(gps) |
| 19 | + |
| 20 | +last_print = time.monotonic() |
| 21 | +while True: |
| 22 | + |
| 23 | + gps.update() |
| 24 | + # Every second print out current time from GPS, RTC and time.localtime() |
| 25 | + current = time.monotonic() |
| 26 | + if current - last_print >= 1.0: |
| 27 | + last_print = current |
| 28 | + # Time & date from GPS informations |
| 29 | + print('Fix timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format( |
| 30 | + gps.timestamp_utc.tm_mon, # Grab parts of the time from the |
| 31 | + gps.timestamp_utc.tm_mday, # struct_time object that holds |
| 32 | + gps.timestamp_utc.tm_year, # the fix time. Note you might |
| 33 | + gps.timestamp_utc.tm_hour, # not get all data like year, day, |
| 34 | + gps.timestamp_utc.tm_min, # month! |
| 35 | + gps.timestamp_utc.tm_sec)) |
| 36 | + |
| 37 | + #Time & date from internal RTC |
| 38 | + print('RTC timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format( |
| 39 | + rtc.RTC.datetime.tm_mon, |
| 40 | + rtc.RTC.datetime.tm_mday, |
| 41 | + rtc.RTC.datetime.tm_year, |
| 42 | + rtc.RTC.datetime.tm_hour, |
| 43 | + rtc.RTC.datetime.tm_min, |
| 44 | + rtc.RTC.datetime.tm_sec)) |
| 45 | + |
| 46 | + #Time & date from time.localtime() function |
| 47 | + local_time = time.localtime() |
| 48 | + print("Local time: {:02}/{:02}/{} {:02}:{:02}:{:02}".format( |
| 49 | + local_time.tm_mon, |
| 50 | + local_time.tm_mday, |
| 51 | + local_time.tm_year, |
| 52 | + local_time.tm_hour, |
| 53 | + local_time.tm_min, |
| 54 | + local_time.tm_sec)) |
0 commit comments