Skip to content

Commit 25567d6

Browse files
authored
Merge pull request #2163 from tannewt/remove_ntp
Use ESP32SPI directly instead of NTP
2 parents 86147bc + e0c283c commit 25567d6

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

PyPortal_Azure_Plant_Monitor/code.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
2121
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
2222
import neopixel
23-
from adafruit_ntp import NTP
23+
import rtc
2424
from adafruit_azureiot import IoTCentralDevice
2525
from adafruit_seesaw.seesaw import Seesaw
2626

@@ -52,11 +52,15 @@
5252
print("WiFi connected!")
5353

5454
# Time setup, needed to authenticate with Azure IoT Central
55-
ntp = NTP(esp)
56-
while not ntp.valid_time:
57-
print("Failed to obtain time, retrying in 5 seconds...")
58-
time.sleep(5)
59-
ntp.set_time()
55+
# get_time will raise ValueError if the time isn't available yet so loop until
56+
# it works.
57+
now_utc = None
58+
while now_utc is None:
59+
try:
60+
now_utc = time.localtime(esp.get_time()[0])
61+
except ValueError:
62+
pass
63+
rtc.RTC().datetime = now_utc
6064

6165
# Soil Sensor Setup
6266
i2c_bus = busio.I2C(board.SCL, board.SDA)

PyPortal_TOTP_Friend/code.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from adafruit_progressbar.progressbar import ProgressBar
1717
from adafruit_display_text.label import Label
1818
from adafruit_esp32spi import adafruit_esp32spi
19-
from adafruit_ntp import NTP
2019
from adafruit_pyportal import PyPortal
20+
import rtc
2121

2222

2323
# Background Color
@@ -207,15 +207,15 @@ def display_otp_key(secret_name, secret_otp):
207207

208208
print("Connected to ", secrets['ssid'])
209209

210-
# Initialize the NTP object
211-
ntp = NTP(esp)
212-
213-
# Fetch and set the microcontroller's current UTC time
214-
# keep retrying until a valid time is returned
215-
while not ntp.valid_time:
216-
ntp.set_time()
217-
print("Could not obtain NTP, re-fetching in 5 seconds...")
218-
time.sleep(5)
210+
# get_time will raise ValueError if the time isn't available yet so loop until
211+
# it works.
212+
now_utc = None
213+
while now_utc is None:
214+
try:
215+
now_utc = time.localtime(esp.get_time()[0])
216+
except ValueError:
217+
pass
218+
rtc.RTC().datetime = now_utc
219219

220220
# Get the current time in seconds since Jan 1, 1970
221221
t = time.time()

0 commit comments

Comments
 (0)