RTC Light Timer Example?

If you would be so kind does anyone know of a simple timer example that you can point me to? I’ve tried quite a few examples and haven’t had any luck because the are all older.

What I’m trying to do is pretty simple, turn on a light every day at 6AM and turn it off at 10PM using RTC. Thanks so much for your help!

Peace,
EJ

You may want to look into this

You don't need to use System.sleep() but can do anything you want in that true or false branch.

Thanks for that Scruff I didn’t think of that. The system is never asleep as I have it doing things pretty much 24/7. Would you recommend that I use local time that is synced every 24 hours or so?

Thanks again!

Peace,
EJ

3 Likes

Thansk for responding Lowell, I was working with your library for a few hours last night and didn’t have much luck. I love the concept of the library, it’s priceless for home security. Basically I was trying to get the D7 onboard LED to turn on and off and couldn’t figure out what I was doing wrong, here’s my code:

    //Program Start
    #include "DailyTimerSpark.h"
    #define SUN_UPDATE_INTERVAL 30*60*1000UL

    const char* cityLocation = "Princeton";  //City for my Photon
    const char* stateLocation = "NJ";     // State for my Photon

    char publishString[125] = "";
    char timeVars[125] = "";

    struct deviceTime{
    int Hour;
    int Minute;
    };

    deviceTime sunset  = {18,30};
    deviceTime sunrise = { 5,30};

    DailyTimer timer1(1, 24, 1, 30, EVERY_DAY, FIXED);      // optional callback function for random number generation, see below example
    DailyTimer timer2(12, 00, 13,  0, SATURDAY, FIXED);                                                    
    // default is FIXED, this will randomize the start time only
    DailyTimer timer3( 8, 30, 23, 30, WEEKENDS);                                                           
    // creates with a default fixed start time and end time
    bool timer1_LastState = false;
    bool timerState;
    uint32_t lastUpdateTime = 0;
    uint32_t lastSunTime = 0;

    void setup()
    {
    timer3.begin(); // use this for syncing the state change tools startTrigger() and endTrigger() on startup
    timer2.begin();
    timer1.begin();
    Serial.begin(9600);
    pinMode(D7, OUTPUT);
    strcpy(publishString, "{\"my-city\": \"");
    strcat(publishString, cityLocation);
    strcat(publishString, "\", \"my-state\": \"");
    strcat(publishString, stateLocation);
    strcat(publishString, "\" }");
    Particle.function("ToggleLED", toggleLED);
    Particle.subscribe(System.deviceID(), webhookHandler, MY_DEVICES);
    Particle.variable("State", timerState);
    Particle.variable("SunTime", timeVars, STRING);
    timer1.setRandomOffset(45, RANDOM_END);
    //timer1.setDaysActive(WEEKDAYS);             // Set the timer to be active on weekdays
    //timer1.setDaysActive(B10101010);            // or define a custom day mask... 
    SMTWTFS0  example: Sunday, Tuesday, Yhursday, Saturday
    //timer1.setRandomDays(4);                    // or four random days per week
    //timer3.setRandomOffset(5);                  // Change random start time, +/- 5 minutes... max 59 mins, default is 15mins
    //Serial.println(timer1.getDays(), BIN);      // getDays() returns active days as a byte in the format above
    Particle.publish("pushover", "Timer Restart", 60, PRIVATE);
    Particle.publish("sun_time", publishString, 60, PRIVATE);
    }
    void loop()
    {
     timerState = timer1.isActive();
     if(timerState != timer1_LastState)
     {
     if(timerState)
     {
    digitalWrite(D7, HIGH);
    Serial.println("LED is ON");
    Particle.publish("pushover", "LED is ON", 60, PRIVATE);
    }
    else
    {
      digitalWrite(D7, LOW);
      Serial.println("LED is OFF");
      Particle.publish("pushover", "LED is OFF", 60, PRIVATE);
    }
    timer1_LastState = timerState;
    }
    if(timer2.startTrigger())
    {
    Serial.println("Timer 2 FIRED!");
    Particle.publish("pushover", "Timer2 Fired", 60, PRIVATE);
    }
    if(timer3.startTrigger())
    {
    Serial.println("Timer 3 FIRED!");
    Particle.publish("pushover", "Timer3 Fired", 60, PRIVATE);
    }
    if(timer3.endTrigger())
    {
    Serial.println("Timer 3 Expired!");
    Particle.publish("pushover", "Timer3 is Inactive", 60, PRIVATE);
    }
    if(millis() - lastUpdateTime > 1000UL)
    {
    char timeBuffer[32] = "";
    Serial.println(Time.timeStr());
    lastUpdateTime += 1000UL;
    }
    if(millis() - lastSunTime > SUN_UPDATE_INTERVAL)
    {
    Particle.publish("sun_time", publishString, 60, PRIVATE);
    lastSunTime += SUN_UPDATE_INTERVAL;
    }
    }
    uint32_t customSeedGenerator()
    {
    Serial.println("New Seed Created");
    return  random(0xFFFFFFFF);
    }
    void webhookHandler(const char *event, const char *data)
    {
    if (strstr(event, "sun_time"))
    {
    gotSunTime(event, data);
    }
    }
    void gotSunTime(const char * event, const char * data)
    {
    deviceTime currentTime;
    char sunriseBuffer[25] = "";
    strcpy(sunriseBuffer, data);
    sunrise.Hour = atoi(strtok(sunriseBuffer, "\"~"));
    sunrise.Minute = atoi(strtok(NULL, "~"));
    sunset.Hour = atoi(strtok(NULL, "~"));
    sunset.Minute = atoi(strtok(NULL, "~"));
    currentTime.Hour = atoi(strtok(NULL, "~"));
    currentTime.Minute = atoi(strtok(NULL, "~"));
    Time.zone(0);
    Time.zone(utcOffset(Time.hour(), currentTime.Hour));
    timer1.setStartTime(sunset.Hour, sunset.Minute);
    //timer1.setEndTime(sunrise.Hour, sunrise.Minute);
    char buffer[125] = "";
    sprintf(buffer, "Sunrise=%02d:%02d, Sunset=%02d:%02d", sunrise.Hour, 
    sunrise.Minute, sunset.Hour, sunset.Minute);
    strcpy(timeVars, buffer);
    }
    int utcOffset(int utcHour, int localHour)  // sorry Baker Island, this won't work for you (UTC-12)
    {
    if (utcHour == localHour)
    {
    return 0;
    }
    else if (utcHour > localHour)
    {
    if (utcHour - localHour >= 12)
    {
    return 24 - utcHour + localHour;
    }
    else
    {
    return localHour - utcHour;
    }
    }
    else
    {
    if (localHour - utcHour > 12)
    {
    return  localHour - 24 - utcHour;
    }
    else
    {
    return localHour - utcHour;
    }
    }
    }
    int toggleLED(String command)
    {
    if (command == "1")
    {
    digitalWrite(D7, HIGH);
    }
    else if (command == "0")
    {
    digitalWrite(D7, LOW);
    }
    else if(command == "-1")
    {
    digitalWrite(D7, !digitalRead(D7));
    }
    char message[125] = "";
    sprintf(message, "LED is %s", digitalRead(D7)? "ON" : "OFF");
    Particle.publish("pushover", message, 60, PRIVATE);
    return digitalRead(D7);
    }

The code verified and I thought that I had my code formatted properly?

DailyTimer timer1(1, 24, 1, 30, EVERY_DAY, FIXED);

Thanks for your patience.

Peace,
EJ

Your timer is set to start at 1:24 AM, are you testing at that time, or are you accounting for the offset from UTC? If I set my time zone, and set the timer to a time a few minutes into the future, I see the D7 LED go on and off as expected with your code.

2 Likes

Good Lord…

Thanks Rik, that’s probably the issue, I hadn’t thought of Daylight Savings.

Peace,
EJ

the example you posted includes a web hook for obtaining your Time Zone.

but for simple testing, you could just use Time.zone() in setup().

Like an idiot, I never set the Time Zone on my device so I tested it at UTC and it worked just like it should, thanks for your patience!

Peace,
EJ