Skip to content

Commit 1c47ea7

Browse files
author
brentru
committed
patching overflow error
1 parent 3bdb4b4 commit 1c47ea7

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

adafruit_ntp.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,31 @@
4242
import time
4343
import 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

4848
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

Lines changed: 5 additions & 1 deletion
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)