Skip to content

Commit 69b0131

Browse files
authored
Merge pull request #3 from brentru/fix-overflow
Fix invalid get_time calls
2 parents 3bdb4b4 + 2523230 commit 69b0131

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

adafruit_ntp.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,24 @@ class 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

examples/ntp_simpletest.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
ntp = 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
3640
current_time = time.time()

0 commit comments

Comments
 (0)