Hello,
I am trying to wake up my ESP32 from deep sleep using a DS3231.
My code is all about storing several alarms into an array, detect the next alarm time once the MCU is awaken, set the next alarm and then go back to sleep.
I got the alarm detection function working but I am probably missing something in the (apparently) easiest part of the sketch, which the interrupt management.
I both tried the attachInterrupt function and the esp syntax (which needs declaring wakeup sources for the ESP32).
If I understand well, the interrupt pin should be kept high with the pullup resistor in order to avoid floating and then it should receive an interrupt from the INT pin of the DS3231 sending it to low.
Any help to figure out what am I missing would be much appreciated.
My code is below.
Thanks
Theo
#include <Wire.h>
#include <ESPmDNS.h>
#include <Arduino.h>
#include "time.h"
#include <string.h>
#include <RTClibExtended.h>
#define ledpin 2
#define wakepin 23
RTC_DS3231 RTC;
void managealarm(){
//pulisci allarmi
RTC.armAlarm(1, false);
RTC.clearAlarm(1);
RTC.alarmInterrupt(1, false);
RTC.armAlarm(2, false);
RTC.clearAlarm(2);
RTC.alarmInterrupt(2, false);
RTC.writeSqwPinMode(DS3231_OFF);
//studio sia i delta positivi che negativi. i delta positivi ci sono se esiste una sveglia oggi più avanti di ora, negative sono tutte quelle già passate
//minpos è tutta la roba in avanti, max neg tutta la roba indietro
int minpos = 86400; //24 ore
int maxneg = 0;
int nextal=0;
int delta=0;
//{al1 hours, al1 minutes, al2 hours, al2 minutes, al3 hours....
int myalarms[]={23,46,14,44};
//time_t variable type, which is the number of seconds elapsed since 1970
DateTime now= RTC.now();
for (int i=0;i<4; i=i+2){
int alhrs = int(myalarms[i]);
int almins = int(myalarms[i+1]);
DateTime sveglia(now.year(),now.month(),now.day(),alhrs,almins,0);
delta = sveglia.unixtime()-now.unixtime();
if(delta>0){
minpos=min(minpos,delta);
}
else{
maxneg=max(maxneg,abs(delta));
}
// ho finito i confronti
if (minpos == 86400){
nextal=86400-maxneg;
}
else{
nextal=minpos;
}
Serial.println(now.unixtime());
Serial.println(nextal);
nextal=now.unixtime()+nextal;
Serial.println(nextal);
}
DateTime setnextalarm (nextal);
RTC.setAlarm(ALM1_MATCH_HOURS,setnextalarm.minute(),setnextalarm.hour(),0);
RTC.armAlarm(1, true);
RTC.alarmInterrupt(1, true);
digitalWrite(ledpin,HIGH);
delay(5000);
digitalWrite(ledpin,LOW);
esp_deep_sleep_start();
}
void setup()
{
Serial.begin(115200);
delay(1000);
pinMode(ledpin, OUTPUT);
pinMode(wakepin, INPUT_PULLUP);
Wire.begin();
if (!RTC.begin()){
Serial.println("RTC non trovato");
}
//RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
//RTC.adjust(DateTime(2014, 1, 21, 3, 0, 0));
esp_sleep_enable_ext0_wakeup(GPIO_NUM_23,0);
RTC.writeSqwPinMode(DS3231_OFF);
managealarm();
}
void loop() {
}