Skip to content

Fix invalid get_time calls #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions adafruit_ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,24 @@ class NTP:
"""Network Time Protocol (NTP) helper module for CircuitPython.
This module does not handle daylight savings or local time.

:param adafruit_esp32spi esp: ESP32SPI Module
:param adafruit_esp32spi esp: ESP32SPI object.
"""
def __init__(self, esp):
# Verify ESP32SPI module
if "ESP_SPIcontrol" in str(type(esp)):
self._esp = esp
else:
raise TypeError("Provided esp is not an ESP_SPIcontrol object")
raise TypeError("Provided object is not an ESP_SPIcontrol object.")
self.valid_time = False

def set_time(self):
"""Fetches and sets the microcontroller's current time
in seconds since since Jan 1, 1970.
"""
now = self._esp.get_time()
now = time.localtime(now[0])
rtc.RTC().datetime = now
try:
now = self._esp.get_time()
now = time.localtime(now[0])
rtc.RTC().datetime = now
self.valid_time = True
except ValueError:
return
6 changes: 5 additions & 1 deletion examples/ntp_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
ntp = NTP(esp)

# Fetch and set the microcontroller's current UTC time
ntp.set_time()
# keep retrying until a valid time is returned
while not ntp.valid_time:
ntp.set_time()
print("Failed to obtain time, retrying in 5 seconds...")
time.sleep(5)

# Get the current time in seconds since Jan 1, 1970
current_time = time.time()
Expand Down