Questions about internal RTC
Posted: 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;
}
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;
}