Getting Hour and Minutes from ESP32 RTC

commando_j
Posts: 32
Joined: Tue Feb 05, 2019 5:26 pm

Getting Hour and Minutes from ESP32 RTC

Postby commando_j » Sun Dec 08, 2024 6:13 pm

Hello,

I'm working with Espressif-IDE and framework 5.3.1. I am able to get the updated time from SNTP and have it written to the ESP32 RTC. And I can also retrieve individual elements of the RTC time using specifiers in the strftime function, for example

strftime(MY_TIME, sizeof(MY_TIME), "%I:%M%p", &timeinfo);
or
strftime(MY_HOUR, sizeof(MY_HOUR), "%I", &timeinfo);

The problem I have is ultimately I am using an external RTC chip for my timekeeping, MCP79510 over SPI. This too is working fine. I can read the individual registers, create a string and write it to my oled display.

The external SPI RTC needs to have the data written to each register as uint8_t values. But the values retrieved from the internal RTC are string values. How can I get for example the HOUR value as a uint8_t to be able to write it to the external RTC?

Order of operation:
Get SNTP time > write to internal RTC > read internal RTC individual elements (HOUR, MINUTE) > write uint8_t HOUR,MINUTE to external RTC.

In Arduino, I can see there are functions such as getMinute(), getHour() but I see nothing in ESP-IDF that does the same.

Any help is appreciated, going nuts trying to figure it out.

commando_j
Posts: 32
Joined: Tue Feb 05, 2019 5:26 pm

Re: Getting Hour and Minutes from ESP32 RTC

Postby commando_j » Sun Dec 08, 2024 7:02 pm

I think I may have found an answer, but need to test more.

I did

uint8_t hrt = timeinfo.tm_hour;

and then to test I printed

ESP_LOGI(TAG, "The formatted int hour is : %d", hrt);

And it printed the correct value of the current hour that was stored in the internal RTC. Does this make sense?

MicroController
Posts: 1818
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Getting Hour and Minutes from ESP32 RTC

Postby MicroController » Sun Dec 08, 2024 11:07 pm

commando_j wrote:
Sun Dec 08, 2024 7:02 pm
Does this make sense?
Probably. Impossible to tell without knowing what your "timeinfo" is (struct tm?) and, more importantly, how you got the data into it.

You may want to check <time.h>.

commando_j
Posts: 32
Joined: Tue Feb 05, 2019 5:26 pm

Re: Getting Hour and Minutes from ESP32 RTC

Postby commando_j » Mon Dec 09, 2024 12:49 am

The internal RTC is written into by the example SNTP code of the ESP-IDF. The timeinfo is the structure used to get the internal RTC data. The structure has members for year, hour, minutes... that are accessible by reading .tm_year, .tm_hour, .tm_min...etc...

The good thing is I can get the data. The bad is now I'm stuck again because the external RTC is expecting BCD data to be written to the registers. I'll be making another post for that.

MicroController
Posts: 1818
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Getting Hour and Minutes from ESP32 RTC

Postby MicroController » Mon Dec 09, 2024 12:56 am

Code: Select all

uint8_t bcd = ((value / 10) << 4) | (value % 10);

Who is online

Users browsing this forum: Majestic-12 [Bot] and 62 guests