From 3b359ebe1848a887e7947339ce2e992f20a40d52 Mon Sep 17 00:00:00 2001 From: Leonardo Rivera Date: Fri, 10 May 2024 00:50:59 -0300 Subject: [PATCH] Examples --- examples/Advanced/Advanced.ino | 5 +- examples/Basic/Basic.ino | 2 +- .../FormattedDateTime/FormattedDateTime.ino | 62 +++++++++++++++++++ examples/IsTimeSet/IsTimeSet.ino | 2 +- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 examples/FormattedDateTime/FormattedDateTime.ino diff --git a/examples/Advanced/Advanced.ino b/examples/Advanced/Advanced.ino index 18a6a97..bb21b16 100644 --- a/examples/Advanced/Advanced.ino +++ b/examples/Advanced/Advanced.ino @@ -30,8 +30,7 @@ void setup(){ void loop() { timeClient.update(); - - Serial.println(timeClient.getFormattedTime()); - + Serial.println(timeClient.getFormattedDateTime("%d %B %Y")); + Serial.println(timeClient.getFormattedDateTime("%Y-%m-%d %H:%M:%S")); delay(1000); } diff --git a/examples/Basic/Basic.ino b/examples/Basic/Basic.ino index f0a2a7c..71d1857 100644 --- a/examples/Basic/Basic.ino +++ b/examples/Basic/Basic.ino @@ -27,7 +27,7 @@ void setup(){ void loop() { timeClient.update(); - Serial.println(timeClient.getFormattedTime()); + Serial.println(timeClient.getFormattedDateTime("%H:%M:%S")); delay(1000); } diff --git a/examples/FormattedDateTime/FormattedDateTime.ino b/examples/FormattedDateTime/FormattedDateTime.ino new file mode 100644 index 0000000..c513626 --- /dev/null +++ b/examples/FormattedDateTime/FormattedDateTime.ino @@ -0,0 +1,62 @@ +#include +// change next line to use with another board/shield +#include +//#include // for WiFi shield +//#include // for WiFi 101 shield or MKR1000 +#include + +const char *ssid = ""; +const char *password = ""; + +WiFiUDP ntpUDP; + +// You can specify the time server pool and the offset (in seconds, can be +// changed later with setTimeOffset() ). Additionally you can specify the +// update interval (in milliseconds, can be changed using setUpdateInterval() ). +NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); + +void setup(){ + Serial.begin(115200); + + WiFi.begin(ssid, password); + + while ( WiFi.status() != WL_CONNECTED ) { + delay ( 500 ); + Serial.print ( "." ); + } + + timeClient.begin(); + timeClient.setDateLanguage("pt"); // Available languages: "pt", "es" and "en" (default) +} + +void loop() { + timeClient.update(); + Serial.print("%Y: "); + Serial.println(timeClient.getFormattedDateTime("%Y")); // Full year (e.g., 2023) + Serial.print("%y: "); + Serial.println(timeClient.getFormattedDateTime("%y")); // Last two digits of the year (e.g., 23 for 2023) + Serial.print("%m: "); + Serial.println(timeClient.getFormattedDateTime("%m")); // Month as a zero-padded decimal number (01 to 12) + Serial.print("%d: "); + Serial.println(timeClient.getFormattedDateTime("%d")); // Day of the month as a zero-padded decimal number (01 to 31) + Serial.print("%H: "); + Serial.println(timeClient.getFormattedDateTime("%H")); // Hour (00 to 23) as a zero-padded decimal number + Serial.print("%M: "); + Serial.println(timeClient.getFormattedDateTime("%M")); // Minute as a zero-padded decimal number (00 to 59) + Serial.print("%S: "); + Serial.println(timeClient.getFormattedDateTime("%S")); // Second as a zero-padded decimal number (00 to 59) + Serial.print("%a: "); + Serial.println(timeClient.getFormattedDateTime("%a")); // Abbreviated weekday name according to the current locale + Serial.print("%A: "); + Serial.println(timeClient.getFormattedDateTime("%A")); // Full weekday name according to the current locale + Serial.print("%w: "); + Serial.println(timeClient.getFormattedDateTime("%w")); // Weekday as a decimal number (0 for Sunday through 6 for Saturday) + Serial.print("%b: "); + Serial.println(timeClient.getFormattedDateTime("%b")); // Abbreviated month name according to the current locale + Serial.print("%B: "); + Serial.println(timeClient.getFormattedDateTime("%B")); // Full month name according to the current locale + Serial.print("%p: "); + 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) + Serial.println("-------------------"); + delay(1000); +} diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 619bfde..b8c2492 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -42,7 +42,7 @@ void setup(){ void loop() { timeClient.update(); - Serial.println(timeClient.getFormattedTime()); + Serial.println(timeClient.getFormattedDateTime("%H:%M:%S")); if(timeClient.isTimeSet()) { if (hour == timeClient.getHours() && minute == timeClient.getMinutes()) { digitalWrite(led, 0);