Skip to content

Commit f43a8e7

Browse files
authored
Merge pull request #51 from tannewt/update_readme
Update readme for time sync changes
2 parents feb6fd1 + ae2b589 commit f43a8e7

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

README.rst

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,32 @@ To create an Azure IoT Hub instance or an Azure IoT Central app, you will need a
6666
ESP32 AirLift Networking
6767
========================
6868

69-
To use this library, you will need to create an ESP32_SPI WifiManager, connected to WiFi. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is via the `Adafruit CircuitPython NTP <https://github.com/adafruit/Adafruit_CircuitPython_NTP>`_ library with the following code:
69+
To use this library, you will need to create an ESP32_SPI WifiManager, connected to WiFi. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is with the following code:
7070

7171
.. code-block:: python
7272
73-
ntp = NTP(esp)
74-
75-
# Wait for a valid time to be received
76-
while not ntp.valid_time:
77-
time.sleep(5)
78-
ntp.set_time()
73+
# get_time will raise ValueError if the time isn't available yet so loop until
74+
# it works.
75+
now_utc = None
76+
while now_utc is None:
77+
try:
78+
now_utc = time.localtime(esp.get_time()[0])
79+
except ValueError:
80+
pass
81+
rtc.RTC().datetime = now_utc
7982
8083
Native Networking
8184
=================
82-
To use this library, with boards that have native networking support, you need to be connected to a network. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is by using the `Adafruit IoT Time API <https://io.adafruit.com/api/docs/#time>`_ via the `Adafruit Requests library <https://github.com/adafruit/Adafruit_CircuitPython_Requests>`_ with the following code:
85+
To use this library, with boards that have native networking support, you need to be connected to a network. You will also need to set the current time, as this is used to generate time-based authentication keys. One way to do this is the `Adafruit NTP library <https://github.com/adafruit/Adafruit_CircuitPython_NTP>`_ with the following code:
8386

8487
.. code-block:: python
8588
8689
pool = socketpool.SocketPool(wifi.radio)
87-
requests = adafruit_requests.Session(pool, ssl.create_default_context())
88-
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
89-
if response:
90-
if response.status_code == 200:
91-
r = rtc.RTC()
92-
r.datetime = time.localtime(int(response.text))
93-
print(f"System Time: {r.datetime}")
94-
else:
95-
print("Setting time failed")
90+
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
91+
92+
# NOTE: This changes the system time so make sure you aren't assuming that time
93+
# doesn't jump.
94+
rtc.RTC().datetime = ntp.datetime
9695
9796
Azure IoT Hub
9897
-------------

0 commit comments

Comments
 (0)