Example Code for Arduino-Get network time with WIFI

This example demonstrates getting the time from a network time server and using the RTC clock that comes with the ESP32 to keep the time updated.

Hardware Preparation

Software Preparation

Sample Code

#include <WiFi.h>

const char *ssid = "********";    //WIFI name
const char *password = "********"; //WIFI password

const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

void printLocalTime()
{
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }
    Serial.println(&timeinfo, "%F %T %A"); // formatted output
}

void setup()
{
    Serial.begin(115200);
    Serial.println();

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WiFi connected!");

    // Get and set the time from the network time server
    // After the acquisition is successful, the chip will use the RTC clock to keep the time updated
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    printLocalTime();

    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
    Serial.println("WiFi disconnected!");
}

void loop()
{
    delay(1000);
    printLocalTime();
}

Parameter Explanation

struct tm structure

struct tm {
int tm_sec; // seconds, value 0~59;
int tm_min; // minutes, value 0~59;
int tm_hour; // hour, value 0~23;
int tm_mday; // The day of the month, the value is 1~31;
int tm_mon; // month, value 0~11;
int tm_year; // year, whose value is equal to the actual year minus 1900;
int tm_wday; // week, value 0~6, 0 is Sunday, 1 is Monday, and so on;
int tm_yday; // The date of the year, value 0~365, 0 represents January 1, 1 represents January 2, and so on;
int tm_isdst; // Daylight saving time identifier, when daylight saving time is implemented, tm_isdst is positive; when daylight saving time is not implemented, tm_isdst is 0; when the situation is unknown, tm_isdst() is negative
};

struct tm structure formatted output

Formatting characters Output
%a Abbreviation for day of the week
%A Full name of the day of the week
%b Abbreviation for month
%B Full name of month
%c Standard date string
%C Last two digits of the year
%d Day of the month in decimal
%D Month/day/year
%e Day of the month in decimal in a two-character field
%F Year-month-day
%g The last two digits of the year, using a week-based year
%G Years, use week-based years
%h Abbreviated month name
%H 24 hour clock
%I 12 hour clock
%j Day of the year in decimal
%m Month in decimal
%M Minutes in decimal
%p Equivalent display of local AM or PM
%r Time in 12 hours
%R Display hours and minutes: hh:mm
%S Second in decimal
%t Horizontal tab
%T Display hours, minutes and seconds: hh:mm:ss
%u Day of the week, Monday is the first day (values from 0 to 6, Monday is 0)
%U Week of the year, with Sunday as the first day (values from 0 to 53)
%V Week of the year, using a week-based year
%w Day of the week in decimal (values from 0 to 6, 0 for Sunday)
%W Week of the year, with Monday as the first day (values from 0 to 53)
%x Standard date string
%X Standard time string
%y Year in decimal without century (values from 0 to 99)
%Y Year in decimal with century
%z Time zone name, or return null if no time zone name is available

Was this article helpful?

TOP