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.
Can anyone help figure out what the problem is?
#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() {
}
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.
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.
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.
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) or RTCAlarm.ino (RTC – Simple RTC Alarm – Realtek IoT/Wi-Fi MCU Solutions) for guide on how to use them.
Thank you.
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.
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.