ESP32 Fails to Sync Time via NTP
Learn why your ESP32 isn't getting the correct time from NTP servers and how to resolve sync issues for real-time applications.
Common Causes
- π
No Internet or DNS Resolution
If the ESP32 is connected to Wi-Fi but lacks access to the internet or cannot resolve NTP hostnames, time sync will fail silently.
- βοΈ
NTP Configuration Missing or Misordered
Using
configTime()
without setting the proper timezone, or calling it before Wi-Fi connects, can prevent time updates. - π§
Firewall Blocking Port 123
Some networks (especially enterprise or guest networks) block outbound NTP traffic on UDP port 123.
- β±
Insufficient Wait Time for NTP Response
NTP servers may take a few seconds to respond. If your code immediately calls
getLocalTime()
afterconfigTime()
, it may return failure.
Symptoms
Time Not Set or Epoch Value Remains Zero
Calls to getLocalTime()
or time()
return Unix timestamp 0 or show default compile-time values.
`getLocalTime()` Returns False
Indicates that NTP sync has not completed or failed entirely.
No NTP Queries Seen on Router
Packet captures or router logs show no NTP traffic, suggesting a failure to initialize the request.
Works on Some Networks, Fails on Others
NTP works at home but fails on corporate, university, or mobile networks due to DNS or firewall restrictions.
Solutions
Call `configTime()` After Wi-Fi Connects
Ensure that configTime()
is only called after WiFi.status()
confirms connection, and before calling getLocalTime()
.
Add a Loop to Wait for Sync
Implement a retry loop to wait until NTP responds before reading the time.
Use IP-Based NTP Servers
Replace hostnames with direct IP addresses to bypass DNS issues.
Test on Unrestricted Network
Try syncing over a home or mobile hotspot network to rule out firewall or DNS blocks.
More Details
ESP32 Fails to Sync Time via NTP #
Your ESP32 connects to Wi-Fi, but getLocalTime()
returns false or always shows the compile-time timestamp. This issue typically occurs when the ESP32 can't reach an NTP server or hasnβt waited long enough for time sync.
This guide explains how NTP syncing works on the ESP32, why it might fail, and how to fix it reliably.
How NTP Works on ESP32 #
ESP32 boards support automatic NTP (Network Time Protocol) synchronization using the built-in configTime()
function. You call it like this:
configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");
Then use:
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
If getLocalTime()
returns false or always shows a default time, something is preventing the NTP request from succeeding.
Common Causes and Fixes #
π Wi-Fi Not Connected Before Calling configTime()
#
NTP requires an active internet connection. If you call configTime()
too early β before WiFi.begin()
finishes β the ESP32 wonβt be able to send the request.
Fix:
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
configTime(0, 0, "pool.ntp.org");
βοΈ NTP Not Ready When Time is Requested #
NTP sync happens asynchronously. If you call getLocalTime() immediately after configTime(), the result may be empty.
Fix: Add a waiting loop:
struct tm timeinfo;
int retries = 0;
while (!getLocalTime(&timeinfo) && retries++ < 10) {
Serial.println("Waiting for NTP time...");
delay(1000);
}
This gives the ESP32 time to retrieve the date and time over UDP.
π‘ Firewall or Network Blocking NTP #
Some guest networks, VPNs, or corporate firewalls block UDP traffic on port 123, which is used by NTP.
Fix:
Test on a different network (like a mobile hotspot)
Use IP addresses instead of DNS:
configTime(0, 0, "129.6.15.28"); // time.nist.gov
β DNS Fails, No IP Resolution #
If the ESP32 cannot resolve pool.ntp.org
due to DNS issues (especially in custom firmware or mesh networks), NTP will silently fail.
Fix: Use numeric IP addresses for NTP servers to bypass DNS:
configTime(0, 0, "216.239.35.0"); // Google public time server
Additional Debugging Tips #
Print the result of
WiFi.localIP()
andWiFi.status()
before attempting time sync.Use
Serial.println(time(nullptr));
to get the raw Unix timestamp and confirm if time is ever set.Ensure you are not using deep sleep or fast boot modes that skip time sync unintentionally.
Example: Robust NTP Sync Code #
void setupTime() {
configTime(0, 0, "pool.ntp.org");
struct tm timeinfo;
for (int i = 0; i < 10; i++) {
if (getLocalTime(&timeinfo)) {
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
return;
}
Serial.println("Waiting for NTP sync...");
delay(1000);
}
Serial.println("Failed to sync time.");
}
Call setupTime()
after Wi-Fi connects to ensure reliable behavior.
Conclusion #
ESP32 time sync failures are usually caused by:
Trying to sync before Wi-Fi is connected
Not waiting long enough for NTP to reply
Network blocking NTP or DNS requests
With proper sequencing and fallback mechanisms, you can ensure your ESP32 always gets the correct time β essential for logging, secure connections, and IoT automation.

Quick Navigation
Additional Resources
Still Stuck with an ESP32 Issue? Let's solve it together.
Our interactive troubleshooting wizard will guide you through common ESP32 problems and their solutions, step by step.