ESP32 Arduino time operations

This article aims to list down, with examples, some common operations that are performed w.r.t time on ESP32. Let’s get started.

Get time from string

If you have a timestamp in a string format and wish to extract time from it, the function to use is strptime (string parse time). If you’ve used Python in the past, you should be aware of this function.

#include <time.h>

struct tm ts = {0};

strptime("2022-03-28T01:34:42.112Z","%Y-%m-%dT%H:%M:%S.%03dZ",&ts);

The list of formatters (%Y, %m, etc.) and their meanings can be found here.

Getting epoch time from struct

If you have time in the struct format and wish to get the epoch time, the corresponding function is mktime.

#include <time.h>

struct tm ts = {0};

//Populate the struct

//Get the epoch time
time_t epoch_ts = mktime(&ts);

Get struct from epoch time

The localtime_r function can be used for this.

#include <time.h>

struct tm ts2; 
localtime_r(&epoch_ts, &ts2);
Serial.print("Time obtained in struct from epoch is: ");
Serial.println(&ts2, "%A, %B %d %Y %H:%M:%S");

Getting string from time struct

This is the opposite of strptime, and the function to do this is strftime (string format time).

#include <time.h>

struct tm ts = {0};

//Populate ts

char ts_char[50] = {0};
strftime(ts_char,sizeof(ts_char),"%A, %B %d %Y %H:%M:%S", &ts);
Serial.println("strftime output is " + String(ts_char));

Set time to an arbitrary value

You can set the time to an arbitrary value using the settimeofday function.

#include <time.h>
#include <sys/time.h>

time_t epoch_ts = 1648499624;
struct timeval tv_ts = {.tv_sec = epoch_ts};
settimeofday(&tv_ts, NULL);

The second argument (NULL) refers to the timezone and is not used. See this.

Get local time

Once the system time is set, the RTC counter increments in the background. You can get the local time at any time by calling the getLocalTime() function.

#include <time.h>

//Get RTC time
struct tm new_ts;
getLocalTime(&new_ts);
Serial.print("Current time obtained from RTC is: ");
Serial.println(&new_ts, "%A, %B %d %Y %H:%M:%S");

Configure time using NTP

This requires a WiFi connection for first-time configuration. The time is fetched from the NTP server and the local time is updated.

#include <WiFi.h>
#include "time.h"

//Setup and begin WiFi

//Configure NTP
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 19800; //IST
const int   daylightOffset_sec = 0;

//Config time using NTP
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

//Get RTC time
getLocalTime(&new_ts);
Serial.print("Current time obtained from RTC after NTP config is: ");
Serial.println(&new_ts, "%A, %B %d %Y %H:%M:%S");

WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);

delay(5000);

//Get RTC time
getLocalTime(&new_ts);
Serial.print("Current time obtained from RTC after NTP config and WiFi off is: ");
Serial.println(&new_ts, "%A, %B %d %Y %H:%M:%S");

GitHub

An Arduino sketch putting all of this together can be found on GitHub here.


For more tutorials on ESP32, check out https://iotespresso.com/category/esp32/. Also, you may find this course on ESP32 on Udemy to be quite helpful. Do check it out

Leave a comment

Your email address will not be published. Required fields are marked *