I'm working on a project where an ESP32 will be running outside of cell and wifi range. Since I can't connect to a time server I'm trying to get the onboard RTC working. I know that's not the most accurate RTC but for my purposes a little drift is fine.
This works well, and keeps time from when I set the time with rtc.setTime(). However if I reboot the ESP32 and comment out that line, my script won't have any idea what time it is.
Am I correct in thinking the onboard RTC doesn't store the time between restarts? I know that it can't advance the time when it's powered off since it doesn't have a battery, but I thought/hoped it would at least store the last known time, so that if I set the time and keep it plugged in, it will keep the time even through a restart. Am I wrong about that?
Why? Members are advised to post the entire sketch.
It sure looks like. Read the specifications for the ESP32. Reading datasheets and the application notes saves You from lots of trouble. Guessing, believing doesn't work for You, only against You.
Not being totally sure I'm saying yes. Try it! Again, read the documentation!
Because I posted a fully working example that demonstrates the problem perfectly. It's a lot easier to understand than me posting 300 lines of code that are 99% concerned with reading data from a solar charge controller using modbus (for example).
RTC timer: This timer allows time keeping in various sleep modes, and can also persist time keeping across any resets (with the exception of power-on resets which reset the RTC timer). The frequency deviation depends on the RTC Timer Clock Sources and affects the accuracy only in sleep modes, in which case the time will be measured at 6.6667 μs resolution.
According to that it sounds like it should keep the time through a reset (though not cycling power), but mine isn't. Unless reflashing counts as cycling power.
Either way I wonder if it's possible to write the time when powering off to a SPIFFS partition (or elsewhere) and then read it when booting back up to make the time quasi persistent. Hmm.
You can use an NVS partition to save data tha you need for the next boot up. I normally use
esp-idf so I do not know how it is done in arduino or if an example exists.
There is an example in the esp-arduino core called eeprom_write.ino that you could probably use for saving some values and then reading them on the next re-boot.
From my reading of the RTC it will keep the time through a reset but not it you turn the power off and then on (power on reset).
But not across a power down event. I wondered the same things and started to look for some Vbat pin on any of them. But I got sidetracked with other stuff. In any case, the onboard RTC would not be temperature compensated. That in itself is a reason to use an external RTC if you want extreme accuracy.
However, it's kind of funny, the DS3231 module I have is actually almost as big as the LOLIN mini I'm mostly using. It would certainly be nicer to just pop a battery into a holder on the MCU board. "You can always just get the time from the internet" is not always true.
Good point. Awhile ago I had a project that used D1-minis and a very small enclosure, and the size challenge wasn't from the ESP but from the RTC. I even had to shave a bit off the top of the battery holder and headers with a dremel to make things fit. I never did find a good RTC that was small. Not that it matters for the project I'm currently working on, but it is funny.
And it's a shame Espressif didn't make a way to keep the onboard RTC running even after a power off with an external battery. I've read that it's accuracy isn't great and will be off about 2 minutes a day, but there's cases like the project I'm working on now where that's acceptable. But of course losing data when it gets unplugged isn't.
It is of course still useful if a timeserver is available.
Actually, when I was looking at the D1 accessory shields, I was wondering why there is no RTC module. Or the classic "logging module" with an RTC and SD card combo.
The DS3231 module for the RPI is very small, but the solder tab battery is usually DOA because it's so tiny and can't last in storage.
The sensible thing to do for storage with a non-removable battery, is to disable the oscillator with the register bit, but then most users would see it as defective.