Skip to content

Commit 3b359eb

Browse files
committed
Examples
1 parent 6ae0d88 commit 3b359eb

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

Diff for: examples/Advanced/Advanced.ino

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ void setup(){
3030

3131
void loop() {
3232
timeClient.update();
33-
34-
Serial.println(timeClient.getFormattedTime());
35-
33+
Serial.println(timeClient.getFormattedDateTime("%d %B %Y"));
34+
Serial.println(timeClient.getFormattedDateTime("%Y-%m-%d %H:%M:%S"));
3635
delay(1000);
3736
}

Diff for: examples/Basic/Basic.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void setup(){
2727
void loop() {
2828
timeClient.update();
2929

30-
Serial.println(timeClient.getFormattedTime());
30+
Serial.println(timeClient.getFormattedDateTime("%H:%M:%S"));
3131

3232
delay(1000);
3333
}

Diff for: examples/FormattedDateTime/FormattedDateTime.ino

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <NTPClient.h>
2+
// change next line to use with another board/shield
3+
#include <ESP8266WiFi.h>
4+
//#include <WiFi.h> // for WiFi shield
5+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
6+
#include <WiFiUdp.h>
7+
8+
const char *ssid = "<SSID>";
9+
const char *password = "<PASSWORD>";
10+
11+
WiFiUDP ntpUDP;
12+
13+
// You can specify the time server pool and the offset (in seconds, can be
14+
// changed later with setTimeOffset() ). Additionally you can specify the
15+
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
16+
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
17+
18+
void setup(){
19+
Serial.begin(115200);
20+
21+
WiFi.begin(ssid, password);
22+
23+
while ( WiFi.status() != WL_CONNECTED ) {
24+
delay ( 500 );
25+
Serial.print ( "." );
26+
}
27+
28+
timeClient.begin();
29+
timeClient.setDateLanguage("pt"); // Available languages: "pt", "es" and "en" (default)
30+
}
31+
32+
void loop() {
33+
timeClient.update();
34+
Serial.print("%Y: ");
35+
Serial.println(timeClient.getFormattedDateTime("%Y")); // Full year (e.g., 2023)
36+
Serial.print("%y: ");
37+
Serial.println(timeClient.getFormattedDateTime("%y")); // Last two digits of the year (e.g., 23 for 2023)
38+
Serial.print("%m: ");
39+
Serial.println(timeClient.getFormattedDateTime("%m")); // Month as a zero-padded decimal number (01 to 12)
40+
Serial.print("%d: ");
41+
Serial.println(timeClient.getFormattedDateTime("%d")); // Day of the month as a zero-padded decimal number (01 to 31)
42+
Serial.print("%H: ");
43+
Serial.println(timeClient.getFormattedDateTime("%H")); // Hour (00 to 23) as a zero-padded decimal number
44+
Serial.print("%M: ");
45+
Serial.println(timeClient.getFormattedDateTime("%M")); // Minute as a zero-padded decimal number (00 to 59)
46+
Serial.print("%S: ");
47+
Serial.println(timeClient.getFormattedDateTime("%S")); // Second as a zero-padded decimal number (00 to 59)
48+
Serial.print("%a: ");
49+
Serial.println(timeClient.getFormattedDateTime("%a")); // Abbreviated weekday name according to the current locale
50+
Serial.print("%A: ");
51+
Serial.println(timeClient.getFormattedDateTime("%A")); // Full weekday name according to the current locale
52+
Serial.print("%w: ");
53+
Serial.println(timeClient.getFormattedDateTime("%w")); // Weekday as a decimal number (0 for Sunday through 6 for Saturday)
54+
Serial.print("%b: ");
55+
Serial.println(timeClient.getFormattedDateTime("%b")); // Abbreviated month name according to the current locale
56+
Serial.print("%B: ");
57+
Serial.println(timeClient.getFormattedDateTime("%B")); // Full month name according to the current locale
58+
Serial.print("%p: ");
59+
Serial.println(timeClient.getFormattedDateTime("%p")); // "AM" or "PM" based on the hour (Note: This is locale-sensitive and might not be applicable in all languages)
60+
Serial.println("-------------------");
61+
delay(1000);
62+
}

Diff for: examples/IsTimeSet/IsTimeSet.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void setup(){
4242
void loop() {
4343
timeClient.update();
4444

45-
Serial.println(timeClient.getFormattedTime());
45+
Serial.println(timeClient.getFormattedDateTime("%H:%M:%S"));
4646
if(timeClient.isTimeSet()) {
4747
if (hour == timeClient.getHours() && minute == timeClient.getMinutes()) {
4848
digitalWrite(led, 0);

0 commit comments

Comments
 (0)