Best practice for using RTC to trigger events on Arduino

Hey, Im new to Arduino and am trying to trigger events based on predetermined real world time, much like an alarm clock.

I am using the RTC.h library and am able to poll pool.ntp.org and adjust for the real time in my time zone.

I am currently having issues triggering event in the bool alarm() function,

I am also interested in any time related feedback. expecially any issues others have come across with use of the loop function and failover/restart protocols.

thanks for the help

#include "RTC.h"
#include <NTPClient.h>
#include <WiFi.h>

const char* ssid     = "*****";                // network SSID (name)
const char* password = "******";       // network password

int wifiStatus = WL_IDLE_STATUS;
WiFiUDP Udp;
NTPClient timeClient(Udp);


void connectToWiFi(){
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (wifiStatus != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    wifiStatus = WiFi.begin(ssid, password);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println("Connected to WiFi");
  printWifiStatus();
}

void setup()
{

  pinMode(A0, OUTPUT); // pin i want to output event to
  Serial.begin(115200);
  while(!Serial);

  connectToWiFi();
  RTC.begin();

  Serial.println("\nStarting connection to server...");
  timeClient.begin();
  timeClient.update();

  auto timeZoneOffsetHours = 10.5;
  auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
  Serial.print("Unix time = ");
  Serial.println(unixTime);
  RTCTime timeToSet = RTCTime(unixTime);
  RTC.setTime(timeToSet);

  // Retrieve the date and time from the RTC and print them
  RTCTime currentTime;
  RTC.getTime(currentTime); 
  Serial.println("The RTC was just set to: " + String(currentTime));
}

bool alarm(){
   // what do i put here??
}

void loop()
{
  RTCTime currentTime;
  char* AlarmTime = "2025-01-15T15:27:38";
  RTC.getTime(currentTime);

  if(alarm() = ture){
    Serial.println("Alarm time");
    DigitalWrite(A0, HIGH)
    }
  Delay (1000)
 }

Please read your code, try to compile it, and ask questions about the errors that are thrown.

There are too many things to comment on, but I’d suggest starting with basic examples in the IDE.

Learn to run before you try to fly.

1 Like

Only one function named setup is allowed in an Arduino program.

After reviewing the Arduino IDE basic examples, one place to start would be the RTC.h library examples, and learn how to use it to do something useful, like setting and responding to an alarm time, before integrating anything else into the code.

One step at a time!

I wouldn't say that you "having issues" with the function since you even not tried to put anything in it.

P.s. Please do not edit your messages if other users already answered to it. It is not polite.

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