Deep Sleep Arduino IDE Amb82 mini

Hello everyone,
I’m currently developing a sketch to use the Deep Sleep mode to save power during the night in a prototype, and wake it up when the day starts. However, I’m facing issues that likely have a simple solution, but I haven’t been able to figure them out.
I’d like to share my work in order to ask for help or receive suggestions for possible improvements to the code. Below, I will present the complete code followed by my analysis.

SKETCH

#include “PowerMode.h”
#include “rtc.h”

#define INICIO_GRABACION 11
#define WAKEUP_SOURCE 2

#define TIMEOUT_MS 5000 // Tiempo máximo para recibir trama completa
uint8_t tiempo[6]; // segundos, minutos, horas, día, mes, año
uint8_t TimeOutCounter = 0;
uint8_t polling = 0;
bool Print = true;
long long seconds = 0;
struct tm *timeinfo;

int year = 2025;
int month = 5;
int day = 15;

int horitas = 19;
int minutitos = 28;
int segunditos = 30;
void Data_Time();
void RTC_Initializations();
bool RtcPoll();
void ImprimirTadosRtc();
void PollingConfirm();
void DeepSleepInit();
char estado = 0;

// set wake up RTC alarm as 5 seconds
#define ALARM_DAY 0
#define ALARM_HOUR 0
#define ALARM_MIN 0
#define ALARM_SEC 5
uint32_t PM_rtc_Alarm[4] = {ALARM_DAY, ALARM_HOUR, ALARM_MIN, ALARM_SEC};
#define WAKUPE_SETTING (uint32_t)(PM_rtc_Alarm)

void setup()
{
Serial.begin(115200);
IoConfig();
Serial.println(“/INICIANDO EL SISTEMA***/”);
rtc.Init();
ImprimirTadosRtc();
}

