Created
June 18, 2023 08:15
-
-
Save lonelybinary/3505b07efdc71354fb8f640789c75b1b to your computer and use it in GitHub Desktop.
Arduino ESP32 RTC Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
/* | |
Get Current unix time stamp from https://www.unixtimestamp.com/ | |
TZ_INFO is "Australia/Sydney" | |
*/ | |
#define UNIX_TIMESTAMP 1687072944 | |
#define TZ_INFO "CST-10" | |
void set_time(time_t unix_time_t) | |
{ | |
/* | |
Set ESP32 RTC time with unix time stamp | |
*/ | |
struct timeval tv = {unix_time_t, 0}; | |
settimeofday(&tv, NULL); | |
/* | |
Set timezone to Sydney Time, | |
and assign the local timezone from setenv | |
*/ | |
setenv("TZ", TZ_INFO, 1); | |
tzset(); | |
}; | |
void setup() | |
{ | |
Serial.begin(115200); | |
delay(5000); | |
// Get current ESP32 RTC UNIX Time Stamp | |
time_t now; time(&now); | |
if (now < 1600000000L) | |
{ | |
Serial.println("Set the ESP32 RTC Timer"); | |
set_time(UNIX_TIMESTAMP); | |
} | |
} | |
void loop() | |
{ | |
/* Get Current Unix Time Stamp*/ | |
struct timeval tv_now; | |
gettimeofday(&tv_now, NULL); | |
Serial.print("Unix Time Stamp Now gettimeofday(): "); | |
Serial.println(tv_now.tv_sec); | |
/* get Current Unix Time Stamp */ | |
time_t now; | |
time(&now); | |
Serial.print("Unix Time Stamp Now time(): "); | |
Serial.println(now); | |
/* | |
Convert Unix Time Stamp into Local Time Struct | |
asctime() converts time struct into string | |
*/ | |
struct tm timeinfo; | |
localtime_r(&now, &timeinfo); | |
Serial.print("Local Time is asctime() : "); | |
Serial.printf(asctime(&timeinfo)); | |
Serial.print("Year : "); | |
Serial.println(timeinfo.tm_year + 1900); | |
Serial.print("Month : "); | |
Serial.println(timeinfo.tm_mon + 1); | |
Serial.print("Day : "); | |
Serial.println(timeinfo.tm_mday); | |
Serial.print("Hour : "); | |
Serial.println(timeinfo.tm_hour); | |
Serial.print("Min : "); | |
Serial.println(timeinfo.tm_min); | |
Serial.print("Sec : "); | |
Serial.println(timeinfo.tm_sec); | |
// Sunday is 0, Monday is 1, and so on. | |
Serial.print("Week Day : "); | |
Serial.println(timeinfo.tm_wday); | |
Serial.print("Year Day : "); | |
Serial.println(timeinfo.tm_yday); | |
/* Unix Timestamp to String */ | |
Serial.print("Local Time is ctime() : "); | |
Serial.printf(ctime(&now)); | |
/* Custom Time Format into String*/ | |
char strftime_buf[64]; | |
strftime(strftime_buf, sizeof(strftime_buf), "%A, %B %d %Y %H:%M:%S", &timeinfo); | |
Serial.print("The formatted Time : "); | |
Serial.println(strftime_buf); | |
/* Easy way: Custom Time Format into String*/ | |
Serial.print("The formatted Time with Serial.println() : "); | |
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment