The date and time should be displayed in several formats as shown below. Open the Serial Monitor at a baud rate of 115200. Make sure you have the right board and COM port selected. Serial.println(currentDate) DemonstrationĪfter inserting your network credentials and modifying the variables to adjust the time to your timezone, test the example. String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay) ![]() int currentYear = ptm->tm_year+1900 įinally, we create a String called currentDate that holds the current date in the YYYY-MM-DD format. To get the year, we need to add 1900 because the tm_year saves the number of years after 1900. The arrays numbering starts at 0, that’s why we subtract 1. Then, we can get the name of the month using the months array we’ve created previously. For example, for the month: int currentMonth = ptm->tm_mon+1 īecause the tm_mday starts at 0, we add 1 to the month so that January corresponds to 1, February to 2, and so on. To get the other elements, you use a similar approach. The following lines get the day of the month as follows: int monthDay = ptm->tm_mday The time structure contains the following elements: struct tm *ptm = gmtime ((time_t *)&epochTime) So, we need to create a time structure ( struct tm) and then, access its elements to get information about the date. The NTP Client doesn’t come with functions to get the date. So, we can access the week day name from the array we’ve created previously as follows String weekDay = weekDays The getDay() function returns a number from 0 to 6, in which 0 corresponds to Sunday and 6 to Saturday. 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:
0 Comments
Leave a Reply. |