Skip to content

Commit 6162899

Browse files
committed
1 parent 978081e commit 6162899

File tree

2 files changed

+175
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)