Skip to content

Commit 98fd191

Browse files
committed
Update examples to use settings.toml for credentials
make precommit happy with line wrapping
1 parent 784a8b4 commit 98fd191

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

adafruit_ntp.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ def _update_time_sync(self) -> None:
7979
is received within socket_timeout seconds, ArithmeticError for substantially incorrect
8080
NTP results."""
8181
if self._socket_address is None:
82-
self._socket_address = self._pool.getaddrinfo(
83-
self._server, self._port)[0][4]
82+
self._socket_address = self._pool.getaddrinfo(self._server, self._port)[0][
83+
4
84+
]
8485

8586
self._packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode
8687
for i in range(1, PACKET_SIZE):
@@ -103,14 +104,18 @@ def _update_time_sync(self) -> None:
103104
srv_send_s, srv_send_f = struct.unpack_from("!II", self._packet, offset=40)
104105

105106
# Convert the server times from NTP to UTC for local use
106-
srv_recv_ns = (srv_recv_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + \
107-
(srv_recv_f * 1_000_000_000 // 2**32)
108-
srv_send_ns = (srv_send_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + \
109-
(srv_send_f * 1_000_000_000 // 2**32)
107+
srv_recv_ns = (srv_recv_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + (
108+
srv_recv_f * 1_000_000_000 // 2**32
109+
)
110+
srv_send_ns = (srv_send_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + (
111+
srv_send_f * 1_000_000_000 // 2**32
112+
)
110113

111114
# _round_trip_delay = (local_recv_ns - local_send_ns) - (srv_send_ns - srv_recv_ns)
112115
# Calculate (best estimate) offset between server UTC and board monotonic_ns time
113-
clock_offset = ((srv_recv_ns - local_send_ns) + (srv_send_ns - local_recv_ns)) // 2
116+
clock_offset = (
117+
(srv_recv_ns - local_send_ns) + (srv_send_ns - local_recv_ns)
118+
) // 2
114119

115120
self._monotonic_start_ns = clock_offset + self._tz_offset * 1_000_000_000
116121

@@ -122,7 +127,9 @@ def datetime(self) -> time.struct_time:
122127
self._update_time_sync()
123128

124129
# Calculate the current time based on the current and start monotonic times
125-
current_time_s = (time.monotonic_ns() + self._monotonic_start_ns) // 1_000_000_000
130+
current_time_s = (
131+
time.monotonic_ns() + self._monotonic_start_ns
132+
) // 1_000_000_000
126133

127134
return time.localtime(current_time_s)
128135

examples/ntp_set_rtc.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Example demonstrating how to set the realtime clock (RTC) based on NTP time."""
55

6+
import os
67
import time
78

89
import rtc
@@ -11,15 +12,19 @@
1112

1213
import adafruit_ntp
1314

14-
# Get wifi details and more from a secrets.py file
15+
# Get wifi AP credentials from a settings.toml file
16+
wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
17+
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
18+
if wifi_ssid is None:
19+
print("WiFi credentials are kept in settings.toml, please add them there!")
20+
raise ValueError("SSID not found in environment variables")
21+
1522
try:
16-
from secrets import secrets
17-
except ImportError:
18-
print("WiFi secrets are kept in secrets.py, please add them there!")
23+
wifi.radio.connect(wifi_ssid, wifi_password)
24+
except ConnectionError:
25+
print("Failed to connect to WiFi with provided credentials")
1926
raise
2027

21-
wifi.radio.connect(secrets["ssid"], secrets["password"])
22-
2328
pool = socketpool.SocketPool(wifi.radio)
2429
ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
2530

examples/ntp_simpletest.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33

44
"""Print out time based on NTP."""
55

6+
import os
67
import time
78

89
import socketpool
910
import wifi
1011

1112
import adafruit_ntp
1213

13-
# Get wifi details and more from a secrets.py file
14+
# Get wifi AP credentials from a settings.toml file
15+
wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
16+
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
17+
if wifi_ssid is None:
18+
print("WiFi credentials are kept in settings.toml, please add them there!")
19+
raise ValueError("SSID not found in environment variables")
20+
1421
try:
15-
from secrets import secrets
16-
except ImportError:
17-
print("WiFi secrets are kept in secrets.py, please add them there!")
22+
wifi.radio.connect(wifi_ssid, wifi_password)
23+
except ConnectionError:
24+
print("Failed to connect to WiFi with provided credentials")
1825
raise
1926

20-
wifi.radio.connect(secrets["ssid"], secrets["password"])
21-
2227
pool = socketpool.SocketPool(wifi.radio)
2328
ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
2429

0 commit comments

Comments
 (0)