0

Questions about internal RTC for ESP32
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Questions about internal RTC for ESP32

by creatorbyte on Fri Mar 22, 2019 8:13 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;
}

creatorbyte
 
Posts: 20
Joined: Sun Dec 30, 2018 7:59 pm

Re: Questions about internal RTC for ESP32

by go on Wed Mar 27, 2019 12:02 am

Can't really answer your question, but I'm interested in the same kinds of questions. Found this, indicating internal ESP32 RTC has quite a bit of drift:
https://www.esp32.com/viewtopic.php?t=3715
When I wanted a clock for my ESP8266, I adapted the standard Arduino NTP example, and used a timer interrupt (a 10Hz Ticker) to keep time between NTP calls, then adjusted when the next NTP came in. Same code runs on ESP32 without a hitch.

This may also be useful, indicates that deep sleep keeps the internal RTC going:
https://blog.adafruit.com/2018/02/20/deep-sleep-tutorial-for-esp32-using-the-rtc-ulp-co-processor-and-two-lines-of-code/

go
 
Posts: 45
Joined: Fri Sep 11, 2015 10:41 am

Please be positive and constructive with your questions and comments.


cron