Questions about internal RTC

creatorbyte
Posts: 2
Joined: Fri Mar 22, 2019 11:22 pm

Questions about internal RTC

Postby creatorbyte » Fri Mar 22, 2019 11:59 pm

I'm very new to the ESP development boards. However, I'm currently working on a small project with an ESP32. I've worked with Arduino boards in the past but the ESP32 is new to me. I'm trying to find a way to get time from the internal RTC so I can display it as a digital clock would. Normally I work with some kind of RTC module that makes this quite easy, but the project I'm working on(An ESP32 "smartwatch") has a very small package with a severely limited number of pins(Only enough for a single I2C connection and RX/TX). If I wanted to add an RTC module at this point, I would need to create a custom PCB that is small enough to fit in the watch and draw power from the main battery instead of a coin cell. I was planning on taking this route when I saw several diagrams/schematics of the ESP32 and discovered that it has a built-in RTC module. I want to find a way to get and set the time for the internal RTC(however inaccurate it will be). For this current project, I really don't care if the RTC is super accurate as long as it won't drift more than 1-2 minutes in a day. I just want to find a way that I can get the current time from an internet server and then Update the internal RTC so it can kind of keep track of time in case it gets powered off while it's away from an internet connection. I will still probably end up making a custom RTC module for my smartwatch but until then I would like to go this route. I tried using the gettimeofday() function but it only gives me the time that the system has been powered on. I can't seem to find any examples of what I'm trying to achieve here and I've been looking and browsing the forums for nearly 4 hours. So my question is: How can a read/write time for the internal RTC? I've seen several forum posts about the depreciated rtc_time_get() function that was supposedly replaced by the gettimeofday(). However, The documentation is not helping me to understand how to do this at all and I'm just getting more confused. Is the RTC supposed to reset after the board loses power? Instead of an off switch, do I need to implement a "Deep Sleep" button to ensure that the RTC stays turned on but everything else is essentially unpowered?

My current use of the gettimeofday() function only tells me the time the system has been turned on and is as follows:

int GetTodaysTime() {
struct timeval tv;
//struct timezone null; //the documentation said that this is obsolete
gettimeofday(&tv, NULL); // its been nearly two years since I last used pointers and structures so I hope I'm not making a stupid mistake :-)

long hms = tv.tv_sec % SEC_PER_DAY;
hms = (hms + SEC_PER_DAY) % SEC_PER_DAY;
int hour = hms / SEC_PER_HOUR;
int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN;
int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN

Serial.println("The current time is: "); // P.s. How could i format this like printf("time is: %f%f%f", h, m, s) for example?
Serial.println(hour);
Serial.println(min);
Serial.println(sec);
return 0;
}

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

Re: Questions about internal RTC

Postby ESP_Sprite » Mon Mar 25, 2019 3:41 am

Yes, the RTC doesn't know the current time from CPU start, and it will reset when the chip is reset externally or the power goes away. Implementing deep sleep would be the thing to do. I think you can set the RTC manually using settimeofday(), by the way.

creatorbyte
Posts: 2
Joined: Fri Mar 22, 2019 11:22 pm

Re: Questions about internal RTC

Postby creatorbyte » Mon Mar 25, 2019 6:13 pm

So I should implement a deep sleep instead of cutting power with a switch to turn it off? That’s probably the best thing to do anyway since I’m always nervous about just cutting power to a processor before it can “shut down” properly. If it’s in a deep sleep stage can I cut power safely or is that still not a good thing to do? I’m thinking about implementing a “sleep” button that will save the RTC time to an SD card and put the ESP into a sleep. The RTC should hold its time during the sleep but when it comes out of the sleep I will have it check the old time vs current time to approximate how long it was sleeping and maybe adjust for inaccuracies at that point and set RTC accordingly.

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

Re: Questions about internal RTC

Postby ESP_Sprite » Wed Mar 27, 2019 2:13 am

There's normally no issue with cutting power to a microcontroller; the exception is when it's writing to flash using a method that's not designed to cope with power failures. (From memory, nvs and spiffs both are; fat is not.) Also, no need to save the time to SD-card: you get 2x8K of memory that will stay powered on during deep sleep, so you can easily save state there.

Who is online

Users browsing this forum: No registered users and 153 guests