Last active
February 23, 2023 19:22
-
-
Save anecdata/1b09c992df194b9fa6033ad58c6e1e49 to your computer and use it in GitHub Desktop.
NTP: manual or automatic (for CircuitPython ports with native wifi) & alternative(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import rtc | |
import wifi | |
import socketpool | |
import adafruit_ntp | |
from secrets import secrets | |
pool = socketpool.SocketPool(wifi.radio) | |
ntp = adafruit_ntp.NTP(pool, tz_offset=0) | |
wifi.radio.connect(secrets["ssid"], secrets["password"]) | |
# manual - sync ntp to the internal rtc when it needs adjustment | |
rtci = rtc.RTC() | |
rtci.datetime = ntp.datetime | |
print(time.localtime()) | |
# automatic - use ntp as the source for all time calls | |
rtc.set_time_source(ntp) | |
print(time.localtime()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative process for sub-second timestamps could be:
The fractional seconds are not accurate relative to real clock time, but the intervals between are reasonably accurate. The fractional seconds are strictly monotonic, so preserve the sequence of timestamps.
Sample output: