0

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

Ken White
  • 123,280
  • 14
  • 225
  • 444
GILO
  • 2,444
  • 21
  • 47

2 Answers2

1

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:

  1. use an external RTC module;
  2. use an external GPS module (requires to be outdoor);
  3. call an NTP service to set the time after reset;
  4. don't unplug it or don't reset it, put it in deep sleep mode.
hcheung
  • 3,377
  • 3
  • 11
  • 23
0

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.

kryozor
  • 315
  • 1
  • 7