Skip to content

Commit

Permalink
Merge pull request #43 from tannewt/use_real_ntp
Browse files Browse the repository at this point in the history
Switch examples for adafruit_ntp 3+
  • Loading branch information
tannewt committed May 17, 2022
2 parents 3ffbaa6 + aea1fd9 commit 084d255
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 154 deletions.
2 changes: 0 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
autodoc_mock_imports = [
"adafruit_binascii",
"adafruit_logging",
"adafruit_requests",
"adafruit_hashlib",
"adafruit_ntp",
"adafruit_minimqtt",
]

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_central_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -53,11 +53,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
8 changes: 2 additions & 6 deletions examples/azureiot_esp32spi/azureiot_central_notconnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -55,11 +55,7 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
time.sleep(5)
ntp.set_time()
rtc.RTC().datetime = time.localtime(esp.get_time()[0])

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_central_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -54,11 +54,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_central_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -55,11 +55,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_hub_directmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -53,11 +53,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_hub_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -55,11 +55,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_hub_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -55,11 +55,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
16 changes: 10 additions & 6 deletions examples/azureiot_esp32spi/azureiot_hub_twin_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import rtc
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_ntp import NTP

# Get wifi details and more from a secrets.py file
try:
Expand Down Expand Up @@ -54,11 +54,15 @@

print("Getting the time...")

ntp = NTP(esp)
# Wait for a valid time to be received
while not ntp.valid_time:
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

print("Time:", str(time.time()))

Expand Down
21 changes: 8 additions & 13 deletions examples/azureiot_native_networking/azureiot_central_commands.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import ssl
import time

import rtc
import socketpool
import wifi

import adafruit_requests
import adafruit_ntp
from adafruit_azureiot import IoTCentralDevice
from adafruit_azureiot.iot_mqtt import IoTResponse

Expand All @@ -27,17 +26,13 @@
if time.localtime().tm_year < 2022:
print("Setting System Time in UTC")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
if response:
if response.status_code == 200:
r = rtc.RTC()
r.datetime = time.localtime(int(response.text))
print(f"System Time: {r.datetime}")
else:
print("Setting time failed")
else:
print("Year seems good, skipping set time.")
ntp = adafruit_ntp.NTP(pool, tz_offset=0)

# NOTE: This changes the system time so make sure you aren't assuming that time
# doesn't jump.
rtc.RTC().datetime = ntp.datetime
else:
print("Year seems good, skipping set time.")

# To use Azure IoT Central, you will need to create an IoT Central app.
# You can either create a free tier app that will live for 7 days without an Azure subscription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

import json
import random
import ssl
import time

import rtc
import socketpool
import wifi

import adafruit_requests
import adafruit_ntp
from adafruit_azureiot import (
IoTCentralDevice,
IoTError,
Expand All @@ -31,17 +30,13 @@
if time.localtime().tm_year < 2022:
print("Setting System Time in UTC")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
if response:
if response.status_code == 200:
r = rtc.RTC()
r.datetime = time.localtime(int(response.text))
print(f"System Time: {r.datetime}")
else:
print("Setting time failed")
else:
print("Year seems good, skipping set time.")
ntp = adafruit_ntp.NTP(pool, tz_offset=0)

# NOTE: This changes the system time so make sure you aren't assuming that time
# doesn't jump.
rtc.RTC().datetime = ntp.datetime
else:
print("Year seems good, skipping set time.")

# To use Azure IoT Central, you will need to create an IoT Central app.
# You can either create a free tier app that will live for 7 days without an Azure subscription,
Expand Down
21 changes: 8 additions & 13 deletions examples/azureiot_native_networking/azureiot_central_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
# SPDX-License-Identifier: MIT

import random
import ssl
import time

import rtc
import socketpool
import wifi

import adafruit_requests
import adafruit_ntp
from adafruit_azureiot import IoTCentralDevice

# Get wifi details and more from a secrets.py file
Expand All @@ -27,17 +26,13 @@
if time.localtime().tm_year < 2022:
print("Setting System Time in UTC")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
if response:
if response.status_code == 200:
r = rtc.RTC()
r.datetime = time.localtime(int(response.text))
print(f"System Time: {r.datetime}")
else:
print("Setting time failed")
else:
print("Year seems good, skipping set time.")
ntp = adafruit_ntp.NTP(pool, tz_offset=0)

# NOTE: This changes the system time so make sure you aren't assuming that time
# doesn't jump.
rtc.RTC().datetime = ntp.datetime
else:
print("Year seems good, skipping set time.")

# To use Azure IoT Central, you will need to create an IoT Central app.
# You can either create a free tier app that will live for 7 days without an Azure subscription,
Expand Down

0 comments on commit 084d255

Please sign in to comment.