Skip to content

add example pico_w/wifi/ntp_system_time#716

Merged
peterharperuk merged 12 commits into
raspberrypi:developfrom
mjcross:ntp_system_time
Jun 12, 2026
Merged

add example pico_w/wifi/ntp_system_time#716
peterharperuk merged 12 commits into
raspberrypi:developfrom
mjcross:ntp_system_time

Conversation

@mjcross

@mjcross mjcross commented Oct 26, 2025

Copy link
Copy Markdown
Contributor

A proposed new NTP example that differs from the existing pico_w/wifi/ntp_client in the following ways:

  1. uses the SNTP app already provided by lwIP (in threadsafe background mode)
  2. uses pico_aon_timer to create a time-of-day clock that can be read asynchronously from user code
  3. illustrates the use of a POSIX timezone (TZ) to convert UTC to local time with daylight saving
  4. includes a README.md to explain what the example does and how to use it

The program connects to Wi-Fi, initialises the lwIP SNTP app, syncs the pico_aon_timer to NTP and then enters a loop to display the local time in London (or UTC, or whichever timezone the user defines); resynchronising to ntp.pool.org in the background every hour.

I suggest it's quite an important use case for the Pico-W, and illustrates a couple of features users may find helpful (such as how to create and use a POSIX timezone).

Comment thread pico_w/wifi/ntp_system_time/README.md Outdated
Comment thread README.md Outdated
Comment thread pico_w/wifi/ntp_system_time/README.md Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
@lurch

lurch commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Hmmm, if #716 and #714 got together and had a baby... 🤔 😂

@mjcross

mjcross commented Oct 27, 2025

Copy link
Copy Markdown
Contributor Author

Hmmm, if #716 and #714 got together and had a baby... 🤔 😂

https://digitalcollections.smu.edu/digital/collection/tir/id/205/

@lurch

lurch commented Oct 28, 2025

Copy link
Copy Markdown
Contributor

Thanks for the extra comments 👍
Not having done any time-based processing in C myself, I was misreading asctime() as ascii_time() whereas I guess it's actually meant to be read as as_ctime() 😂

@mjcross

mjcross commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

I was misreading asctime() as ascii_time() whereas I guess it's actually meant to be read as as_ctime() 😂

Oooo never considered that... My "lengthy and extensive research" (ha ha) suggests it might be the former: https://retrocomputing.stackexchange.com/questions/25601/what-is-the-meaning-of-asctime
In fact for anything more complex than a timestamp it's probably better to use strftime() but that's a bit OTT for an embedded app :-)

@mjcross

mjcross commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

Found a way to avoid hardcoding the timezone names in the printf() :-)

Comment thread pico_w/wifi/ntp_system_time/README.md Outdated
Comment thread pico_w/wifi/ntp_system_time/lwipopts.h
Comment thread pico_w/wifi/ntp_system_time/README.md Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
@mjcross

mjcross commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

Many thanks for the excellent suggestions - should be all wrapped up in the latest commit (please let me know if I missed anything)

Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
Comment thread pico_w/wifi/ntp_system_time/ntp_system_time.c Outdated
@mjcross

mjcross commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

Looking really good now - many thanks again for your time to review

@mjcross

mjcross commented Oct 30, 2025

Copy link
Copy Markdown
Contributor Author

Spent far too long today chasing down why some simple test code couldn't find the function definition for setenv(). Eventually realised it was because in the example I'd forgotten to #include <stdlib.h>, and got away with it because lwIP pulls it in.
Hopefully by importing it explicitly it'll save someone else from the same fate...

@mjcross

mjcross commented Jan 9, 2026

Copy link
Copy Markdown
Contributor Author

I discovered this was giving very incorrect times of day when run on RP2040. This is because the aon_timer on that platform has a resolution of only one second, which is incompatible with SNTP round-trip compensation. To fix this I have added a test in the example's CMakeLists.txt to disable SNTP_COMP_ROUNDTRIP in lwipopts.h for RP2040.
If you know of a cleaner/better way of handling it I'd be happy to oblige :-)

@lurch

lurch commented Jan 10, 2026

Copy link
Copy Markdown
Contributor

Looking at the RP2040 datasheet it seems like you could set up the RTC peripheral to trigger an interrupt once a minute (e.g. when the "seconds" value rolls around to 0), and then you could use the callback for that interrupt to read the current Timer peripheral value ("The system timer peripheral on RP2040 provides a global microsecond timebase for the system"). And then when you need the time with a smaller granularity than a second, you could re-read the timer again, subtract the value you stored in the callback, and that would give you the number of microseconds since the start of the last minute? (I guess it's very unlikely that an SNTP round-trip would take longer than a minute!)

Of course it's entirely up to you to decide if it's worth adding that level of complexity to your example 😉

@mjcross

mjcross commented Jan 10, 2026

Copy link
Copy Markdown
Contributor Author

Looking at the RP2040 datasheet it seems like you could set up the RTC peripheral to trigger an interrupt once a minute
[...]

very inventive!

Of course it's entirely up to you to decide if it's worth adding that level of complexity to your example 😉

To be honest if anyone needs round-trip compensation that much it'd be more cost-effective to just send them a free Pico2_W!

@mjcross

mjcross commented Jan 10, 2026

Copy link
Copy Markdown
Contributor Author

@lurch I was thinking more of the proposed method of adjusting the lwIP options depending on the platform (i.e. the condition in the CMakeLists.txt): I was wondering whether there was a more preferred way of doing that :-)

@lurch

lurch commented Jan 11, 2026

Copy link
Copy Markdown
Contributor

@lurch I was thinking more of the proposed method of adjusting the lwIP options depending on the platform (i.e. the condition in the CMakeLists.txt): I was wondering whether there was a more preferred way of doing that :-)

That's more of a question for @peterharperuk - he's much more familiar with lwIP than I am!

@mjcross

mjcross commented Jan 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @lurch 👍
Having had a look at §2.6 of the SDK pdf I'm more comfortable that the conditional target_compiler_definitions() in CMake is a fairly normal thing to do.

@peterharperuk peterharperuk added this to the 2.2.1 milestone Jun 5, 2026
@peterharperuk peterharperuk merged commit 852afe7 into raspberrypi:develop Jun 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants