From cca0c082c85278761268c3a7c48e9dea0656b9c6 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Mon, 10 Sep 2018 11:03:28 +0200 Subject: [PATCH] Add clock check before writing data --- circuitpython/code/cameteo.py | 4 ++-- circuitpython/code/main.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/circuitpython/code/cameteo.py b/circuitpython/code/cameteo.py index f91df6f..99c19d8 100644 --- a/circuitpython/code/cameteo.py +++ b/circuitpython/code/cameteo.py @@ -426,7 +426,7 @@ def set_clock_from_localtime(clock, threshold=2.0, debug=False): print(err) print("Local time source : " + TIME_FORMAT.format(*time.localtime()[0:6])) print("Wait some time to have proper time from time source...") - return 1 + return False # Max difference between GPS and internal RTC (in seconds): if abs(local_seconds - time.mktime(clock.datetime)) >= threshold: @@ -435,4 +435,4 @@ def set_clock_from_localtime(clock, threshold=2.0, debug=False): clock.datetime = time.localtime() # Trust localtime if there is a bias print("Clocks synced !") - return 0 + return True diff --git a/circuitpython/code/main.py b/circuitpython/code/main.py index 3498141..03d1f8f 100644 --- a/circuitpython/code/main.py +++ b/circuitpython/code/main.py @@ -41,12 +41,12 @@ TODO for v1 : * send data through UART (work-in-progress) """ __author__ = "arofarn" -__version__ = 0.2 +__version__ = 0.3 ####################### import time -import rtc import gc +import rtc import board from busio import UART # import micropython @@ -95,8 +95,8 @@ gps_data = cameteo.GPSData(debug=False) data = [sys_data, gps_data, bme_data] -rtc.set_time_source(gps_data, debug=True) - +rtc.set_time_source(gps_data) +CLOCK_SYNCED = False ############# # Main loop # @@ -108,16 +108,16 @@ while True: for src in data: src.update(current_time, verbose=PRINT_DATA) - cameteo.set_clock_from_localtime(CLOCK, threshold=2.0) + + # Sync clocks + CLOCK_SYNCED = cameteo.set_clock_from_localtime(CLOCK, threshold=2.0, debug=True) for src in data: - src.write_on_flash(current_time) + #Only write data when clocks are OK + if CLOCK_SYNCED: + src.write_on_flash(current_time) src.send_json(current_time, uart=rpi_uart, verbose=True) - # # First check if files need to rotate - # last_rotation = rotate_files(last_rotation) - - gc.collect() # micropython.mem_info(1) # print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))