Skip to content

Use ESP32SPI directly instead of NTP #2163

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 1 commit into from
May 17, 2022
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
16 changes: 10 additions & 6 deletions PyPortal_Azure_Plant_Monitor/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions PyPortal_TOTP_Friend/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down