after Deep Sleep ESP32 - Time offset

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

after Deep Sleep ESP32 - Time offset

Postby weicks » Tue Sep 25, 2018 12:40 pm

Has anyone already noticed a similar problem?
i leave my esp32 for 9 hours in the DeepSleep and after waking up (with its timer) is a time offset of the RTC of +10 minutes and a few seconds.

i have a rtc clock which works fine before switching in DeepSleep (no offset).

ESP_Sprite
Posts: 1930
Joined: Thu Nov 26, 2015 4:08 am

Re: after Deep Sleep ESP32 - Time offset

Postby ESP_Sprite » Wed Sep 26, 2018 1:40 am

If you don't have an external 32KHz crystal, the ESP32 will try to keep its internal time using an internal 150KHz oscillator (because the main oscillator is shut off in this mode). This oscillator isn't that precise, leading to the time offset you're seeing. It's a hardware thing, it can't really be helped (except by adding a RTC crystal).

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

Re: after Deep Sleep ESP32 - Time offset

Postby weicks » Wed Sep 26, 2018 10:21 am

thanks for your replay!

my setup: external RTC 1307 on the controller. No alarm function on hardware side available, therefore the ESP should use the internal timer (RTC from the controller) and should than go into the DeepSleep
when waking up, the clock should be used again by the external RTC.
The external RTC works without problems with 0 to max. 1 second offset per hour.
so i think the internal RTC of the contoller didn´t mind if it is not as accurate.

By the way, the samplecode I used is from here: https://github.com/Makuna/Rtc/blob/master/examples/DS1307_Simple/DS1307_Simple.ino

and the usual command to go into DeepSleep:
if (((dt.Hour () == 22) && (dt.Minute () == 00) && (dt.Second () == 00))) {
           esp_sleep_enable_timer_wakeup (TIME_TO_SLEEP * uS_TO_S_FACTOR);
           esp_deep_sleep_start ();
}

would be glad if someone has a solution for this :)

ESP_Sprite
Posts: 1930
Joined: Thu Nov 26, 2015 4:08 am

Re: after Deep Sleep ESP32 - Time offset

Postby ESP_Sprite » Thu Sep 27, 2018 1:18 pm

Well, what happens is that the internal RTC runs a bit too fast or slow, causing the error to accumulate over time. If you really need a deep sleep wakeup at a specific time, what you could do is wake up a bit (20 minutes?) earlier, then look at the DS1307 to see how much more you actually need to sleep. Because this time is way less than 9 hours, the error will also be less.

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

Re: after Deep Sleep ESP32 - Time offset

Postby weicks » Fri Sep 28, 2018 7:34 am

i really don´t understand why this happens, because at daytime i measured a offset of eg. max +3 seconds over 12 hours (while controller is running). I know its also not a good offset but wouldn´t be as bad because in the final program i do a sync with the ntpServer (but it´s not always possible to sync with wifi) because of this .. the clock don´t have to be as accurate.
The problem occurs only after the deepsleep. if i do a deepsleep of only 1 hour there is a very small offset (would say +3 seconds) i don´t know how much but its much under 50seconds. Because in 9 hours 10 min offset would be over 1 minute per hour!!!

@ESP_Sprite: so i thought what if i do it like this (did you mean it like this?):
if(((dt.Hour() == 22) && (dt.Minute() == 0) && (dt.Second() == 0)))

{ Rtc.SetDateTime(RtcDateTime(dt.Year(), dt.Month(), dt.Day(), 21, 50, 00));

esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
delay(100);
}
with this the external RTC would be set 10 min less. But also didn´t work.. after i did this i also measured a offset of about +8 minutes!?

so I wonder, how is the external RTC connected with the deepsleep!? for my understanding it shouldnt, because this are two different things. the timer for deepsleep is running in the esp and the RTC is running external.
Last edited by weicks on Fri Sep 28, 2018 7:46 am, edited 1 time in total.

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

Re: after Deep Sleep ESP32 - Time offset

Postby weicks » Fri Sep 28, 2018 7:40 am

I also tried an other rtc library now ..with the same result (RTClib)
so the problem must be somewhere else..

what i thought .. is that i set the clock in the setup() 10 minuts less .. so it would set the clock after the deepsleep back .. but i do not think this is the best solution

ESP_Sprite
Posts: 1930
Joined: Thu Nov 26, 2015 4:08 am

Re: after Deep Sleep ESP32 - Time offset

Postby ESP_Sprite » Fri Sep 28, 2018 9:12 am

You're just messing around now. These are the facts:
- Your DS1307 should keep the correct time at all times, with almost no drift.
- When the ESP32 is fully powered, its internal RTC keeps the correct time as well, with almost no drift,
- When the ESP32 is in deep sleep, its internal RTC drifts; in your case 10 mins over 9 hours but it's probably temperature and voltage dependent
- When in deep sleep, the ESP32 uses its internal RTC to wake up. Thus, the wake up time can be 'off'.

Note that the ESP32 by itself has no clue about the DS1370 connected to it. Unless your code fetches data from that chip explicitly, the ESP32 is oblivious to its existence. As the ESP32 does not run code during deep sleep, the DS1307 does absolutely nothing during that time.

The solution I propose:
- Set the wakeup delay you pass to esp_sleep_enable_timer_wakeup 20 minutes short of what you actually want.
- When you now wake up, it's going to be somewhat too early, and your ESP32 clock will be off by some amount. Fetch the (precise!) time from the DS1307, figure out how long you actually still need to sleep and go back to sleep.
- Because the 2nd time you go to sleep is way shorter than the 1st time, the error is way less, and you should wake up with only a few seconds offset.

Does that make sense?

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

Re: after Deep Sleep ESP32 - Time offset

Postby weicks » Fri Sep 28, 2018 9:32 am

yes, i try it today!

weicks
Posts: 7
Joined: Tue Sep 25, 2018 12:37 pm

Re: after Deep Sleep ESP32 - Time offset

Postby weicks » Mon Oct 01, 2018 5:46 am

today i found the solution. i have had to do the deep sleep at the second core of my esp. there only the wlan connection is executed. so i putted in the deepsleep and now it works! :)
thanks for your help ESP_Sprit!

Maybe someone can explain why it is like that?

Who is online

Users browsing this forum: No registered users and 6 guests