This repository was 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
/
Copy pathNINA_NTPClient_Basic.ino
101 lines (78 loc) · 4.27 KB
/
NINA_NTPClient_Basic.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
/****************************************************************************************************************************
NINA_NTPClient_Basic.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
WiFiUDP ntpUDP;
#define TIME_ZONE_OFFSET_HRS (-4)
NTPClient timeClient(ntpUDP);
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print(F("\nStart NINA_NTPClient_Basic on ")); Serial.print(BOARD_NAME);
Serial.print(F(" with ")); Serial.println(SHIELD_TYPE);
Serial.println(WIFI_WEBSERVER_VERSION);
Serial.println(NTPCLIENT_GENERIC_VERSION);
Serial.println("Connecting to: " + String(ssid));
WiFi.begin(ssid, pass);
while ( WiFi.status() != WL_CONNECTED )
{
delay ( 500 );
Serial.print ( "." );
}
Serial.print(F("\nNINA_NTPClient_Basic started @ IP address: "));
Serial.println(WiFi.localIP());
timeClient.begin();
timeClient.setTimeOffset(3600 * TIME_ZONE_OFFSET_HRS);
// default 60000 => 60s. Set to once per hour
timeClient.setUpdateInterval(SECS_IN_HR);
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);
}