setting system time/date

bmartin0
Posts: 33
Joined: Tue Aug 03, 2021 10:38 pm

setting system time/date

Postby bmartin0 » Tue Apr 26, 2022 8:52 pm

Hello,
I have an external RTC connected to the ESP32 via an i2c port. I want to use this time/date as the ESP32 time reference. I need to update the ESP32 time/date at boot with this external RTC value. I also have an sdcard interface and I want to be able to create a file and have that file time/date info match the RTC time/date. Currently I'm using POSIX commands such as fopen() and when a file or directory is created I get the default time/date which is something like 11:00pm, January 1, 1979. I know that I need to initialize the ESP32 time and date from the RTC at boot and have looked at the comments/examples on the web for this with no luck. Most of the info seems to be using an internet retreived time/date. My device only uses AP mode and I have no internet connection hence the need for the RTC.
Can any one provide an example of setting the time/date of the ESP32 based on the following strings(that I get from my i2c connected RTC chipset)
time: 14:48:22
date: 04/26/2022

I have the current time/date, just need to know how to initialize the ESP32 timer system with that value at boot so when a user creates a file on the sd card from a client connected device it will have the correct date.
Any help would be appreciated.
Thanks

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: setting system time/date

Postby ESP_Sprite » Wed Apr 27, 2022 1:33 am

You probably want to use settimeofday() to update the systems idea of the current time.

bmartin0
Posts: 33
Joined: Tue Aug 03, 2021 10:38 pm

Re: setting system time/date

Postby bmartin0 » Thu Apr 28, 2022 4:33 pm

Ok, so if you need to set the ESP32 system time and date at boot from an RTC(connected to the serial port), here's a method that works. Runtime.hour, minute, second, etc. is updated by reading the RTC via serial commands before calling this routine. Hopes this helps.

void SetESP32SysTime(void)
{
struct tm tm;
char tmpcmd[TX_BUF_SIZE];

//Get current time from the RTC
memset(tmpcmd, '\0', TX_BUF_SIZE);
//Now put the command to get douts into the tx buffer
sprintf(&tmpcmd[0], "%s", "gtime\r");

//Ship out the command to the STM32
sendData(TX_TASK_TAG, tmpcmd);

//Wait 50ms for the response
vTaskDelay(50/portTICK_RATE_MS);
ESP_LOGI(TAG, "System time %d:%d:%d", Runtime.hour, Runtime.minute, Runtime.second);

//Get current date from the RTC
memset(tmpcmd, '\0', TX_BUF_SIZE);
//Now put the command to get douts into the tx buffer
sprintf(&tmpcmd[0], "%s", "gdate\r");

//Ship out the command to the STM32
sendData(TX_TASK_TAG, tmpcmd);

//Wait 50ms for the response
vTaskDelay(50/portTICK_RATE_MS);
ESP_LOGI(TAG, "System date %d/%d/%d", Runtime.month, Runtime.day, Runtime.year);

tm.tm_year = (Runtime.year + 2000) - 1900;
tm.tm_mon = Runtime.month - 1;
tm.tm_mday = Runtime.day;
tm.tm_hour = Runtime.hour;
tm.tm_min = Runtime.minute;
tm.tm_sec = Runtime.second;
time_t t = mktime(&tm);
ESP_LOGI(TAG, "Setting System Time: %s", asctime(&tm));
struct timeval now = { .tv_sec = t };
settimeofday(&now, NULL);
}

bmartin0
Posts: 33
Joined: Tue Aug 03, 2021 10:38 pm

Re: setting system time/date

Postby bmartin0 » Thu Apr 28, 2022 4:35 pm

The year in this code is Runtime.year = 22 just to be clear.

Who is online

Users browsing this forum: No registered users and 406 guests