ESP interrupt pin with DS3231

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() {

}

Hi

The ESP32 has an integrated RTC.
Use it to wake him up.

" ESP32 Deep Sleep with Arduino IDE and Wake Up Sources | Random Nerd Tutorials "

Hi, thank you, already did.
It's my actual setup but I want to evolve It using an alarm instead of a timer. The RTC timer Is set to microseconds and It manages unsigned longs who can go to barely one hour of sleep.
I thought It Is pretty pointless when aiming to wake up the mcu 2 or 3 times at most at precise Times of the day rather than using a constant interval (i.e. I don't need night time wakeups).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.