|
| 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 | +} |
0 commit comments