#include <Wire.h>
#include <RTClib.h>
#include <esp_deep_sleep.h>
RTC_DS1307 rtc;
const int WAKEUP_PIN = 19; // SQW pin connected to D19 on ESP32
void setup() {
Serial.begin(9600);
delay(1000); // Allow time for serial monitor to initialize
pinMode(WAKEUP_PIN, INPUT_PULLUP); // Set the SQW pin as input with internal pull-up resistor
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
// Uncomment the next line if you want to set the RTC to the time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
printCurrentTime(); // Print current time before going to sleep
// Calculate the time to wake up after 10 seconds
DateTime now = rtc.now();
DateTime wakeup = now + TimeSpan(0, 0, 0, 10); // Add 10 seconds
// Set RTC alarm to wake up after 10 seconds
rtc.setAlarm1(wakeup, DS1307_A1_Second); // Set alarm to trigger every 10 seconds
// Enable alarm interrupt
rtc.enableAlarm(1);
// Sleep until the next alarm
esp_deep_sleep_enable_ext0_wakeup((gpio_num_t)WAKEUP_PIN, 0); // Wake up when SQW pin goes low
esp_deep_sleep_start();
}
void loop() {
// This code won't be executed as ESP32 goes into deep sleep after setup
}
void printCurrentTime() {
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}