Skip to content

Commit

Permalink
Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
leodrivera committed May 10, 2024
1 parent 6ae0d88 commit 3b359eb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
5 changes: 2 additions & 3 deletions examples/Advanced/Advanced.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void setup(){
void loop() {
timeClient.update();

Serial.println(timeClient.getFormattedTime());
Serial.println(timeClient.getFormattedDateTime("%H:%M:%S"));

delay(1000);
}
62 changes: 62 additions & 0 deletions examples/FormattedDateTime/FormattedDateTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

const char *ssid = "<SSID>";
const char *password = "<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);
}
2 changes: 1 addition & 1 deletion examples/IsTimeSet/IsTimeSet.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3b359eb

Please sign in to comment.