Add clock check before writing data

This commit is contained in:
Pierrick C 2018-09-10 11:03:28 +02:00
parent e84682c391
commit cca0c082c8
2 changed files with 12 additions and 12 deletions

View File

@ -426,7 +426,7 @@ def set_clock_from_localtime(clock, threshold=2.0, debug=False):
print(err) print(err)
print("Local time source : " + TIME_FORMAT.format(*time.localtime()[0:6])) print("Local time source : " + TIME_FORMAT.format(*time.localtime()[0:6]))
print("Wait some time to have proper time from time source...") print("Wait some time to have proper time from time source...")
return 1 return False
# Max difference between GPS and internal RTC (in seconds): # Max difference between GPS and internal RTC (in seconds):
if abs(local_seconds - time.mktime(clock.datetime)) >= threshold: 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 clock.datetime = time.localtime() # Trust localtime if there is a bias
print("Clocks synced !") print("Clocks synced !")
return 0 return True

View File

@ -41,12 +41,12 @@ TODO for v1 :
* send data through UART (work-in-progress) * send data through UART (work-in-progress)
""" """
__author__ = "arofarn" __author__ = "arofarn"
__version__ = 0.2 __version__ = 0.3
####################### #######################
import time import time
import rtc
import gc import gc
import rtc
import board import board
from busio import UART from busio import UART
# import micropython # import micropython
@ -95,8 +95,8 @@ gps_data = cameteo.GPSData(debug=False)
data = [sys_data, gps_data, bme_data] 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 # # Main loop #
@ -108,16 +108,16 @@ while True:
for src in data: for src in data:
src.update(current_time, verbose=PRINT_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: for src in data:
#Only write data when clocks are OK
if CLOCK_SYNCED:
src.write_on_flash(current_time) src.write_on_flash(current_time)
src.send_json(current_time, uart=rpi_uart, verbose=True) 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() gc.collect()
# micropython.mem_info(1) # micropython.mem_info(1)
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc())) # print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))