# RTC Light Timer Example?

**URL:** <https://community.particle.io/t/rtc-light-timer-example/32435>\
**Category:** General\
**Created:** [May 6, 2017, 5:52pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435 "2017-05-06T17:52:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learjet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/learjet/32/13894_2.png) [@Learjet](https://community.particle.io/u/Learjet)\
**Post date:** [May 6, 2017, 5:52pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/1 "2017-05-06T17:52:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 6, 2017, 7:04pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/2 "2017-05-06T19:04:46Z")

</div>

You may want to look into this

> [@Battery saving help](https://community.particle.io/t/battery-saving-help/32240/12):
>
> Actually I would not check for a specific hour alone. Imagine what is if your device wakes up between 3am and 9am for some reason? Then the device would not go back to sleep or if this happens at 2:59am, the device would wake again 59 minutes too late. Hence I’d do things slighty different Knowing that Time.local() returns the local date/time in seconds allows for easy range checks by use of the fact, that a day has 86400 seconds const int secGo2Sleep = 02\*3600 + 00\*60 + 00; // 00 minutes a…

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

---

<div class="post-metadata">

**Author:** ![Learjet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/learjet/32/13894_2.png) [@Learjet](https://community.particle.io/u/Learjet)\
**Post date:** [May 6, 2017, 9:08pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/3 "2017-05-06T21:08:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [May 6, 2017, 9:14pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/4 "2017-05-06T21:14:47Z")

</div>

> [@DailyTimerSpark library added](https://community.particle.io/t/dailytimerspark-library-added/22911):
>
> I spend a lot of time on presence simulation projects that do things like turning on/off various things in our home at seemingly random times. The classic example of this use case is an indoor or outdoor light that comes on and off randomly as to fool would be villains into believing someone is home. I developed a library for Arduino some time back and finally ported it to Particle. It uses the Time class in particle and you can set timers for WEEKENDS, WEEKDAYS, single days (e.g. WEDNESDAY) …

---

<div class="post-metadata">

**Author:** ![Learjet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/learjet/32/13894_2.png) [@Learjet](https://community.particle.io/u/Learjet)\
**Post date:** [May 7, 2017, 1:54am UTC](https://community.particle.io/t/rtc-light-timer-example/32435/5 "2017-05-07T01:54:37Z")

</div>

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:

```cpp
    //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

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [May 7, 2017, 5:23am UTC](https://community.particle.io/t/rtc-light-timer-example/32435/6 "2017-05-07T05:23:40Z")

</div>

> [@Learjet](#):
>
> DailyTimer timer1(1, 24, 1, 30, EVERY\_DAY, FIXED);

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.

---

<div class="post-metadata">

**Author:** ![Learjet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/learjet/32/13894_2.png) [@Learjet](https://community.particle.io/u/Learjet)\
**Post date:** [May 7, 2017, 3:57pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/7 "2017-05-07T15:57:19Z")

</div>

Good Lord…

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

Peace,  
EJ

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [May 7, 2017, 4:05pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/8 "2017-05-07T16:05:00Z")

</div>

> [@Learjet](#):
>
> Thanks Rik, that's probably the issue, I hadn't thought of Daylight Savings.

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()`.

---

<div class="post-metadata">

**Author:** ![Learjet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/learjet/32/13894_2.png) [@Learjet](https://community.particle.io/u/Learjet)\
**Post date:** [May 7, 2017, 4:51pm UTC](https://community.particle.io/t/rtc-light-timer-example/32435/9 "2017-05-07T16:51:11Z")

</div>

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
