/*******************************/
/* VENDORS DEPENDENCIES */
/*******************************/
// Install library "Low-Power" made by Rocket Scream Electronics
// Install library "RTClib" made by Adafruit
// Install library "DS3231" made by Andrew Wickert
/*******************************/
/* CONFIGURATION VARIABLES */
/*******************************/
#define DEBUG // Uncomment to enable logs
/*****************************************************************************************/
/* DO NOT EDIT BELOW THIS LINE */
/*****************************************************************************************/
/*******************************/
/* Import dependencies */
/*******************************/
#include <time.h>
#include <RTClib.h>
#ifndef DEBUG
#include "LowPower.h"
#endif
/*******************************/
/* Global variables */
/*******************************/
RTC_DS3231 rtc;
/*******************************/
/* Setup function */
/*******************************/
void setup() {
Serial.begin(9600);
Serial.println(F("Booting up"));
// Assign pins
pinMode(LED_BUILTIN, OUTPUT);
// Turn LED on
digitalWrite(LED_BUILTIN, HIGH);
// Check RTC device is connected
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
Serial.flush();
while (1) delay(10);
}
// Update time on power lost or on first boot
if (rtc.lostPower()) {
Serial.println(F("RTC lost power. Set time to compilation one"));
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// 13 Dec 2023 18:00:00
// rtc.adjust(DateTime(2023, 12, 13, 18, 0, 0));
}
// Read today's date
time_t now;
DateTime utc = rtc.now();
// Display today's date
Serial.print(F("Now: "));
Serial.print(utc.day(), DEC);
Serial.print(F("/"));
Serial.print(utc.month(), DEC);
Serial.print(F("/"));
Serial.print(utc.year(), DEC);
Serial.print(F(" "));
Serial.print(utc.hour(), DEC);
Serial.print(F(":"));
Serial.print(utc.minute(), DEC);
Serial.print(F(":"));
Serial.println(utc.second(), DEC);
// Turn LED off
digitalWrite(LED_BUILTIN, LOW);
}
void _sleep(uint16_t sleepInSeconds) {
#ifdef DEBUG
delay(sleepInSeconds * 1000);
#else
if (sleepInSeconds & 0x01) LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
if (sleepInSeconds & 0x02) LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
if (sleepInSeconds & 0x04) LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
while (sleepInSeconds & 0xFFF8) {
sleepInSeconds = sleepInSeconds - 8;
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
#endif
}
void loop() {
Serial.println("> Loop");
// Turn LED ON
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
// Read current timestamp
time_t now;
DateTime utc = rtc.now();
#ifdef DEBUG
// Display today's date
Serial.print(F("Now: "));
Serial.print(utc.day(), DEC);
Serial.print(F("/"));
Serial.print(utc.month(), DEC);
Serial.print(F("/"));
Serial.print(utc.year(), DEC);
Serial.print(F(" "));
Serial.print(utc.hour(), DEC);
Serial.print(F(":"));
Serial.print(utc.minute(), DEC);
Serial.print(F(":"));
Serial.println(utc.second(), DEC);
#endif
// Turn LED OFF
digitalWrite(LED_BUILTIN, LOW);
#ifdef DEBUG
_sleep(10);
#else
// Wake up every 10 minutes
_sleep(6000);
#endif
}