Retrieve date and time from an NTP server!

What is an NTP server?

NTP stands for Network Time Protocol and is a standard Internet protocol for time synchronization between computer systems. The protocol enables all networked devices to synchronize with Coordinated Universal Time (UTC) within a few milliseconds (50 milliseconds over the public Internet and less than 5 milliseconds in a LAN environment). Coordinated Universal Time (UTC) is a global time standard that is closely linked to Greenwich Mean Time (GMT). UTC does not vary and is the same worldwide, so that clients can synchronize with servers regardless of location
In Central Europe we have Central European Time (CET) and it is UTC +1 hour. The time zone or summer time changeover is adopted by the client.

NTP server architecture

The NTP server is based on a hierarchical structure consisting of four levels, each level being referred to as a "stratum". The first layer (layer 0) contains high-precision atomic/radio clocks that provide the exact time. The second layer (layer 1) is directly connected to the first layer and therefore contains the most accurate time available, which we get from the first layer. The second and third layers are connected to the layer above them and synchronize with each other.

Retrieve NTP server data

The most common configuration for NTP is operation in client-server mode. A client device such as the ESP8266 connects to an NTP server via the User Datagram Protocol (UDP) at Port 123, which is best placed as close as possible to the client.
The client then sends a request packet to an NTP server. In response to this request, the NTP server sends a timestamp packet. A timestamp packet can contain several pieces of information such as UNIX timestamp, accuracy and delay.
A client can then read out the current date and time values.
The time stamp sent by the NTP server corresponds to Coordinated Universal Time (UTC) with an accuracy of a few milliseconds. The client can then add any local time zone offset or daylight saving time offset to this received UTC time.

Host Name: https://www.ntppool.org/de/

wordwide  asia
Asien  –  asia.pool.ntp.org
Europe  –  europe.pool.ntp.org
North America  –  north-america.pool.ntp.org
Germany  –  de.pool.ntp.org
Switzerland  –  ch.pool.ntp.org
Austria –   at.pool.ntp.org

POSIX - Time zone setting

POSIX (Portable Operating System Interface) is a family of standards that also includes the format of the time zone setting. This string contains all the important information about the time zone and the handling of the summer/winter time changeover.

CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00 - Central Europe time zone

CET: Central European Time

-1 CEST -2: Im Winter -1 Stunde und im Sommer -2 Stunden zur UTC Zeit

M3.5.0/02:00:00: In month 3, in week 5 on day 0 (Sunday) at 2:00:00, the time is changed from winter time to summer time.

M10.5.0/03:00:00: The changeover from summer time to winter time takes place in October in week 5 on Sunday at 3:00:00 am.

Commands to format the time

We have several commands available to display the time in a form that is easy for us to read. Interestingly, there are a few curiosities, such as the year. To display the year correctly, 1900 must be added. The month also takes some getting used to, as it starts at 0 and not 1. What is common, but we should still know, is that the day of the week starts on Sunday and not Monday as we usually do.

Sketch

				
					/*
Project:  NTC Zeit Server for ESP32 and ESP8266
Author:   Thomas Edlinger for www.edistechlab.com
Date:     Created 04.06.2023
Version:  V1.0
IDE:      Arduino IDE 2.1.0
 
Required Board (Tools -> Board -> Boards Manager...)
 - Board: esp8266 by ESP8266 Community   V3.1.2 
 - Board: esp32   by Espressif Systems   V2.0.9

Required libraries (sketch -> include library -> manage libraries)
 - 
*/

#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <time.h>

// Wi-Fi Settings
const char *wifi_ssid = "Deine_SSID";
const char *wifi_password = "Dein-WiFi-Passwort";
const char* NTP_SERVER = "ch.pool.ntp.org";
const char* TZ_INFO    = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00";

char wochentage[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
String monat[12] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};

time_t now;
tm tm;

void setup () {
  Serial.begin(115200);
  setup_wifi();
  configTime(0, 0, NTP_SERVER);
  setenv("TZ", TZ_INFO, 1);
}

void loop () {
  time(&now);                       // Liest die aktuelle Zeit
  localtime_r(&now, &tm);           // Beschreibt tm mit der aktuelle Zeit
  
  Serial.printf("%02d-%02d-%04d \t", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
  //Serial.printf("%02d %s %04d \t", tm.tm_mday, monat[tm.tm_mon], tm.tm_year + 1900);  // Monat ausgeschrieben
  
  Serial.print(wochentage[tm.tm_wday]);
  //Serial.print(tm.tm_wday);         // Wochentag als Zahl, 0 = Sonntag
  Serial.print("\tder ");
  Serial.print(tm.tm_mday);
  Serial.print(" " + monat[tm.tm_mon] + " ");
  Serial.print(tm.tm_year + 1900);

  Serial.printf(" \tUhrzeit: %02d:%02d:%02d \n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  delay(1000);
}

void setup_wifi() {
  delay(10);
  WiFi.begin(wifi_ssid, wifi_password);
  int counter = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (++counter > 100) ESP.restart();
    Serial.print(".");
  }
  Serial.println("WiFi connected");
}
				
			

Material

Watch the video

★☆★★ If you would like to support the channel via ★☆★★

or via

Twint Spenden Code

Letzte Aktualisierung am 2024-12-12 / Affiliate Links / Bilder von der Amazon Product Advertising API