Int currentSecond = timeClient.getSeconds() Int currentMinute = timeClient.getMinutes() You can get the hours, minutes or seconds separately using the getHours(), getMinutes() and getSeconds() functions as follows: int currentHour = timeClient.getHours() String formattedTime = timeClient.getFormattedTime() The getFormattedTime() function returns the time in HH:MM:SS format. For example, to get the epoch time: unsigned long epochTime = timeClient.getEpochTime() Then, we can use the functions provided by the library to get time. In the loop(), call the update() function to get the current date and time from the NTP server. We live in Portugal, so we don’t need to adjust the time. Here are some examples for different timezones: You can use the setTimeOffset() method to adjust the time for your timezone in seconds. Next, connect the ESP8266 to the internet.
In the setup(), initialize the Serial Monitor to display the information. The above copyright notice and this permission notice shall be included in allĬopies or substantial portions of the Software.Ĭonst char *ssid = "REPLACE_WITH_YOUR_SSID" Ĭonst char *password = "REPLACE_WITH_YOUR_PASSWORD" Of this software and associated documentation files. Permission is hereby granted, free of charge, to any person obtaining a copy It displays the current date and time in several formats in the Serial Monitor.
The following code connects the ESP8266 to an NTP Server ( ) to request date and time.
This library doesn’t come with functions to return the date, but we’ll show you in the code how to get the date (day, month and year). GetFormattedTime() – returns a String with the time formatted like HH:MM:SS
GetEpochTime() – returns an unsigned long with the epoch time (number of seconds that have elapsed since Janu(midnight GMT) GetSeconds() – returns an int number with the current second GetMinutes() – returns an int number with the current minutes (0 to 59) GetHours() – returns an int number with the current hour (0 to 23) in 24 hour format GetDay() – returns an int number that corresponds to the the week day (0 to 6) starting on Sunday The NTPClient Library comes with the following functions to return time: