Skip to content

Add datetime property to use GPS object as time source #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions adafruit_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def has_fix(self):
"""True if a current fix for location information is available."""
return self.fix_quality is not None and self.fix_quality >= 1

@property
def datetime(self):
"""Return struct_time object to feed rtc.set_time_source() function"""
return self.timestamp_utc

def _parse_sentence(self):
# Parse any NMEA sentence that is available.
# pylint: disable=len-as-condition
Expand Down
54 changes: 54 additions & 0 deletions examples/time_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Simple script using GPS timestamps as RTC time source
# The GPS timestamps are available without a fix and keep the track of
# time while there is powersource (ie coin cell battery)

import time
import board
import rtc
import busio
import adafruit_gps

uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=3000)

gps = adafruit_gps.GPS(uart, debug=False)
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
gps.send_command(b'PMTK220,1000')

print("Set GPS as time source")
rtc.set_time_source(gps)

last_print = time.monotonic()
while True:

gps.update()
# Every second print out current time from GPS, RTC and time.localtime()
current = time.monotonic()
if current - last_print >= 1.0:
last_print = current
# Time & date from GPS informations
print('Fix timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format(
gps.timestamp_utc.tm_mon, # Grab parts of the time from the
gps.timestamp_utc.tm_mday, # struct_time object that holds
gps.timestamp_utc.tm_year, # the fix time. Note you might
gps.timestamp_utc.tm_hour, # not get all data like year, day,
gps.timestamp_utc.tm_min, # month!
gps.timestamp_utc.tm_sec))

#Time & date from internal RTC
print('RTC timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format(
rtc.RTC.datetime.tm_mon,
rtc.RTC.datetime.tm_mday,
rtc.RTC.datetime.tm_year,
rtc.RTC.datetime.tm_hour,
rtc.RTC.datetime.tm_min,
rtc.RTC.datetime.tm_sec))

#Time & date from time.localtime() function
local_time = time.localtime()
print("Local time: {:02}/{:02}/{} {:02}:{:02}:{:02}".format(
local_time.tm_mon,
local_time.tm_mday,
local_time.tm_year,
local_time.tm_hour,
local_time.tm_min,
local_time.tm_sec))