void loop()
{

if(!digitalRead(INICIO_GRABACION) && estado == 0){

    estado = 1;

}
if (estado == 1){

    PollingConfirm();

}

}
void Data_Time(){

seconds = rtc.Read();
timeinfo = localtime(&seconds);

horitas = timeinfo->tm_hour;
minutitos = timeinfo->tm_min;
segunditos = timeinfo->tm_sec;
day = timeinfo->tm_mday;
month = (timeinfo->tm_mon + 1);
year = (timeinfo->tm_year + 1900);

}
void RTC_Initializations(){

rtc.Init(); 
long long epochTime =rtc.SetEpoch(year, month, day, horitas, minutitos, segunditos);
rtc.Write(epochTime);

seconds = rtc.Read();
timeinfo = localtime(&seconds);

day = timeinfo->tm_mday;
month = (timeinfo->tm_mon + 1);
year = (timeinfo->tm_year + 1900);

horitas = timeinfo->tm_hour;
minutitos = timeinfo->tm_min;
segunditos = timeinfo->tm_sec;

}
bool RtcPoll() {
unsigned long startTime = millis();
int index = 0;
uint8_t buffer[7];

// Esperar hasta recibir al menos 7 bytes o que se agote el tiempo
while (millis() - startTime < TIMEOUT_MS) {
if (Serial.available()) {
buffer[index++] = Serial.read();

  if (index == 7) break;
}

}

if (index != 7) return false; // Timeout

// Validar encabezado
if (buffer[0] != ‘R’) return false;

// Copiar los valores
for (int i = 0; i < 6; i++) {
tiempo[i] = buffer[i + 1];
}

// Validar rangos
if (tiempo[0] > 59) return false; // segundos
if (tiempo[1] > 59) return false; // minutos
if (tiempo[2] > 23) return false; // horas
if (tiempo[3] < 1 || tiempo[3] > 31) return false; // día
if (tiempo[4] < 1 || tiempo[4] > 12) return false; // mes
if (tiempo[5] > 99) return false; // año

return true;
}
void ImprimirTadosRtc(){

Data_Time();

Serial.println(“/HORA:MINUTOS:SEGUNDOS***/”);
Serial.print(horitas);Serial.print(“:”); Serial.print(minutitos);Serial.print(“:”);Serial.println(segunditos);
Serial.println(“/DIS/MES/ANIO***/”);
Serial.print(day);Serial.print(“/”); Serial.print(month);Serial.print(“/”);Serial.println(year);

}
void PollingConfirm(){

if(RtcPoll() && TimeOutCounter < 5 && polling == 0) {

polling = 1;

} else if(polling == 1){

segunditos = tiempo[0];
minutitos = tiempo[1];
horitas = tiempo[2];
day = tiempo[3];
month = tiempo[4];
year = tiempo[5];

Serial.println("/*************DATOS RECIBIDOS CORRECTAMENTE****************/");
RTC_Initializations();
ImprimirTadosRtc();
Serial.println("/*************ENTRANDO EN DEEP SLEEP CON DATOS NUEVOS****************/");
DeepSleepInit();

}

else{
Serial.println(“Error: Trama inválida o timeout”);
Serial.println(“Numero de intentos:”);
Serial.println(TimeOutCounter + 1);
TimeOutCounter++;
if(TimeOutCounter == 2){
Serial.println(“Se alcanzo el limite de intentos”);
TimeOutCounter = 0;
Serial.println(“Se manitien los datos anteriormente cargados”);
RTC_Initializations();
ImprimirTadosRtc();
Serial.println(“/ENTRANDO EN DEEP SLEEP CON DATOS VIEJOS***/”);
DeepSleepInit();

}

}

}
void IoConfig(){

pinMode(INICIO_GRABACION,INPUT_PULLUP);

}
void DeepSleepInit(){

/*
uint32_t rtc_alarm[4];
rtc_alarm[0] = 0;
rtc_alarm[1] = 0;
rtc_alarm[2] = 0;
rtc_alarm[3] = 5; //SECONDS*/

PowerMode.begin(DEEPSLEEP_MODE, WAKEUP_SOURCE, 0, WAKUPE_SETTING);
PowerMode.start(year, month, day, horitas, minutitos, segunditos);

Serial.println("/*************HELLO****************/");

}
ANALYSIS

The sequence starts correctly: the RTC is queried, the data is loaded into variables, and the values are printed exactly as shown in the following screenshot.

It enters the void loop, and after receiving the signal from the push button, it begins waiting for data to be received via the serial port—or simply for the timeout to be exceeded, in which case it proceeds without updating the date and time values.

If the timeout is exceeded without receiving any data, the following result is obtained.

In the green box, you can see the data sent by the RTC when it is reconfigured with the same values it had from the beginning.

In the red box, you can see the same data read back from the RTC.

In the blue box, you can see when the methods PowerMode.begin() and PowerMode.start() are executed. The latter appears to initialize the RTC again using the same rtc.SetEpoch() and rtc.Write() methods, as it prints the same values except for the year—which I understand is the default initialization behavior of the RTC.

In the next image, you can see that the system restarts correctly 5 seconds later, which matches the expected behavior based on the configuration.

However, when new data is sent, it is received correctly, the RTC is updated without any issues, but when the updated values are passed to the PowerMode.start() method, the system does not restart—it becomes unresponsive.

Once again, I want to clarify that the goal of this code is to implement a way to modify the wake-up time from deep sleep based on the sunrise time, which changes throughout the year. Therefore, I need to be able to modify the variables passed to the PowerMode.start() method. Perhaps this is what’s causing the issue, although I’ve also tried using a constant value instead, and the behavior remains the same.
Thank you very much in advance to anyone who takes the time to read and respond.
Any suggestions, comments, or ideas to improve or solve the issue are greatly appreciated.
I’m looking forward to your support and guidance to better understand how to properly use the Deep Sleep feature on the AMB82-MINI.

Best regards,
Nicolás.