Please someone explain me why the date on rtc isn't persisting after I wake up my NodeMCU from a sleep mode!
Code:
#include <Wire.h>
#include <ESP32Time.h>
#include <EEPROM.h>
ESP32Time rtc(0);
void setup() {
Serial.begin(115200);
Wire.begin();
EEPROM.begin(4096);
pinMode(12,INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(16, WAKEUP_PULLUP);
if(!EEPROM.read(500)){
//this if is to make me only update the date once. It's not working as intended
EEPROM.write(500,0);
rtc.setTime(EEPROM.read(0),EEPROM.read(1),EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5)+2000);
//to make things faster, instead of syncing with a NTP server, I saved a date on the memory
}
EEPROM.commit();
EEPROM.end();
}
void loop() {
volatile unsigned long currentMillis = millis();
volatile static unsigned long previousMillis = 0;
volatile static boolean luz = false;
if (currentMillis - previousMillis >= 10){
previousMillis = currentMillis;
if(!digitalRead(12)){
if(millis()%200<10){
if(luz){
digitalWrite(LED_BUILTIN, HIGH);
luz = !luz;
}
else{
digitalWrite(LED_BUILTIN, LOW);
luz = !luz;
}
}
if(millis()%3000<10){
EEPROM.begin(4096);
EEPROM.write(500,255);
delay(1);
EEPROM.commit();
EEPROM.end();
ESP.deepSleep(10e06);
delay(10);
}
}
else{
digitalWrite(LED_BUILTIN, LOW); //for some reason this line has the opposite logic
}
}
if(millis()%2000<1){
Serial.println(rtc.getTime("%Y %m %d %H %M %S"));
}
}
Notice how there is only one line that can set the date, which even being not called, the date isn't kept from the moments the node was awake.
Please help!