NTP Sync Flow
Run the NTP Sync Flow MicroSim Fullscreen
About This MicroSim
A WiFi clock repeats the same routine for as long as it runs: connect, ask an NTP server for the time, convert it to local time, show it, wait, and do it again. Real networks fail sometimes, so a good clock also plans for two kinds of trouble:
- WiFi fails: the router is busy, the signal is weak, or the password is wrong. The Pico gives up after a timeout and retries a few times.
- NTP fails: WiFi works, but the server does not answer, and
ntptime.settime()raises anOSError. The Pico catches the error and retries.
If the retries run out, the clock does not crash. It keeps running on its last known time, because its internal RTC keeps counting, and it tries again at the next scheduled sync. The green path in the flowchart is the normal route; the orange paths are the retries and the fallback.
The code for each step comes from the main-w.py program in Chapter 14.
How to Use
- Read the flowchart from Start at the top. The green path shows what happens when everything works.
- Click (or hover over) any box to see the MicroPython code for that step in the panel on the right.
- Press WiFi fails to light up the WiFi retry path in orange. Notice that the NTP steps are never reached.
- Press NTP fails to light up the NTP retry path instead. Press the active button again to return to the normal path.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Learning Objective
Students will sequence the steps of a time sync and identify what to do at each failure point (Bloom: Understanding, Applying).
Grade Level
9-12 (Clocks and Watches with AI)
Duration
15 minutes
Prerequisites
- WiFi connection,
secrets.py, andntptime(Chapter 14) tryandexcept(Chapter 4)
Activities
- Sequence (4 min): Before opening the MicroSim, give students the step names on cards and have them put the steps in order. Then compare with the flowchart.
- Match code to steps (4 min): Students click each box and point to the matching lines in the chapter's
main-w.pyprogram. - Failure drill (4 min): For each toggle, students describe in one sentence what the user of the clock would see on the display while the failure happens.
- Extend (3 min): Where would you add a step that writes the synced time back to a DS3231 (Chapter 13)? Sketch the new box and its arrows.
Assessment
- Put these steps in order: apply time zone offset, connect to WiFi,
ntptime.settime(), readsecrets.py. - What exception does
ntptime.settime()raise when the server does not answer, and how should the program handle it? (OSError; catch it, wait, and retry a few times.) - Explain why "keep running on last known time" is a better fallback than stopping the program.
References
- Network Time Protocol - Wikipedia overview of NTP.
- Connecting to the Internet with Raspberry Pi Pico W - Raspberry Pi guide to WiFi on the Pico W.
- MicroPython network module - Documentation for
network.WLAN. - Mermaid flowchart syntax - The diagram language used by this MicroSim, including
clickcallbacks.