Estoy tratando de usar el siguiente código. Pero en este caso, sleepMemory.wakeCount siempre es "uno" cada vez que mi contador se despierta de un sueño profundo. Intenté imprimir los valores en el monitor serie y descubrí que mi sleepMemory.wakeCount no se almacena en la memoria. ¿Puedes ayudarme por favor?
// NVM Data
#define RTCMemOffset 10 // arbitrary location
#define MAGIC_NUMBER 55 // used to know if the memory is good and been
written to
extern "C" {
#include "user_interface.h" // this is for the RTC memory read/write
functions
}
typedef struct
{
int wakeCount;
bool bFirstTime;
int magicNumber;
} nvmData;
nvmData sleepMemory;
bool bDoneSleeping;
void setup()
{
Serial.begin(115200);
Serial.setTimeout(2000);
Serial.println("Starts here");
bDoneSleeping = false;
if(sleepMemory.magicNumber != MAGIC_NUMBER) // memory got corrupt or
this is the first time
{
Serial.println("entered first loop");
sleepMemory.bFirstTime = true;
sleepMemory.magicNumber = MAGIC_NUMBER;
sleepMemory.wakeCount = 0;
bDoneSleeping = false;// EDIT: Set bDoneSleeping to true so
that the code runs on boot up
Serial.println("entered second loop");
sleepMemory.wakeCount += 1; // incrememnt the sleep counter
Serial.println("wakecount: ");
Serial.println(sleepMemory.wakeCount);
sleepMemory.bFirstTime = false;
}
if(sleepMemory.wakeCount >= 4) // reset the counter and do whatever
else you need to do since 4 hours have passed
{
sleepMemory.wakeCount = 0;
bDoneSleeping = true;
}
system_rtc_mem_write(RTCMemOffset, &sleepMemory, sizeof(sleepMemory));
if(bDoneSleeping)
{
// Perform tasks
Serial.println("perform task");
}
else{
Serial.println("going to sleep");
ESP.deepSleep(3600e6,WAKE_RF_DEFAULT);
}
}
void loop(){}