chumicro-ntp¶
Non-blocking SNTP client for CircuitPython, MicroPython, and CPython.
Pure-Python, takes a UDP socket you provide, returns the server's transmit timestamp without blocking your tick loop.
Install¶
# CircuitPython (after `circup bundle-add ChuMicro/ChuMicro-Bundle`)
circup install chumicro_ntp
# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle/chumicro_ntp
# CPython
pip install chumicro-ntp
No board running yet? Start here goes from a new board to your own code running on it. Installing libraries covers registering the bundle, the experimental channel, and the pre-compiled .mpy packages.
Quick example¶
from chumicro_ntp import NTPClient
from chumicro_sockets import udp_socket
from chumicro_timing import ticks_ms
from chumicro_wifi import WifiConfig, WifiService
# The board has to be on the network first; this is the chumicro-wifi
# service from the ChuMicro quick start. Drive it from your loop until
# wifi.connected is True, then open the socket. CircuitPython takes the
# radio from the service; MicroPython and CPython take no radio at all.
wifi = WifiService(WifiConfig(ssid="home-wifi", password="…"))
sock = udp_socket(radio=wifi.adapter.radio)
sock.setblocking(False)
client = NTPClient(socket=sock, server="pool.ntp.org")
request = client.query()
while not request.done:
now = ticks_ms()
if client.check(now):
client.handle(now)
print("unix seconds:", request.unix_seconds)
In a program with more going on, hand the loop to chumicro-runner:
from chumicro_runner import Runner
runner = Runner()
runner.add(client)
while not request.done:
now = runner.tick()
runner.wait(now)
wait() parks the CPU until the reply lands.
Documentation¶
- User Guide: getting started, bringing your own UDP transport, the runner pattern, and the failure modes a query can end in
- API Reference:
NTPClient, theNTPResulthandlequery()returns, andNTPError