From 30552d5379adfd1a53f2394f84ecd4b9990062ea Mon Sep 17 00:00:00 2001 From: HamzaYslmn Date: Wed, 17 May 2023 10:30:37 +0300 Subject: [PATCH] Basic Example, esp8266 and esp32 --- examples/EspDateTime/EspDateTime.ino | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/EspDateTime/EspDateTime.ino diff --git a/examples/EspDateTime/EspDateTime.ino b/examples/EspDateTime/EspDateTime.ino new file mode 100644 index 0000000..8531649 --- /dev/null +++ b/examples/EspDateTime/EspDateTime.ino @@ -0,0 +1,56 @@ +#ifdef ESP32 + #include +#else + #include +#endif +#include +#include + +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP); + +void setup() { + Serial.begin(115200); + WiFi.begin("yourSSID", "yourPassword"); + + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + + timeClient.begin(); + timeClient.update(); +} + +void loop() { + timeClient.update(); + + if (timeClient.isTimeSet()) { + unsigned long epochTime = timeClient.getEpochTime(); + struct tm *timeinfo; + time_t time = (time_t)epochTime; + timeinfo = gmtime(&time); + + int year = timeinfo->tm_year + 1900; + int month = timeinfo->tm_mon + 1; + int day = timeinfo->tm_mday; + int hour = timeinfo->tm_hour; + int minute = timeinfo->tm_min; + int second = timeinfo->tm_sec; + + Serial.print("Current date and time: "); + Serial.print(year); + Serial.print("-"); + Serial.print(month); + Serial.print("-"); + Serial.print(day); + Serial.print(" "); + Serial.print(hour); + Serial.print(":"); + Serial.print(minute); + Serial.print(":"); + Serial.println(second); + } + + delay(1000); +} \ No newline at end of file