From a758b2c3f43f88c6a73acf756b7d0849286acf8f Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Sun, 29 Jul 2018 17:27:52 +0200 Subject: [PATCH] Add function to set date/time with GPS clock if available and diffence >= 5s --- circuitpython/main.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/circuitpython/main.py b/circuitpython/main.py index 474fa57..90164fb 100644 --- a/circuitpython/main.py +++ b/circuitpython/main.py @@ -139,6 +139,22 @@ def update_neopixel(data): return (rouge, vert, bleu) +def set_clock_from_GPS(): + if gps_enable and gps.has_fix: + #Convert GPS timestamp into struct_time + gps_datetime = time.struct_time((gps.timestamp_utc.tm_year, + gps.timestamp_utc.tm_mon, + gps.timestamp_utc.tm_mday, + gps.timestamp_utc.tm_hour, + gps.timestamp_utc.tm_min, + gps.timestamp_utc.tm_sec, 0, 0, 0)) + #Max difference between GPS and internal RTC (in seconds): + if abs(time.mktime(gps_datetime) - time.mktime(clock.datetime)) >= 5.0: + print("Clock difference with GPS!") + print("Previous date/time : " + datetime_format.format(*clock.datetime[0:6])) + clock.datetime = gps_datetime #Trust GPS if there is a bias + print("New date/time : " + datetime_format.format(*clock.datetime[0:6])) + ######### # Setup # ######### @@ -148,7 +164,7 @@ gc.collect() #Enable RTC of the feather M0 board clock = rtc.RTC() -clock.datetime = time.struct_time((2018, 7, 29, 15, 31, 30, 0, 0, 0)) +#clock.datetime = time.struct_time((2018, 7, 29, 15, 31, 30, 0, 0, 0)) # BME280 sensors (I2C) i2c = I2C(board.SCL, board.SDA) @@ -194,6 +210,7 @@ while True: current = time.monotonic() if current - last_update >= update_interval: last_update = current + set_clock_from_GPS() data.update() if print_data: data.show()