Skip to content

Commit 0b3604e

Browse files
Add example of using with Arduino Ethernet Shield (#111)
1 parent 62fafd8 commit 0b3604e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <EthernetUdp.h>
2+
#include <NTPClient.h>
3+
4+
#define ethernetShieldPin 10 // Most Arduino shields
5+
// #define ethernetShieldPin 5 // MKR ETH shield
6+
// #define ethernetShieldPin 0 // Teensy 2.0
7+
// #define ethernetShieldPin 20 // Teensy++ 2.0
8+
// #define ethernetShieldPin 15 // ESP8266 with Adafruit Featherwing Ethernet
9+
// #define ethernetShieldPin 33 // ESP32 with Adafruit Featherwing Ethernet
10+
11+
byte localMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
12+
unsigned int localUdpPort = 8888;
13+
EthernetUDP udp;
14+
15+
NTPClient timeClient(udp);
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
while (!Serial) {
20+
; // wait for serial port to connect. Needed for native USB port only
21+
}
22+
23+
Ethernet.init(ethernetShieldPin);
24+
Serial.println("Initialize Ethernet with DHCP");
25+
if (Ethernet.begin(localMac) == 0) {
26+
Serial.println("Failed to configure Ethernet using DHCP");
27+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
28+
Serial.println("Ethernet shield was not found.");
29+
}
30+
if (Ethernet.linkStatus() == LinkOFF) {
31+
Serial.println("Ethernet cable is not connected.");
32+
}
33+
while (true) {
34+
delay(1);
35+
}
36+
}
37+
udp.begin(localUdpPort);
38+
}
39+
40+
void loop() {
41+
if(timeClient.update()) {
42+
Serial.print("[NTP] Updated current time: ");
43+
Serial.println(timeClient.getFormattedTime());
44+
}
45+
}

0 commit comments

Comments
 (0)