NetBurner 3.5.8
PDF Version
Network Time Server (NTP) Client

Example Path: examples/NTPClient

NTP Client Example Application

A NetBurner application that synchronizes its clock from an Internet time server using the Network Time Protocol (NTP) and serves a small web dashboard for viewing the time in any time zone.

Warning
For this example to function, your NetBurner device must have access to the Internet with a working configuration for the IP address, mask and gateway.

Overview

The device runs an NtpClientServlet that keeps the system clock synchronized with an NTP server. A web page reads that clock live and displays it for a user-selected time zone, alongside a UTC/US world clock and the current NTP sync status. The same information is also logged to the debug serial port. It serves as a practical example of network time synchronization and of using the global time zone database in timezones.h.

Web Interface

Browse to http://<device-ip>/ to reach the dashboard:

  • NTP Sync Status — status pills showing whether time is valid, how long ago it last updated, and which server is in use, plus a Sync now button that forces an immediate refresh.
  • Local Time — a live clock and full date line for the selected time zone. The page re-fetches every couple of seconds so the time stays current.
  • UTC & US Time Zones — a world-clock table showing UTC plus Eastern, Central, Mountain, and Pacific time.
  • Change time zone — opens a picker (settz.html) listing every zone from the timezones.h database, grouped into US/Canada and International using each record's bUsCanada flag. Selecting a zone applies its POSIX TZ string with tzsetchar().

All dynamic values are supplied by <!--CPPCALL ... --> data providers in the C++ source; no page markup is built in C++. The pages use a self-contained stylesheet (plain CSS, no framework or CDN) so they render directly from the device with no external dependencies.

Time Zone Features

Supported US World-Clock Zones

The world-clock table uses predefined POSIX format strings for the major US time zones:

  • Eastern Time (EST/EDT)
  • Central Time (CST/CDT)
  • Mountain Time (MST/MDT)
  • Pacific Time (PST/PDT)

Time Zone Selection

The picker offers the complete TZRecords[] database, split into:

  • US/Canada Time Zones: records flagged with bUsCanada
  • International Time Zones: the rest of the global database
Note
Good example of using the global time zone database in timezones.h

Application Flow

  1. Initialization: System starts, enables network diagnostics, and starts the web server.
  2. Network Setup: Waits for an active network connection (10-second timeout).
  3. Default Zone: Selects US Pacific as the startup zone.
  4. NTP Synchronization: Waits for the NTP client to report a valid time, retrying as needed.
  5. Serving: The web dashboard is live; the selected-zone time is also logged to serial every 10 seconds.

Prerequisites

  • NetBurner device with network connectivity
  • Proper IP configuration (IP address, subnet mask, gateway)
  • Internet access to reach NTP servers
  • A web browser; optionally MTTTY or a similar terminal program to watch the serial log

Key Functions

ShowNtpStatus(), ShowSelectedClock(), ShowWorldClock(), EmitTimeZoneOptions()

CPPCALL data providers that write the dynamic values for each tag straight to the HTTP socket with fdprintf().

SetTzPost() / SyncNowPost()

POST handlers that apply a selected time zone (tzsetchar()) or force an NTP refresh (RefreshNow()), then redirect back to the dashboard.

UserMain(void *pd)

Main entry point: initializes the stack, starts the web server, synchronizes NTP, and logs the selected-zone time to serial.

Technical Details

Dependencies

  • init.h: System initialization
  • http.h / httppost.h: Web server and POST handling
  • fdprintf.h: Writing dynamic values to the HTTP socket
  • time.h: Time manipulation functions
  • ipshow.h: IP address display utilities
  • timezones.h: Time zone database and utilities
  • nbtime.h: NetBurner NTP/time functions

NTP Client

The application uses the NtpClientServlet class for NTP operations:

  • TimeIsValid() reports whether the clock has been set
  • GetSecsSinceUpdate() tracks time since the last NTP update
  • RefreshNow() forces an immediate synchronization

Troubleshooting

  • No Network: Ensure the device has proper network configuration.
  • NTP Timeout / "Awaiting NTP": Check Internet connectivity and firewall settings.
  • Page not updating: The dashboard refreshes every 2 seconds; confirm the device booted and the clock shows "Synced".

Development Notes

  • Remove EnableSystemDiagnostics() for production deployment.
  • The time zone database is extensible through the TZRecords structure.
  • IPv6 support is available when compiled with the IPV6 flag.