Reputation: 2623
Hi to restart the device without resetting RTC clock. For example, when I run:
import machine
print(str(time.localtime()))
# Set the datetime
machine.RTC().datetime((...))
print(str(time.localtime()))
machine.reset()
print(str(time.localtime()))
Outputs like this
(2000, 1, 1, 0, 2, 4, 5, 1)
(2052, 11, 10, 10, 26, 45, 6, 315)
# Resets
(2000, 1, 1, 0, 2, 4, 5, 1)
I would like reset everything but the RTC time
Upvotes: 0
Views: 1822
Reputation: 1
At the technical manual of ESP32 it is stated that there are 3 reset sources which are CPU reset, Core reset and System reset. System reset causes all the registers including RTC registers get cleared. Watchdog timer and brown-out resets are also causes system reset. If your system is a battery powered device you can stay away from system reset in some extent but using an external RTC would be more trustworthy.
Upvotes: 0
Reputation: 325
You can't. The RTC is a hardware device, and it's not reset when you reset the microcontroller. You can only reset the RTC by removing the battery or by using the reset pin on the RTC chip.
But if you still want to preserve the RTC, you can use the RTC memory to store the current time, and then restore it after the reset. For example:
import machine
import time
# Save the current time in RTC memory
rtc = machine.RTC()
rtc.memory(str(time.localtime()))
# Set the datetime
rtc.datetime((...))
# Reset the device
machine.reset()
# Restore the time from RTC memory
rtc = machine.RTC()
time.localtime(eval(rtc.memory()))
print(str(time.localtime()))
The eval() function is used to evaluate a string as a Python expression. It's not safe to use eval() with untrusted input, but in this case it's safe because the string is generated by the program itself.
Note that the RTC memory is volatile, so it will be lost if the battery is removed or if the device is powered off. You can use the RTC memory to store other data, but you should use a non-volatile storage like a file or a database to store data that you want to preserve.
Upvotes: 0
Reputation: 4034
ESP32 internal RTC do not preserved the settings with hard reset, ESP32 RTC time however is preserved during deep sleep.
If you want to "preserved" the RTC time:
Upvotes: 1