Interfacing TimeAlarms with TimeZone libraries

Does anyone know how to interface the time alarms library with the TimeZone library so that the TimeAlarms go off at the time in the timezone for the user rather than the UTC time set in the system clock, further set by the NTP library.

Anyone? Anyone?

Bueller?

#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
#include <Timezone.h>

const char *ssid     = "***";
const char *password = "***";

WiFiUDP ntpUDP;

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "us.pool.ntp.org", 3600, 60000);

// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -300};    // Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -360};     // Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr;


void setup(){
 Serial.begin(115200);

 WiFi.begin(ssid, password);

 while ( WiFi.status() != WL_CONNECTED ) {
   delay ( 500 );
   Serial.print ( "." );
 }

 timeClient.begin();
}

void loop() {
 timeClient.update();

 setTime(timeClient.getEpochTime());

 
 time_t utc = now();
 time_t local = myTZ.toLocal(utc, &tcr);
 Serial.println();
 
 printDateTime(utc, "UTC");
 printDateTime(local, tcr -> abbrev);
 
 


 //Serial.println(timeClient.getFormattedTime());

 delay(1000);
}


// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
   char buf[32];
   char m[4];    // temporary storage for month string (DateStrings.cpp uses shared buffer)
   strcpy(m, monthShortStr(month(t)));
   sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
       hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
   Serial.println(buf);
}

The following was extracted from TimeAlarms.cpp. Notice how it uses UTC in the now() function. Also notice how ive included the paramArgs argument into the function so it can work with arguments, rather than being a static function. There is a setter above (not in view).

void TimeAlarmsClass::serviceAlarms()
{
  if (!isServicing) {
    isServicing = true;
    for (servicedAlarmId = 0; servicedAlarmId < dtNBR_ALARMS; servicedAlarmId++) {
      if (Alarm[servicedAlarmId].Mode.isEnabled && (now() >= Alarm[servicedAlarmId].nextTrigger)) {
        OnTick_t TickHandler = Alarm[servicedAlarmId].onTickHandler;
        if (Alarm[servicedAlarmId].Mode.isOneShot) {
          free(servicedAlarmId);  // free the ID if mode is OnShot
        } else {
          Alarm[servicedAlarmId].updateNextTrigger();
        }
        if (TickHandler != NULL) {
          (*TickHandler)(Alarm[servicedAlarmId].paramArgs);     // call the handler
        }
      }
    }
    isServicing = false;
  }
}

Bueller?

It's Ferris' day off this year.

If the Timezone library provides a method to convert a UTC time to a local time, I would find it difficult to believe that it doesn't provide a method to convert a local time to a UTC time.

Not that it matters, because the TimeAlarm class knows nothing about UTC time. It ONLY understands local time.

Notice how it uses UTC in the now() function.

It does NOT.

PaulS:
If the Timezone library provides a method to convert a UTC time to a local time

It does, here it is illustrated as in the Clock.ino example:

setTime(myTZ.toUTC(compileTime()));

But the alarms arent going off....

But the alarms arent going off....

Why are you setting the time on the Arduino to UTC time? Are all the clocks in your house set to UTC time?

Wouldn't it make more sense to set the time on the Arduino to local time?

Have you waited 24 hours to see if the alarms go off?

PaulS:
Are all the clocks in your house set to UTC time?

No

PaulS:
Wouldn't it make more sense to set the time on the Arduino to local time?

If I knew how I would.

PaulS:
Have you waited 24 hours to see if the alarms go off?

Ain't nobody got time for that, no pun intended.

I'm just gonna save myself the trouble. Alexa has a built in scheduler.

If I knew how I would.

setTime(compileTime());

Ain't nobody got time for that, no pun intended.

If you think that the alarm should go off at 2:00 PM, and you have a 7 hour difference between local and UTC time, then the alarm could actually go off at 9:00 PM. Or, 7:00 AM. You need to check both possibilities before you complain that the code doesn't work.

I'm just gonna save myself the trouble. Alexa has a built in scheduler.

Using that, you've learned nothing. If that's what you want, go right ahead.