File tree Expand file tree Collapse file tree 2 files changed +16
-7
lines changed Expand file tree Collapse file tree 2 files changed +16
-7
lines changed Original file line number Diff line number Diff line change 4242import time
4343import rtc
4444
45- __version__ = "0.0.0-auto .0"
45+ __version__ = "1.0 .0"
4646__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NTP.git"
4747
4848class NTP :
4949 """Network Time Protocol (NTP) helper module for CircuitPython.
5050 This module does not handle daylight savings or local time.
5151
52- :param adafruit_esp32spi esp: ESP32SPI Module
52+ :param adafruit_esp32spi esp: ESP32SPI object.
5353 """
5454 def __init__ (self , esp ):
5555 # Verify ESP32SPI module
5656 if "ESP_SPIcontrol" in str (type (esp )):
5757 self ._esp = esp
5858 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
6061
6162 def set_time (self ):
6263 """Fetches and sets the microcontroller's current time
6364 in seconds since since Jan 1, 1970.
6465 """
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 3030ntp = NTP (esp )
3131
3232# 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 )
3438
3539# Get the current time in seconds since Jan 1, 1970
3640current_time = time .time ()
You can’t perform that action at this time.
0 commit comments