Compare displayed NTP time to reference to execute code
Posted: Wed Dec 14, 2022 9:20 pm
My NTP Clock project will be doing different things while displaying time, one task is to run code at a certain time. My full code at bottom. My displayed time is formatted like this "TIME:00:00:00", so I tried to use:
This code failed, with no serial output. It was a variable type mismatch. I converted the Time array to a String, the time comparison then worked.
Please excuse this post as I resolved it.
But this seems to be an easy way to convert a simple clock to a multi-function project that can do timed events, with no sleep requirement.
Code: Select all
if (Time == "TIME:19:52:00") {
Serial.println("Timer Event Activated !!"); //No output
}
This code failed, with no serial output. It was a variable type mismatch. I converted the Time array to a String, the time comparison then worked.
Code: Select all
void loop() {
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
Time[12] = second_ % 10 + 48;
Time[11] = second_ / 10 + 48;
Time[9] = minute_ % 10 + 48;
Time[8] = minute_ / 10 + 48;
Time[6] = hour_ % 10 + 48;
Time[5] = hour_ / 10 + 48;
Date[5] = month_ / 10 + 48;
Date[6] = month_ % 10 + 48;
Date[8] = day_ / 10 + 48;
Date[9] = day_ % 10 + 48;
Date[13] = (year_ / 10) % 10 + 48;
Date[14] = year_ % 10 % 10 + 48;
// Send time and date to serial monitor
Serial.println(Time);
Serial.println(Date);
//----------------------- Timed Event ---------------------------------
String strTime = String(Time);
if (strTime == "TIME:21:19:00") {
Serial.println("Timer Event Activated !!");
}
// Display time and date on the 16x2 LCD
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
last_second = second_;
}
delay(200);
}
Please excuse this post as I resolved it.
But this seems to be an easy way to convert a simple clock to a multi-function project that can do timed events, with no sleep requirement.