ESP8266 NTP Internet Clock: Let the NodeMCU Learn the Time Online, No Battery or Module
The NodeMCU V3 joins your home Wi-Fi, learns the time from an NTP server on the internet and prints it to the Serial port once a second: 14:05:09. No battery, clock module or any other part is needed; the board and a USB cable are enough. Once the board knows the time it keeps counting by itself and corrects it from the server now and then. The code comes set to the time zone of Türkiye (UTC+3); you change one number for another country. The ESP8266 only joins 2.4 GHz networks.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// ESP8266 NTP Internet Clock: Let the NodeMCU Learn the Time Online, No Battery or Module
4// ─────────────────────────────────────────
5// The NodeMCU joins your home Wi-Fi, learns the time from an NTP server on
6// the internet and prints it to the Serial port once a second. No battery
7// or clock module is needed; the board keeps counting by itself and
8// corrects the time from the server now and then.
9//
10// Pins:
11// Micro USB -> computer (power + Serial port)
12
13#include <ESP8266WiFi.h>
14#include <time.h>
15
16// ============ WI-FI NETWORK ============
17// Write the name and password of your own network inside the quotes. The ESP8266 only joins 2.4 GHz
18// networks.
19const char *const WIFI_SSID = "YOUR_WIFI_NAME";
20const char *const WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
21const unsigned long WIFI_HINT_MS = 20000; // Time until the hint (ms)
22
23// ============ CLOCK ============
24// The time zone in seconds: Turkiye is UTC+3 with no daylight saving. Change the 3 for another
25// country.
26const long UTC_OFFSET_S = 3 * 3600;
27const char *const NTP_SERVER = "pool.ntp.org";
28const unsigned long PRINT_MS = 1000; // ms
29// Until the board learns the time, time returns a small number; past this date (2020) the clock is
30// set.
31const time_t CLOCK_SET_AFTER = 1600000000;
32
33bool clockSet = false;
34unsigned long lastPrint = 0;
35bool wifiUp = false;
36bool hintDone = false;
37
38// Prints the time once a second. It does not wait: it looks at the time passed since the last
39// print.
40void showTime() {
41 if (millis() - lastPrint < PRINT_MS) {
42 return;
43 }
44 lastPrint = millis();
45 time_t now = time(nullptr);
46 if (now < CLOCK_SET_AFTER) {
47 return;
48 }
49 struct tm *t = localtime(&now);
50 char text[12];
51 if (!clockSet) {
52 clockSet = true;
53 strftime(text, sizeof(text), "%d.%m.%Y", t);
54 Serial.println("Time received");
55 Serial.print("Date: ");
56 Serial.println(text);
57 }
58 strftime(text, sizeof(text), "%H:%M:%S", t);
59 Serial.print("Time: ");
60 Serial.println(text);
61}
62
63// Watches the connection: reports when it is made and when it drops. The ESP8266 rejoins a lost
64// network by itself.
65void checkWifi() {
66 bool up = WiFi.status() == WL_CONNECTED;
67 if (up && !wifiUp) {
68 hintDone = true;
69 Serial.println("Wi-Fi connected");
70 } else if (!up && wifiUp) {
71 Serial.println("Wi-Fi lost. Reconnecting");
72 }
73 wifiUp = up;
74
75 if (!up && !hintDone && millis() > WIFI_HINT_MS) {
76 hintDone = true;
77 Serial.println("Could not join the Wi-Fi");
78 Serial.println("Check the network name and password");
79 Serial.println("The ESP8266 only sees 2.4 GHz networks");
80 }
81}
82
83void setup() {
84 Serial.begin(115200);
85 Serial.println();
86 Serial.println("NTP internet clock");
87 if (strncmp(WIFI_SSID, "YOUR_", 5) == 0) {
88 Serial.println("Put your Wi-Fi name and password in the code");
89 }
90 Serial.println("Connecting to Wi-Fi");
91 // The board opens no network of its own, it joins yours
92 WiFi.mode(WIFI_STA);
93 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
94 // Tells the board the time zone and the server. Once on Wi-Fi the board asks for the time by
95 // itself and refreshes it now and then.
96 configTime(UTC_OFFSET_S, 0, NTP_SERVER);
97}
98
99// loop never waits: a long delay would freeze the page and can drop the Wi-Fi.
100void loop() {
101 checkWifi();
102 showTime();
103}
104FAQ
What is NTP?
NTP is the way to ask time servers on the internet what time it is. The board sends a small question to the server and the answer carries the current time. Servers always give the time as UTC, that is without a time zone; the board adds the difference. pool.ntp.org is a pool of volunteer servers all over the world and points you to a nearby one.
Does the board forget the time when the internet drops?
No. Once the board has learned the time it keeps counting with its own counter; the time lines keep coming even if the Wi-Fi drops. While connected it asks again about once an hour and corrects the drift. If the board loses power the time is gone, because there is no battery: the board asks for it again when it starts.
How do I show the time of another country?
UTC_OFFSET_S is that country's difference from UTC, in seconds. For Türkiye it says 3 * 3600; for Germany you write 1 * 3600 in winter, for Japan 9 * 3600. For countries to the west the number is negative. In a country with daylight saving you set the second value in configTime to 3600 in summer; for Türkiye it is always 0.
Why does the time not come at once?
The board joins the Wi-Fi first and asks the server after that; the answer usually arrives within a second or two. Until then time returns a very small number, because the board starts counting from the year 1970. The code waits for the number to be past 2020; without that wait you would see a wrong date like 01.01.1970 in the first lines.
