Espressif Systems ESP8266 Beginner's Manual page 45

Hide thumbs Also See for ESP8266:
Table of Contents

Advertisement

To request the time from the NTP server, you have to send a certain sequence of 48 bytes. We don't need any fancy features, so just
set the first byte to request the time, and leave all other 47 bytes zero.
To actually send the packet, you have to start the packet, specifying the IP address of the server, and the NTP port number, port 123.
Then just write the buffer to the packet, and send it with endPacket.
The last three functions are just some simple math to convert seconds to hours, minutes and seconds.
Using the example
Enter your Wi-Fi credentials on lines 79-81, and hit upload. If you have a working Internet connection, you should get an output that
looks like this:
Connecting
.........
Connected to Wi-Fi SSID
IP address:
192.168.1.2
Starting UDP
Local port:
123
Time server IP: 216.229.0.179
Sending NTP request ...
NTP response:
1488378061
UTC time:
14:21:53
Sending NTP request ...
NTP response:
1488378114
UTC time:
14:22:53
Sending NTP request ...
NTP response:
1488378174
UTC time:
14:23:53
Sending NTP request ...
NTP response:
1488378234
UTC time:
14:24:53
Sending NTP request ...
NTP response:
1488378294
UTC time:
14:25:53
...
You should see the time update every second, and
If you don't have an Internet connection, the DNS lookup of the time server will fail:
Connecting
.........
Connected to Wi-Fi SSID
IP address:
192.168.1.2
Starting UDP
Local port:
123
DNS lookup failed. Rebooting.
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
If your connection is not reliable, or if there's heavy traffic, you might encounter some dropped packets:
Sending NTP request ...
NTP response:
1488378780
UTC time:
14:33:54
Sending NTP request ...
UTC time:
14:34:54
Sending NTP request ...
NTP response:
1488378895
UTC time:
14:35:0
As you can see, the ESP never received a response to the second NTP request. That's not really an issue, as long as at least some
packets make it through.
Local time and daylight savings
An NTP server returns the UTC time. If you want local time, you have to compensate for your time zone and daylight savings. For
example, if you want CET (Central European Time), you have to add 3600 to the UNIX time during winter, (3600 s = 1 h), and 7200
during summer (DST).
should show up every minute.
Sending NTP request ...

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ESP8266 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents