File tree 2 files changed +15
-6
lines changed
2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -49,19 +49,24 @@ class NTP:
49
49
"""Network Time Protocol (NTP) helper module for CircuitPython.
50
50
This module does not handle daylight savings or local time.
51
51
52
- :param adafruit_esp32spi esp: ESP32SPI Module
52
+ :param adafruit_esp32spi esp: ESP32SPI object.
53
53
"""
54
54
def __init__ (self , esp ):
55
55
# Verify ESP32SPI module
56
56
if "ESP_SPIcontrol" in str (type (esp )):
57
57
self ._esp = esp
58
58
else :
59
- raise TypeError ("Provided esp is not an ESP_SPIcontrol object" )
59
+ raise TypeError ("Provided object is not an ESP_SPIcontrol object." )
60
+ self .valid_time = False
60
61
61
62
def set_time (self ):
62
63
"""Fetches and sets the microcontroller's current time
63
64
in seconds since since Jan 1, 1970.
64
65
"""
65
- now = self ._esp .get_time ()
66
- now = time .localtime (now [0 ])
67
- rtc .RTC ().datetime = now
66
+ try :
67
+ now = self ._esp .get_time ()
68
+ now = time .localtime (now [0 ])
69
+ rtc .RTC ().datetime = now
70
+ self .valid_time = True
71
+ except ValueError :
72
+ return
Original file line number Diff line number Diff line change 30
30
ntp = NTP (esp )
31
31
32
32
# Fetch and set the microcontroller's current UTC time
33
- ntp .set_time ()
33
+ # keep retrying until a valid time is returned
34
+ while not ntp .valid_time :
35
+ ntp .set_time ()
36
+ print ("Failed to obtain time, retrying in 5 seconds..." )
37
+ time .sleep (5 )
34
38
35
39
# Get the current time in seconds since Jan 1, 1970
36
40
current_time = time .time ()
You can’t perform that action at this time.
0 commit comments