Skip to content

Commit 4527f57

Browse files
authored
Merge pull request #9 from arofarn/master
Add datetime property to use GPS object as time source
2 parents a18af4b + 8176e15 commit 4527f57

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

adafruit_gps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def has_fix(self):
133133
"""True if a current fix for location information is available."""
134134
return self.fix_quality is not None and self.fix_quality >= 1
135135

136+
@property
137+
def datetime(self):
138+
"""Return struct_time object to feed rtc.set_time_source() function"""
139+
return self.timestamp_utc
140+
136141
def _parse_sentence(self):
137142
# Parse any NMEA sentence that is available.
138143
# pylint: disable=len-as-condition

examples/time_source.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)