This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Ethernet_NTPClient_Advanced.ino
315 lines (252 loc) · 11.4 KB
/
Ethernet_NTPClient_Advanced.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/****************************************************************************************************************************
Ethernet_NTPClient_Advanced.ino
For AVR, ESP8266/ESP32, SAMD21/SAMD51, nRF52, STM32, SAM DUE, WT32_ETH01, RTL8720DN boards using
a) Ethernet W5x00, ENC28J60, LAN8742A
b) WiFiNINA
c) ESP8266/ESP32 WiFi
d) ESP8266/ESP32-AT-command WiFi
e) WT32_ETH01 (ESP32 + LAN8720)
f) RTL8720DN
Based on and modified from Arduino NTPClient Library (https://github.com/arduino-libraries/NTPClient)
to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, SAM DUE, RTL8720DN, etc.
and Ethernet/WiFi/WiFiNINA shields
Copyright (C) 2015 by Fabrice Weinberg and licensed under MIT License (MIT)
Built by Khoi Hoang https://github.com/khoih-prog/NTPClient_Generic
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <NTPClient_Generic.h> // https://github.com/khoih-prog/NTPClient_Generic
// A UDP instance to let us send and receive packets over UDP
EthernetUDP ntpUDP;
// NTP server
//World
//char timeServer[] = "time.nist.gov";
// Canada
char timeServer[] = "0.ca.pool.ntp.org";
//char timeServer[] = "1.ca.pool.ntp.org";
//char timeServer[] = "2.ca.pool.ntp.org";
//char timeServer[] = "3.ca.pool.ntp.org";
// Europe
//char timeServer[] = ""europe.pool.ntp.org";
#define TIME_ZONE_OFFSET_HRS (-4)
#define NTP_UPDATE_INTERVAL_MS 60000L
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, timeServer, (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);
void initEthernet()
{
#if USE_ETHERNET_PORTENTA_H7
ET_LOGWARN(F("======== USE_PORTENTA_H7_ETHERNET ========"));
#elif USE_NATIVE_ETHERNET
ET_LOGWARN(F("======== USE_NATIVE_ETHERNET ========"));
#elif USE_ETHERNET_GENERIC
ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ==========="));
#elif USE_ETHERNET_ESP8266
ET_LOGWARN(F("=========== USE_ETHERNET_ESP8266 ==========="));
#elif USE_ETHERNET_ENC
ET_LOGWARN(F("=========== USE_ETHERNET_ENC ==========="));
#else
ET_LOGWARN(F("========================="));
#endif
#if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7)
#if (USING_SPI2)
#if defined(CUR_PIN_MISO)
ET_LOGWARN(F("Default SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI);
ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO);
ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK);
ET_LOGWARN1(F("SS:"), CUR_PIN_SS);
ET_LOGWARN(F("========================="));
#endif
#else
ET_LOGWARN(F("Default SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
ET_LOGWARN(F("========================="));
#endif
#if defined(ESP8266)
// For ESP8266, change for other boards if necessary
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN D2 // For ESP8266
#endif
ET_LOGWARN1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN);
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// For ESP8266
// Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8
// EthernetGeneric X X X X X 0
// Ethernet_ESP8266 0 0 0 0 0 0
// D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs
// Must use library patch for Ethernet, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
#elif defined(ESP32)
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 5 //22 // For ESP32
#endif
ET_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, EthernetLarge libraries
// ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
// ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
#elif ETHERNET_USE_RPIPICO
pinMode(USE_THIS_SS_PIN, OUTPUT);
digitalWrite(USE_THIS_SS_PIN, HIGH);
// ETHERNET_USE_RPIPICO, use default SS = 5 or 17
#ifndef USE_THIS_SS_PIN
#if defined(ARDUINO_ARCH_MBED)
#define USE_THIS_SS_PIN 5 // For Arduino Mbed core
#else
#define USE_THIS_SS_PIN 17 // For E.Philhower core
#endif
#endif
ET_LOGWARN1(F("RPIPICO setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, EthernetLarge libraries
// For RPI Pico using Arduino Mbed RP2040 core
// SCK: GPIO2, MOSI: GPIO3, MISO: GPIO4, SS/CS: GPIO5
// For RPI Pico using E. Philhower RP2040 core
// SCK: GPIO18, MOSI: GPIO19, MISO: GPIO16, SS/CS: GPIO17
// Default pin 5/17 to SS/CS
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
#else //defined(ESP8266)
// unknown board, do nothing, use default SS = 10
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 10 // For other boards
#endif
#if defined(BOARD_NAME)
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
#else
ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN);
#endif
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET )
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
#endif // defined(ESP8266)
#endif // #if !(USE_NATIVE_ETHERNET)
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
#if !(USE_NATIVE_ETHERNET || USE_ETHERNET_PORTENTA_H7)
ET_LOGWARN(F("========================="));
#if defined( ESP32 )
// Just info to know how to connect correctly
// To change for other SPI
ET_LOGWARN(F("Currently Used SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), PIN_MOSI);
ET_LOGWARN1(F("MISO:"), PIN_MISO);
ET_LOGWARN1(F("SCK:"), PIN_SCK);
ET_LOGWARN1(F("SS:"), PIN_SS);
#else
#if defined(CUR_PIN_MISO)
ET_LOGWARN(F("Currently Used SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI);
ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO);
ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK);
ET_LOGWARN1(F("SS:"), CUR_PIN_SS);
#else
ET_LOGWARN(F("Currently Used SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
#endif
#endif
ET_LOGWARN(F("========================="));
#elif (USE_ETHERNET_PORTENTA_H7)
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("No Ethernet found. Stay here forever");
while (true)
{
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Not connected Ethernet cable");
}
#endif
Serial.print(F("Using mac index = "));
Serial.println(index);
Serial.print(F("Connected! IP address: "));
Serial.println(Ethernet.localIP());
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStarting Ethernet_NTPClient_Advanced on "); Serial.print(BOARD_NAME);
Serial.print(" with "); Serial.println(SHIELD_TYPE);
Serial.println(ETHERNET_WEBSERVER_VERSION);
Serial.println(NTPCLIENT_GENERIC_VERSION);
initEthernet();
timeClient.begin();
Serial.println("Using NTP Server " + timeClient.getPoolServerName());
}
void loop()
{
timeClient.update();
if (timeClient.updated())
Serial.println("********UPDATED********");
else
Serial.println("******NOT UPDATED******");
Serial.println("UTC : " + timeClient.getFormattedUTCTime());
Serial.println("UTC : " + timeClient.getFormattedUTCDateTime());
Serial.println("LOC : " + timeClient.getFormattedTime());
Serial.println("LOC : " + timeClient.getFormattedDateTime());
Serial.println("UTC EPOCH : " + String(timeClient.getUTCEpochTime()));
Serial.println("LOC EPOCH : " + String(timeClient.getEpochTime()));
// Function test
// Without leading 0
Serial.println(String("UTC : ") + timeClient.getUTCHours() + ":" + timeClient.getUTCMinutes() + ":" + timeClient.getUTCSeconds() + " " +
timeClient.getUTCDoW() + " " + timeClient.getUTCDay() + "/" + timeClient.getUTCMonth() + "/" + timeClient.getUTCYear() + " or " +
timeClient.getUTCDay() + " " + timeClient.getUTCMonthStr() + " " + timeClient.getUTCYear());
// With leading 0
Serial.println(String("UTC : ") + timeClient.getUTCStrHours() + ":" + timeClient.getUTCStrMinutes() + ":" + timeClient.getUTCStrSeconds() + " " +
timeClient.getUTCDoW() + " " + timeClient.getUTCDay() + "/" + timeClient.getUTCMonth() + "/" + timeClient.getUTCYear() + " or " +
timeClient.getUTCDay() + " " + timeClient.getUTCMonthStr() + " " + timeClient.getUTCYear());
// Without leading 0
Serial.println(String("LOC : ") + timeClient.getHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds() + " " +
timeClient.getDoW() + " " + timeClient.getDay() + "/" + timeClient.getMonth() + "/" + timeClient.getYear() + " or " +
timeClient.getDay() + " " + timeClient.getMonthStr() + " " + timeClient.getYear());
// With leading 0
Serial.println(String("LOC : ") + timeClient.getStrHours() + ":" + timeClient.getStrMinutes() + ":" + timeClient.getStrSeconds() + " " +
timeClient.getDoW() + " " + timeClient.getDay() + "/" + timeClient.getMonth() + "/" + timeClient.getYear() + " or " +
timeClient.getDay() + " " + timeClient.getMonthStr() + " " + timeClient.getYear());
delay(10000);
}