diff --git a/PyPortal_Azure_Plant_Monitor/code.py b/PyPortal_Azure_Plant_Monitor/code.py index 1915ec48e..3d17dfa07 100755 --- a/PyPortal_Azure_Plant_Monitor/code.py +++ b/PyPortal_Azure_Plant_Monitor/code.py @@ -20,7 +20,7 @@ from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket import neopixel -from adafruit_ntp import NTP +import rtc from adafruit_azureiot import IoTCentralDevice from adafruit_seesaw.seesaw import Seesaw @@ -52,11 +52,15 @@ print("WiFi connected!") # Time setup, needed to authenticate with Azure IoT Central -ntp = NTP(esp) -while not ntp.valid_time: - print("Failed to obtain time, retrying in 5 seconds...") - time.sleep(5) - ntp.set_time() +# get_time will raise ValueError if the time isn't available yet so loop until +# it works. +now_utc = None +while now_utc is None: + try: + now_utc = time.localtime(esp.get_time()[0]) + except ValueError: + pass +rtc.RTC().datetime = now_utc # Soil Sensor Setup i2c_bus = busio.I2C(board.SCL, board.SDA) diff --git a/PyPortal_TOTP_Friend/code.py b/PyPortal_TOTP_Friend/code.py index 476954b62..d209f8c75 100644 --- a/PyPortal_TOTP_Friend/code.py +++ b/PyPortal_TOTP_Friend/code.py @@ -16,8 +16,8 @@ from adafruit_progressbar.progressbar import ProgressBar from adafruit_display_text.label import Label from adafruit_esp32spi import adafruit_esp32spi -from adafruit_ntp import NTP from adafruit_pyportal import PyPortal +import rtc # Background Color @@ -207,15 +207,15 @@ def display_otp_key(secret_name, secret_otp): print("Connected to ", secrets['ssid']) -# Initialize the NTP object -ntp = NTP(esp) - -# Fetch and set the microcontroller's current UTC time -# keep retrying until a valid time is returned -while not ntp.valid_time: - ntp.set_time() - print("Could not obtain NTP, re-fetching in 5 seconds...") - time.sleep(5) +# get_time will raise ValueError if the time isn't available yet so loop until +# it works. +now_utc = None +while now_utc is None: + try: + now_utc = time.localtime(esp.get_time()[0]) + except ValueError: + pass +rtc.RTC().datetime = now_utc # Get the current time in seconds since Jan 1, 1970 t = time.time()