Skip to main content

chumicro-ntp

An SNTP (Simple Network Time Protocol) client that runs in your tick loop without blocking it.

Polls one server, advances on each runner tick, and gives you the unix seconds when the response lands. Pure Python, no compiled module, no time.sleep(). Your LED keeps blinking through the network hop. UDP transport is injected so apps with a custom socket layer don't drag chumicro-sockets into the device deploy.


Part of the ChuMicro family: small, focused Python libraries for microcontrollers and laptops. Browse all libraries.

Install

# CircuitPython (after `circup bundle-add ChuMicro/ChuMicro-Bundle-Experimental`)
circup install chumicro_ntp

# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle-Experimental/chumicro_ntp

# CPython
pip install chumicro-ntp-experimental

For bundle setup, pre-compiled .mpy bundles, the experimental channel, and details on PyPI naming, see the chumicro INSTALL guide.

Quick example

from chumicro_ntp import NTPClient
from chumicro_sockets import udp_socket
from chumicro_timing import ticks_ms

# On CircuitPython pass radio=wifi.adapter.radio (your chumicro-wifi
# WifiService's board radio); the kwarg is ignored on MP / CPython.
sock = udp_socket(radio=None)
sock.setblocking(False)                     # required: a blocking recv wedges on packet loss
client = NTPClient(socket=sock, server="pool.ntp.org")
request = client.query()
while not request.done:
    if client.check(ticks_ms()):
        client.handle(ticks_ms())
print("unix seconds:", request.unix_seconds)

chumicro_sockets.udp_socket builds the default bound UDP socket. Pass any object satisfying the sendto / recvfrom_into / close / setblocking contract (see NTPClient's docstring) to NTPClient(socket=...). chumicro_sockets.udp_socket and chumicro_sockets.testing.FakeUDPSocket are the built-in producers. NTPClient.from_config wires that default itself through the shared chumicro_sockets.sockets_factory module, imported lazily, so apps with a custom UDP transport keep chumicro-sockets out of their device deploy.

What's included

Symbol Purpose
NTPClient(socket, *, server="pool.ntp.org", port=123, timeout_ms=5000, ticks=None) Runner-shaped SNTP client. Single in-flight query at a time; mirrors HttpClient.busy.
NTPClient.query() Send a request; returns a NTPResult to poll.
NTPClient.check(now_ms) / handle(now_ms) Runner contract: handle drains the recv socket and detects timeouts.
NTPClient.cancel() Abort an in-flight query.
NTPResult Per-query handle. done, unix_seconds, error.
NTPError OSError subclass raised on protocol-level failures (short/malformed response, kiss-of-death, timeout, cancel).
chumicro_sockets.udp_socket(radio=None) Built-in bound UDP socket for NTPClient(socket=...). NTPClient.from_config reaches it through the shared chumicro_sockets.sockets_factory; a custom transport skips that import and keeps chumicro-sockets out of the deploy.

Where this fits

Depends on chumicro-sockets for UDP transport and chumicro-timing for ticks. A single pip install chumicro-ntp-experimental brings the stack. Used directly in app code; no other ChuMicro library depends on it.

Platform support

Pure-Python; runs identically on CPython, MicroPython, and CircuitPython.

Examples

Example What it shows
examples/ntp_query.py Real query against pool.ntp.org: wifi up, UDP socket via factory, runner-shaped poll loop. Cross-runtime (CP + MP).

Contributing

Issues, bug reports, and pull requests are welcome, and so is "I ran it on this board and here's what happened", some of the most useful feedback a hardware project can get. Development happens in the ChuMicro repository, whose contributing guide covers setup and the test workflow.

Docs

📖 Stable docs · Experimental docs

Find this library

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chumicro_ntp_experimental-0.15.2.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chumicro_ntp_experimental-0.15.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file chumicro_ntp_experimental-0.15.2.tar.gz.

File metadata

File hashes

Hashes for chumicro_ntp_experimental-0.15.2.tar.gz
Algorithm Hash digest
SHA256 7bb6da293ecc6d61e58df8756cbcaa6cd5d35965aa5ad780671b9f6e70949ae5
MD5 34165f8eabeab4248927eb1857318f74
BLAKE2b-256 d16486cf22cd9bf8642f762f99f4eb9835ca4bfc17251cf75998ac5b5354521d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_ntp_experimental-0.15.2.tar.gz:

Publisher: release.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chumicro_ntp_experimental-0.15.2-py3-none-any.whl.

File metadata

File hashes

Hashes for chumicro_ntp_experimental-0.15.2-py3-none-any.whl
Algorithm Hash digest
SHA256 179de6904f91d198dd2c67bfa1abd73969c4594838fe508b14e8b0667ee10b80
MD5 e8be32558bc2ea92c9013259b97ae166
BLAKE2b-256 c002248b91928375d7b88100f1dc27b6170ba2b97da9e5a02713aad058632b6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_ntp_experimental-0.15.2-py3-none-any.whl:

Publisher: release.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.15.3

2 files

This release

0.15.2 This release

2 files

0.15.1

2 files

0.15.0

2 files

0.14.0

2 files

0.13.1

2 files

0.13.0

2 files

0.12.2

2 files

0.12.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page