forked from arduino-libraries/NTPClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of using with Arduino Ethernet Shield (arduino-libraries#111
- Loading branch information
Łukasz Gierałtowski
authored and
Łukasz Gierałtowski
committed
Jan 27, 2023
1 parent
62fafd8
commit f12b54b
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <EthernetUdp.h> | ||
#include <NTPClient.h> | ||
|
||
#define ethernetShieldPin 10 // Most Arduino shields | ||
// #define ethernetShieldPin 5 // MKR ETH shield | ||
// #define ethernetShieldPin 0 // Teensy 2.0 | ||
// #define ethernetShieldPin 20 // Teensy++ 2.0 | ||
// #define ethernetShieldPin 15 // ESP8266 with Adafruit Featherwing Ethernet | ||
// #define ethernetShieldPin 33 // ESP32 with Adafruit Featherwing Ethernet | ||
|
||
byte localMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | ||
unsigned int localUdpPort = 8888; | ||
EthernetUDP udp; | ||
|
||
NTPClient timeClient(udp); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
Ethernet.init(ethernetShieldPin); | ||
Serial.println("Initialize Ethernet with DHCP"); | ||
if (Ethernet.begin(localMac) == 0) { | ||
Serial.println("Failed to configure Ethernet using DHCP"); | ||
if (Ethernet.hardwareStatus() == EthernetNoHardware) { | ||
Serial.println("Ethernet shield was not found."); | ||
} | ||
if (Ethernet.linkStatus() == LinkOFF) { | ||
Serial.println("Ethernet cable is not connected."); | ||
} | ||
while (true) { | ||
delay(1); | ||
} | ||
} | ||
udp.begin(localUdpPort); | ||
} | ||
|
||
void loop() { | ||
if(timeClient.update()) { | ||
Serial.print("[NTP] Updated current time: "); | ||
Serial.println(timeClient.getFormattedTime()); | ||
} | ||
} |