pico-examples icon indicating copy to clipboard operation
pico-examples copied to clipboard

add example pico_w/wifi/ntp_system_time

Open mjcross opened this issue 2 months ago • 8 comments

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).

mjcross avatar Oct 26 '25 21:10 mjcross

Hmmm, if #716 and #714 got together and had a baby... :thinking: :joy:

lurch avatar Oct 27 '25 18:10 lurch

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

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

mjcross avatar Oct 27 '25 22:10 mjcross

Thanks for the extra comments :+1:
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() :joy:

lurch avatar Oct 28 '25 01:10 lurch

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 avatar Oct 28 '25 08:10 mjcross

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

mjcross avatar Oct 28 '25 10:10 mjcross

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

mjcross avatar Oct 28 '25 13:10 mjcross

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

mjcross avatar Oct 28 '25 14:10 mjcross

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 avatar Oct 30 '25 20:10 mjcross