Get Epoch/Unix Time with the ESP32 (Arduino)
Learn how to request Epoch time or Unix time using the ESP32 board with Arduino IDE. Getting the epoch time can be useful to timestamp your readings, etc...
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
This quick guide shows how to get epoch/unix time using the ESP32 board with Arduino IDE. Getting the epoch time can be useful to timestamp your readings, give unique names to files, and other applications. We’ll request the current epoch time from an NTP server, so the ESP32 needs to have an Internet connection.
If you want to get date and time in a human readable format, we recommend the following tutorial instead:
ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
What is epoch time?
The
Epoch Time
(also know as
Unix epoch,
Unix time
POSIX time
or
Unix timestamp)
is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
NTP (Network Time Protocol)
NTP stands for Network Time Protocol and it is a networking protocol for clock synchronization between computer systems. In other words, it is used to synchronize computer clock times in a network.
There are NTP servers like
pool.ntp.org
that anyone can use to request time as a client. In this case, the ESP32 is an NTP Client that requests time from an NTP Server (
pool.ntp.org)
ESP32 Get Epoch/Unix Time Function
To get epoch/unix time with the ESP32, you can use the following function
getTime()
:
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
This function returns the current epoch time. Continue reading for a complete example.
ESP32 Get Epoch/Unix Time Example
Copy the following code to your Arduino IDE. This code connects to the internet and requests the time from an NTP server (
pool.ntp.org
).
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include `WiFi.h`
#include \"time.h\"
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
// NTP server to request epoch time
const char* ntpServer = \"pool.ntp.org\";
// Variable to save current epoch time
unsigned long epochTime;
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
// Initialize WiFi
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(\"Connecting to WiFi ..\");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
configTime(0, 0, ntpServer);
}
void loop() {
epochTime = getTime();
Serial.print(\"Epoch Time: \");
Serial.println(epochTime);
delay(1000);
}
Insert your network credentials in the following variables and the code will work straight away:
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
How the Code Works
Let’s take a quick look on how the code works.
Libraries
First, you need to include the
WiFi
library to connect the ESP32 to your local network; and the
time
library to handle time structures.
#include `WiFi.h`
#include \"time.h\"
Network Credentials
Insert your network credentials in the following variables so that the ESP32 can connect to your network.
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
NTP Server
We’ll request the time from
pool.ntp.org
, which is a cluster of timeservers that anyone can use to request the time.
const char* ntpServer = \"pool.ntp.org\";
getTime() function
The
getTime()
function gets and returns the current epoch time. If it is not able to get the time, it returns
0
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
initWiFi()
The
initWiFi()
function initializes Wi-Fi and connects the ESP32 to your local network.
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(\"Connecting to WiFi ..\");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
setup()
In the
setup()
, initialize the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
Call the
initWiFi()
function to initialize Wi-Fi:
initWiFi();
Configure the time using the
configTime()
function. The first and second arguments correspond to the GMT time offset and daylight saving time. However, because we’re interested in getting epoch time, these arguments should be
0
. The last argument is the NTP server (
ntpServer
):
configTime(0, 0, ntpServer);
loop()
In the
loop()
, get the epoch time and save it in the
epochTime
variable:
epochTime = getTime();
Finally, print the epoch time every second:
Serial.print(\"Epoch Time: \");
Serial.println(epochTime);
delay(1000);
Demonstration
Upload the code to your ESP32 board. Then, open the Serial Monitor at a baud rate of 115200. Press the ESP32 on-board RST button. It should connect to Wi-Fi and start printing the current epoch/unix time every second.
Wrapping Up
This was a quick guide showing you how to get epoch/unix time with the ESP32. The epoch time is the number of seconds elapsed since January 1 1970. To get time, we need to connect to an NTP server, so the ESP32 needs to have access to the internet.
If you’re interested in getting date and time in a human readable format, refer to the next tutorial:
ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
We hope you’ve found this tutorial useful. If you want to learn more about the ESP32, check our resources:
Learn ESP32 with Arduino IDE
Build Web Servers with ESP32 and ESP8266
More ESP32 Projects and Tutorials…
If you want to get date and time in a human readable format, we recommend the following tutorial instead:
ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
What is epoch time?
The
Epoch Time
(also know as
Unix epoch,
Unix time
POSIX time
or
Unix timestamp)
is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
NTP (Network Time Protocol)
NTP stands for Network Time Protocol and it is a networking protocol for clock synchronization between computer systems. In other words, it is used to synchronize computer clock times in a network.
There are NTP servers like
pool.ntp.org
that anyone can use to request time as a client. In this case, the ESP32 is an NTP Client that requests time from an NTP Server (
pool.ntp.org)
ESP32 Get Epoch/Unix Time Function
To get epoch/unix time with the ESP32, you can use the following function
getTime()
:
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
This function returns the current epoch time. Continue reading for a complete example.
ESP32 Get Epoch/Unix Time Example
Copy the following code to your Arduino IDE. This code connects to the internet and requests the time from an NTP server (
pool.ntp.org
).
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include `WiFi.h`
#include \"time.h\"
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
// NTP server to request epoch time
const char* ntpServer = \"pool.ntp.org\";
// Variable to save current epoch time
unsigned long epochTime;
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
// Initialize WiFi
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(\"Connecting to WiFi ..\");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
configTime(0, 0, ntpServer);
}
void loop() {
epochTime = getTime();
Serial.print(\"Epoch Time: \");
Serial.println(epochTime);
delay(1000);
}
Insert your network credentials in the following variables and the code will work straight away:
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
How the Code Works
Let’s take a quick look on how the code works.
Libraries
First, you need to include the
WiFi
library to connect the ESP32 to your local network; and the
time
library to handle time structures.
#include `WiFi.h`
#include \"time.h\"
Network Credentials
Insert your network credentials in the following variables so that the ESP32 can connect to your network.
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
NTP Server
We’ll request the time from
pool.ntp.org
, which is a cluster of timeservers that anyone can use to request the time.
const char* ntpServer = \"pool.ntp.org\";
getTime() function
The
getTime()
function gets and returns the current epoch time. If it is not able to get the time, it returns
0
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println(\"Failed to obtain time\");
return(0);
}
time(&now);
return now;
}
initWiFi()
The
initWiFi()
function initializes Wi-Fi and connects the ESP32 to your local network.
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(\"Connecting to WiFi ..\");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
setup()
In the
setup()
, initialize the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
Call the
initWiFi()
function to initialize Wi-Fi:
initWiFi();
Configure the time using the
configTime()
function. The first and second arguments correspond to the GMT time offset and daylight saving time. However, because we’re interested in getting epoch time, these arguments should be
0
. The last argument is the NTP server (
ntpServer
):
configTime(0, 0, ntpServer);
loop()
In the
loop()
, get the epoch time and save it in the
epochTime
variable:
epochTime = getTime();
Finally, print the epoch time every second:
Serial.print(\"Epoch Time: \");
Serial.println(epochTime);
delay(1000);
Demonstration
Upload the code to your ESP32 board. Then, open the Serial Monitor at a baud rate of 115200. Press the ESP32 on-board RST button. It should connect to Wi-Fi and start printing the current epoch/unix time every second.
Wrapping Up
This was a quick guide showing you how to get epoch/unix time with the ESP32. The epoch time is the number of seconds elapsed since January 1 1970. To get time, we need to connect to an NTP server, so the ESP32 needs to have access to the internet.
If you’re interested in getting date and time in a human readable format, refer to the next tutorial:
ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
We hope you’ve found this tutorial useful. If you want to learn more about the ESP32, check our resources:
Learn ESP32 with Arduino IDE
Build Web Servers with ESP32 and ESP8266
More ESP32 Projects and Tutorials…
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
- Raspberry Pi
- BME280
Software & tools
- Arduino IDE
- Node-RED
- MQTT
- MicroPython
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.