# BW16 Get RTC time with Deep Sleep

**URL:** <https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666>\
**Category:** Peripherals\
**Tags:** evb-bw16, rtl872xd\
**Created:** [February 5, 2024, 6:10am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666 "2024-02-05T06:10:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeeshan104](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/zeeshan104/32/1922_2.png) [@zeeshan104](https://forum.amebaiot.com/u/zeeshan104)\
**Post date:** [February 5, 2024, 6:10am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/1 "2024-02-05T06:10:31Z")

</div>

Hello everyone, I am using BW16 (RTL7820DN with Arduino Core). I want to wake up my BW16 with RTC and also get the epoch time from RTC. I have a little program. What I am doing here is setting the initial time of RTC with the NTP server (just one time). Then read the time and go to sleep for one minute.  
The problem is that epoch time is not changing. Check out the image.

 ![RTC_Prob](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/7/7dfec9d9c203e966e85c84ee139e8e38dec52610.png)

Can anyone help figure out what the problem is?

```auto
#include <sntp/sntp.h>
#include <rtc_api.h>
#include <WiFi.h>
#include <PowerSave.h>
long s = 0, us = 0;
unsigned int tick = 0;
#include <Preferences.h>
Preferences pr;
#define AON_TIMER_SLEEP_DURATION 300000
void setup() {
  pr.begin("initial", false);
  boolean p = pr.getBool("rtcinit");
  pr.end();
  Serial.begin(115200);
  Serial.println("Connecting to WiFi");
  WiFi.begin("------", "---------");
  while (WiFi.status() != WL_CONNECTED) {
    delay(10);
  }
  Serial.println("connected to wifi");
  if (p == false) {
    Serial.println("Initializing RTC");
    sntp_init();
    while (s <= 0) { //wait for time update
      sntp_get_lasttime(&s, &us, &tick);
    }
    Serial.print("Initial epoch time to init RTC : "); Serial.println(s + (us / 1000000));
    rtc_init();
    rtc_write(s + (us / 1000000));
    pr.begin("initial", false);
    pr.putBool("rtcinit", true);
    pr.end();
  }

  long epoch = rtc_read();
  Serial.print("RTC epoch time : "); Serial.println(epoch);
  PowerSave.DS_RTC_WAKEUP();
  PowerSave.RTCWakeSetup(0, 0, 1, 0); // 1 min sleep
  //PowerSave.DS_AON_TIMER_WAKEUP();
  //PowerSave.AONTimerDuration(AON_TIMER_SLEEP_DURATION);
  Serial.println("Going to sleep");
  PowerSave.enable();

}

void loop() {

}

```

---

<div class="post-metadata">

**Author:** ![Kelvin\_Huang](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/kelvin_huang/32/2130_2.png) [@Kelvin\_Huang](https://forum.amebaiot.com/u/Kelvin_Huang)\
**Post date:** [February 5, 2024, 8:05am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/2 "2024-02-05T08:05:49Z")

</div>

Hi @zeeshan104 ,

Please refer to this example [ambd\_arduino/Arduino\_package/hardware/libraries/NTPClient/examples/Advanced/Advanced.ino at dev · ambiot/ambd\_arduino · GitHub](https://github.com/ambiot/ambd_arduino/blob/dev/Arduino_package/hardware/libraries/NTPClient/examples/Advanced/Advanced.ino). Instead of using SNTP, you can consider using NTP to get the real time update.

Thank you.

---

<div class="post-metadata">

**Author:** ![zeeshan104](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/zeeshan104/32/1922_2.png) [@zeeshan104](https://forum.amebaiot.com/u/zeeshan104)\
**Post date:** [February 5, 2024, 9:09am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/3 "2024-02-05T09:09:30Z")

</div>

Hello @Kelvin_Huang. Thank you for your reply. Getting time from ntp or sntp is not the problem. The problem is the RTC.  
Look at the image that I shared. initially, I updated RTC time from SNTP time. And go for one minute of sleep. After waking up, I did not get RTC time.

---

<div class="post-metadata">

**Author:** ![Kelvin\_Huang](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/kelvin_huang/32/2130_2.png) [@Kelvin\_Huang](https://forum.amebaiot.com/u/Kelvin_Huang)\
**Post date:** [February 6, 2024, 2:25am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/4 "2024-02-06T02:25:02Z")

</div>

Hi @zeeshan104 ,

Your sntp\_get\_lasttime(&s, &us, &tick); is in the set up loop. The code only runs once.  
The value of s and us do not get updated.  
Also, the RTC epoch time seems wrong. It is 12 years ahead of current year.

The reason why I suggested using NTPClient on the Arduino is because there is an existing working example to get the right date and time with high precision. SNTP updates at a slower rate compared to NTP. After that you can use the rtc\_write value to write the actual time when it wakes up.

Thank you.

---

<div class="post-metadata">

**Author:** ![zeeshan104](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/zeeshan104/32/1922_2.png) [@zeeshan104](https://forum.amebaiot.com/u/zeeshan104)\
**Post date:** [February 6, 2024, 4:18am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/5 "2024-02-06T04:18:59Z")

</div>

Hello @Kelvin_Huang,  
I am not interested in ntp or sntp. Let’s say I initialize manually the RTC say `rtc_write(1707192100)`. And then processor sleeps for let’s say 100 seconds. Next time the processor wakes up and I read the RTC with `read_rtc()`. it should be the 1707192200 (forget about the code execution time). Am I right or missing something.  
Is there any example?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Kelvin\_Huang](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/kelvin_huang/32/2130_2.png) [@Kelvin\_Huang](https://forum.amebaiot.com/u/Kelvin_Huang)\
**Post date:** [February 6, 2024, 6:02am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/6 "2024-02-06T06:02:14Z")

</div>

Hi @zeeshan104 ,

I see, then you are using the wrong API on the arduino level.

Please refer to RTC.ino ([RTC – Simple RTC – Realtek IoT/Wi-Fi MCU Solutions](https://www.amebaiot.com/en/amebad-arduino-rtc/)) or RTCAlarm.ino ([RTC – Simple RTC Alarm – Realtek IoT/Wi-Fi MCU Solutions](https://www.amebaiot.com/en/amebad-arduino-rtc-alarm/)) for guide on how to use them.

Thank you.

---

<div class="post-metadata">

**Author:** ![zeeshan104](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/zeeshan104/32/1922_2.png) [@zeeshan104](https://forum.amebaiot.com/u/zeeshan104)\
**Post date:** [February 6, 2024, 6:17am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/7 "2024-02-06T06:17:14Z")

</div>

Hi @Kelvin_Huang, I am going to see the PowerSave Arduino Library. And RTC lib as well. I think, the library reinitializes the RTC time every time the processor wakes up. or maybe another problem I do not know. Thanks for your time.

---

<div class="post-metadata">

**Author:** ![Kelvin\_Huang](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/kelvin_huang/32/2130_2.png) [@Kelvin\_Huang](https://forum.amebaiot.com/u/Kelvin_Huang)\
**Post date:** [February 6, 2024, 7:13am UTC](https://forum.amebaiot.com/t/bw16-get-rtc-time-with-deep-sleep/2666/8 "2024-02-06T07:13:58Z")

</div>

Hi @zeeshan104,

Your rtc\_read() need to be in the loop() to be updating from your initial time.  
It will start counting from the time you initialised which is from your rtc\_write().

You can give the RTC.ino a try and you will understand the behavior.

Thank you.
