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)
}