1
+ /*
2
+
3
+ UDP NTP Client
4
+
5
+ Get the time from a Network Time Protocol (NTP) time server
6
+ Demonstrates use of UDP sendPacket and ReceivePacket
7
+ For more on NTP time servers and the messages needed to communicate with them,
8
+ see http://en.wikipedia.org/wiki/Network_Time_Protocol
9
+
10
+ created 4 Sep 2010
11
+ by Michael Margolis
12
+ modified 9 Apr 2012
13
+ by Tom Igoe
14
+
15
+ This code is in the public domain.
16
+
17
+ modified 2025
18
+ by Infineon Technologies AG
19
+ */
20
+
21
+ #include < WiFi.h>
22
+ #include < WiFiUdp.h>
23
+ #include " secrets.h"
24
+
25
+ int status = WL_IDLE_STATUS;
26
+ char ssid[] = NET_SECRET_SSID; // your network SSID (name)
27
+ char pass[] = NET_SECRET_PASSWORD; // your network password
28
+ int keyIndex = 0 ; // your network key Index number (needed only for WEP)
29
+
30
+ unsigned int localPort = 2390 ; // local port to listen for UDP packets
31
+
32
+ IPAddress timeServer (129 , 6 , 15 , 28 ); // time.nist.gov NTP server
33
+
34
+ const int NTP_PACKET_SIZE = 48 ; // NTP time stamp is in the first 48 bytes of the message
35
+
36
+ byte packetBuffer[ NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
37
+
38
+ // A UDP instance to let us send and receive packets over UDP
39
+ WiFiUDP Udp;
40
+
41
+ void setup () {
42
+ // Open serial communications and wait for port to open:
43
+ Serial.begin (9600 );
44
+ while (!Serial) {
45
+ ; // wait for serial port to connect. Needed for native USB port only
46
+ }
47
+
48
+ // attempt to connect to WiFi network:
49
+ while (status != WL_CONNECTED) {
50
+ Serial.print (" Attempting to connect to SSID: " );
51
+ Serial.println (ssid);
52
+ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
53
+ status = WiFi.begin (ssid, pass);
54
+
55
+ // wait 10 seconds for connection:
56
+ delay (10000 );
57
+ }
58
+
59
+ Serial.println (" Connected to WiFi" );
60
+ printWifiStatus ();
61
+
62
+ Serial.println (" \n Starting connection to server..." );
63
+ Udp.begin (localPort);
64
+ }
65
+
66
+ void loop () {
67
+ sendNTPpacket (timeServer); // send an NTP packet to a time server
68
+ // wait to see if a reply is available
69
+ delay (1000 );
70
+ if (Udp.parsePacket ()) {
71
+ Serial.println (" packet received" );
72
+ // We've received a packet, read the data from it
73
+ Udp.read (packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
74
+
75
+ // the timestamp starts at byte 40 of the received packet and is four bytes,
76
+ // or two words, long. First, extract the two words:
77
+
78
+ unsigned long highWord = word (packetBuffer[40 ], packetBuffer[41 ]);
79
+ unsigned long lowWord = word (packetBuffer[42 ], packetBuffer[43 ]);
80
+ // combine the four bytes (two words) into a long integer
81
+ // this is NTP time (seconds since Jan 1 1900):
82
+ unsigned long secsSince1900 = highWord << 16 | lowWord;
83
+ Serial.print (" Seconds since Jan 1 1900 = " );
84
+ Serial.println (secsSince1900);
85
+
86
+ // now convert NTP time into everyday time:
87
+ Serial.print (" Unix time = " );
88
+ // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
89
+ const unsigned long seventyYears = 2208988800UL ;
90
+ // subtract seventy years:
91
+ unsigned long epoch = secsSince1900 - seventyYears;
92
+ // print Unix time:
93
+ Serial.println (epoch);
94
+
95
+
96
+ // print the hour, minute and second:
97
+ Serial.print (" The UTC time is " ); // UTC is the time at Greenwich Meridian (GMT)
98
+ Serial.print ((epoch % 86400L ) / 3600 ); // print the hour (86400 equals secs per day)
99
+ Serial.print (' :' );
100
+ if (((epoch % 3600 ) / 60 ) < 10 ) {
101
+ // In the first 10 minutes of each hour, we'll want a leading '0'
102
+ Serial.print (' 0' );
103
+ }
104
+ Serial.print ((epoch % 3600 ) / 60 ); // print the minute (3600 equals secs per minute)
105
+ Serial.print (' :' );
106
+ if ((epoch % 60 ) < 10 ) {
107
+ // In the first 10 seconds of each minute, we'll want a leading '0'
108
+ Serial.print (' 0' );
109
+ }
110
+ Serial.println (epoch % 60 ); // print the second
111
+ }
112
+ // wait ten seconds before asking for the time again
113
+ delay (10000 );
114
+ }
115
+
116
+ // send an NTP request to the time server at the given address
117
+ void sendNTPpacket (IPAddress& address) {
118
+
119
+ // set all bytes in the buffer to 0
120
+ memset (packetBuffer, 0 , NTP_PACKET_SIZE);
121
+ // Initialize values needed to form NTP request
122
+ // (see URL above for details on the packets)
123
+
124
+ packetBuffer[0 ] = 0b11100011 ; // LI, Version, Mode
125
+ packetBuffer[1 ] = 0 ; // Stratum, or type of clock
126
+ packetBuffer[2 ] = 6 ; // Polling Interval
127
+ packetBuffer[3 ] = 0xEC ; // Peer Clock Precision
128
+ // 8 bytes of zero for Root Delay & Root Dispersion
129
+ packetBuffer[12 ] = 49 ;
130
+ packetBuffer[13 ] = 0x4E ;
131
+ packetBuffer[14 ] = 49 ;
132
+ packetBuffer[15 ] = 52 ;
133
+ // all NTP fields have been given values, now
134
+ // you can send a packet requesting a timestamp:
135
+ Udp.beginPacket (address, 123 ); // NTP requests are to port 123
136
+
137
+ Udp.write (packetBuffer, NTP_PACKET_SIZE);
138
+
139
+ Udp.endPacket ();
140
+ }
141
+
142
+
143
+ void printWifiStatus () {
144
+ // print the SSID of the network you're attached to:
145
+ Serial.print (" SSID: " );
146
+ Serial.println (WiFi.SSID ());
147
+
148
+ // print your WiFi local IP address:
149
+ IPAddress ip = WiFi.localIP ();
150
+ Serial.print (" IP Address: " );
151
+ Serial.println (ip);
152
+
153
+ // print the received signal strength:
154
+ long rssi = WiFi.RSSI ();
155
+ Serial.print (" signal strength (RSSI):" );
156
+ Serial.print (rssi);
157
+ Serial.println (" dBm" );
158
+ }
0 commit comments