Skip to content

Commit aea4426

Browse files
added NTP time example
1 parent bcc6394 commit aea4426

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* TimeNTP_ESP32WiFi.ino
3+
* Example showing time sync to NTP time source
4+
*
5+
* This sketch uses the ESP32WiFi library
6+
* This sketch uses the Time library https://github.com/PaulStoffregen/Time
7+
*/
8+
9+
#include <TimeLib.h>
10+
#include <WiFi.h>
11+
#include <WiFiUdp.h>
12+
13+
const char ssid[] = "ssid"; // your network SSID (name)
14+
const char pass[] = "password"; // your network password
15+
16+
// NTP Servers:
17+
static const char ntpServerName[] = "pool.ntp.org";
18+
19+
const int timeZone = 0; // UTC
20+
21+
WiFiUDP Udp;
22+
unsigned int localPort = 8888; // local port to listen for UDP packets
23+
24+
time_t getNtpTime();
25+
void digitalClockDisplay();
26+
void printDigits(int digits);
27+
void sendNTPpacket(IPAddress &address);
28+
29+
void setup()
30+
{
31+
Serial.begin(115200);
32+
while (!Serial) ; // Needed for Leonardo only
33+
delay(250);
34+
Serial.println("TimeNTP Example");
35+
Serial.print("Connecting to ");
36+
Serial.println(ssid);
37+
WiFi.disconnect();
38+
WiFi.mode(WIFI_MODE_STA);
39+
WiFi.begin(ssid, pass);
40+
41+
while (WiFi.status() != WL_CONNECTED) {
42+
delay(500);
43+
Serial.print(".");
44+
}
45+
46+
Serial.print("IP number assigned by DHCP is ");
47+
Serial.println(WiFi.localIP());
48+
Serial.println("Starting UDP");
49+
Udp.begin(localPort);
50+
Serial.print("Local port: ");
51+
// Serial.println(Udp.localPort());
52+
Serial.println("waiting for sync");
53+
setSyncProvider(getNtpTime);
54+
setSyncInterval(300);
55+
}
56+
57+
time_t prevDisplay = 0; // when the digital clock was displayed
58+
59+
void loop()
60+
{
61+
if (timeStatus() != timeNotSet) {
62+
if (now() != prevDisplay) { //update the display only if time has changed
63+
prevDisplay = now();
64+
digitalClockDisplay();
65+
}
66+
}
67+
}
68+
69+
void digitalClockDisplay()
70+
{
71+
// digital clock display of the time
72+
Serial.print(hour());
73+
printDigits(minute());
74+
printDigits(second());
75+
Serial.print(" ");
76+
Serial.print(day());
77+
Serial.print(".");
78+
Serial.print(month());
79+
Serial.print(".");
80+
Serial.print(year());
81+
Serial.println();
82+
}
83+
84+
void printDigits(int digits)
85+
{
86+
// utility for digital clock display: prints preceding colon and leading 0
87+
Serial.print(":");
88+
if (digits < 10)
89+
Serial.print('0');
90+
Serial.print(digits);
91+
}
92+
93+
/*-------- NTP code ----------*/
94+
95+
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
96+
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
97+
98+
time_t getNtpTime()
99+
{
100+
IPAddress ntpServerIP; // NTP server's ip address
101+
102+
while (Udp.parsePacket() > 0) ; // discard any previously received packets
103+
Serial.println("Transmit NTP Request");
104+
// get a random server from the pool
105+
WiFi.hostByName(ntpServerName, ntpServerIP);
106+
Serial.print(ntpServerName);
107+
Serial.print(": ");
108+
Serial.println(ntpServerIP);
109+
sendNTPpacket(ntpServerIP);
110+
uint32_t beginWait = millis();
111+
while (millis() - beginWait < 1500) {
112+
int size = Udp.parsePacket();
113+
if (size >= NTP_PACKET_SIZE) {
114+
Serial.println("Receive NTP Response");
115+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
116+
unsigned long secsSince1900;
117+
// convert four bytes starting at location 40 to a long integer
118+
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
119+
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
120+
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
121+
secsSince1900 |= (unsigned long)packetBuffer[43];
122+
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
123+
}
124+
}
125+
Serial.println("No NTP Response :-(");
126+
return 0; // return 0 if unable to get the time
127+
}
128+
129+
// send an NTP request to the time server at the given address
130+
void sendNTPpacket(IPAddress &address)
131+
{
132+
// set all bytes in the buffer to 0
133+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
134+
// Initialize values needed to form NTP request
135+
// (see URL above for details on the packets)
136+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
137+
packetBuffer[1] = 0; // Stratum, or type of clock
138+
packetBuffer[2] = 6; // Polling Interval
139+
packetBuffer[3] = 0xEC; // Peer Clock Precision
140+
// 8 bytes of zero for Root Delay & Root Dispersion
141+
packetBuffer[12] = 49;
142+
packetBuffer[13] = 0x4E;
143+
packetBuffer[14] = 49;
144+
packetBuffer[15] = 52;
145+
// all NTP fields have been given values, now
146+
// you can send a packet requesting a timestamp:
147+
Udp.beginPacket(address, 123); //NTP requests are to port 123
148+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
149+
Udp.endPacket();
150+
}

0 commit comments

Comments
 (0)