How ESP32 keeps the time.localtime() without battery?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
isg-mini
Posts: 3
Joined: Thu Mar 18, 2021 3:56 pm

How ESP32 keeps the time.localtime() without battery?

Post by isg-mini » Wed Sep 29, 2021 11:33 am

Hello Team,

I need help to understand how the micropython OS, for the ESP32SPI, can keep the date&time, even without power.

Let me explain:

In our code, we update the date&time using the ntp.settime() via Wi-Fi. No issues here. The 'problem' comes when I unpower the board, 30 min, and once I turn it on, without let it execute the main.py, I enter in the REPL, and after execute time.localtime(), the date&time are sync. How it's possible?

What I expect is to have the same date, but with the time delayed 30 min. There is some kind of 'hidden' code in micropython that it executes ntp.settime() at boot?

Thanks.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How ESP32 keeps the time.localtime() without battery?

Post by Roberthh » Wed Sep 29, 2021 1:09 pm

Without any power the time/date information should be totally gone, not even delayed. So there must be code that updates date & time. Maybe not in main.py, maybe in boot.py. Are you sure that there is no supply for the clock connected, like a coin cell?

davef
Posts: 813
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: How ESP32 keeps the time.localtime() without battery?

Post by davef » Wed Sep 29, 2021 5:16 pm

How do you
I enter in the REPL
rshell?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: How ESP32 keeps the time.localtime() without battery?

Post by scruss » Wed Sep 29, 2021 6:02 pm

As davef hinted, using rshell will attempt to set the clock from your local computer

femtopy
Posts: 7
Joined: Mon Jun 07, 2021 4:57 am

Re: How ESP32 keeps the time.localtime() without battery?

Post by femtopy » Thu Sep 30, 2021 5:12 am

I used to have similar application on my Raspberry Pi and found the OS syncs the time every now and then as long as it has the internet connection. I remember the OS saves that sync time. If I power down the RPi, remove the Ethernet cable, then re-power maybe 10min later, the time is off. Once I reconnect the Ethernet, it corrects the time. I can't remember whether it syncs every minute or 2 min. I do remember it was in low number of minutes.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How ESP32 keeps the time.localtime() without battery?

Post by Roberthh » Thu Sep 30, 2021 6:08 am

The discussion is about the RPi Pico, which foes not have an OS or Ethernet port. But the other comments ate right. PC programs like rshell or Thonny set the time when the device gets connected.

kotk_o
Posts: 10
Joined: Mon Mar 22, 2021 12:19 pm

Re: How ESP32 keeps the time.localtime() without battery?

Post by kotk_o » Thu Sep 30, 2021 9:03 am

davef wrote:
Wed Sep 29, 2021 5:16 pm
How do you
I enter in the REPL
rshell?
We use Thonny IDE. The boot.py is empty, and we rename the main.py to main1.py, also, once we power on the board with the ESP32-WROVER-E, we click "Stop/restart" (stop icon) button in order to enter to REPL without executing the main program. The board is not connected to Wi-Fi. (We powered off the AP)

After that, the we do:

Code: Select all

import time
time.localtime()
And the date & time is sync!!! The only thing that I can image, is that Thonny is doing something... Is not possible that the time is sync without internet.

kotk_o
Posts: 10
Joined: Mon Mar 22, 2021 12:19 pm

Re: How ESP32 keeps the time.localtime() without battery?

Post by kotk_o » Thu Sep 30, 2021 9:08 am

scruss wrote:
Wed Sep 29, 2021 6:02 pm
As davef hinted, using rshell will attempt to set the clock from your local computer
Really? Thonny IDE is using rshell? in its "Shell window"? If this is true, this is really dangerous... because how can you test if the ntp.settime() really works? only becaus it doesn't return an exception?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How ESP32 keeps the time.localtime() without battery?

Post by pythoncoder » Thu Sep 30, 2021 9:51 am

how can you test if the ntp.settime() really works?
With a print statement. Or by deliberately miss-setting the time before the call to ntptime.

I don't know about Thonny, but rshell sets the time to enable rsync to work correctly.
Peter Hinch
Index to my micropython libraries.

isg-mini
Posts: 3
Joined: Thu Mar 18, 2021 3:56 pm

Re: How ESP32 keeps the time.localtime() without battery?

Post by isg-mini » Thu Sep 30, 2021 11:23 am

Thanks!

Post Reply