Electron Schedule

I need some direction on a project I am working on. I already have code running on my Electron which monitors a couple of switches and temp sensor on my RV. What I want to do is set up a schedule that can be edited online. A schedule like from 8am to 6pm on Mon/Wed/Friday allow doors/windows to open/close no action, from 6:01pm to 7:59am notify if doors/windows open/close and temp is outside a certain range.

What is the best way to implement something like this?

Thanks for your help…

I made a library that allows you to create multiple schedules which would enable you to do this quite easily. You can modify the startTime and endTime, the active days, etc. using a Particle.function().

the library is here. The “documentation” is in the example.

but it is certainly not the only way to do this…

2 Likes

Thanks…I’ll give that a shot…

Have a question about the dailyimer.

What I was trying to do was set a start/stop time, then when the time expired set a new start/stop. The new start time worked OK but the stop time never happened. Here’s a small part of the code; So what I want to do is keep resetting the start/stop time when the previous time expires. Is this the best way to do this or is there a better way.

thanks

DailyTimer timer2(18,  30, 9,  30, EVERY_DAY, RANDOM_END, customSeedGenerator);

timer2.setRandomOffset(30, RANDOM_END);

bool timerState2 = timer2.isActive();  //State Change method this block
  if(timerState2 != timer2_LastState)
  {
    if(timerState2)
    {
           //
      digitalWrite(D5, HIGH);
      digitalWrite(D4, LOW);
    }
    else
    {
      digitalWrite(D5, LOW);

      //turn back on with new start/stop     
        timer2.setStartTime(10,00); 
        timer2.setEndTime(10,30);
        
     
    }
    timer2_LastState = timerState2;
  }

can confirm you are using this version of the library?

Maybe that is my issue…I’m using the current one called “DailyTimerSpark” I saw the name were different but I though the actual code was the same? I’m using it on an Electron!

thanks

OK, lemme know if you have any issues…

FYI it is refactored considerably

Seems to be working good, but I did find one issue.

I did a:
ledTimer1.setStartTime(8, 00); // set the start time
ledTimer1.setEndTime(8, 30); // and the end time

but the time of day was 9:00 when I reset the time

What happen was it turned on the LED but never turned it off.

I could see why it turn on (8:00) but should it have turned off (8:30). even though it was time of day 9:00?

I this something I have to handle in code myself?

Thanks

you found a bug!!

I updated the repository with the fix and added another example with a Particle function.

bool DailyTimer::sync()
{
  bool currentState = isActive(this);
  if(currentState && autoSync)
  {
    if(startTimeCallback)
      startTimeCallback();
  }
  else if(!currentState && autoSync)
  {
    if (endTimeCallback)
      endTimeCallback();
  }
  return state = currentState;
}

very nice that fixed the issue!

Another question I have is:

if I have multiple on/off times per day like:

on at 9 off at 10, on at 11 off at 12, etc…what is the best way to implement something like that,
right now I check in a off function and just reset the one timer to new on/off. That requires keeping a table/array of time but maybe that is the best way to do that or should I set multiple timers per day?

Great product!!

I thought about the opportunity to do that when I made the library.

I suppose I would, like you mentioned create an array of times and use the endTimeCallback() to cycle through that array and reset the times to the next bracket (not including random start/stop times from the conversation at this moment) .

That would require a persistent variable from which you could increment along the way, but that seems doable.

Maybe I could work on a way to do this using some library object. I’ll give it some thought.

thanks!

Have another question… which may be very obvious but it didn’t work exactly as I expected.

How to you setup a schedule that runs everyday for 24 hrs a day and never stops?

I tried setting start time to say 8:00 and i set end time to NULL. This rans but at some point ended.

What do you set end time too?

Thanks

I came across this before and started to add three methods:

myDailyTimer.manualOn() // run the start callback and maintain that "manual" state
myDailyTimer.manualOff() . // run the end callback and maintain that "manual" state
myDailyTimer.resume() . // resume the timer in its natural state

if you want to test that, I pushed it to github.

I tried a couple things but no luck on the way I tried.

  1. set start to 00:00 end to 23:59 it turned it off but never restarted
  2. set start to 08:00 end to 08:00 never turned on

The code to resume looks interesting is that in your github?

I guess the other way to do it is at end routine just reset the last start and end times.

Thanks

just pushed up…

testing i did and results…
Examples are for everyday.

It seems when I set:
ledTimer1.setStartTime(08:00, 00:00);
ledTimer1.setEndTime(11:00, 00:00);

and the actual time is say 7am (07:00)
this it not work, it ran my SystemOff subroutine when I set the above times
and never ran anything when the actual time reached 8am.

Now if the actual time is 09:00
this works.

So it looks like ledTimer1 start/end time has to be between the current time
to come on.
example: 9am is between 8am and 11am in the example above and works.

I also tried the ledTimer1.resume(); in the SystemOff subroutine but didn’t work either.

1 Like

are you sure you understand it correctly:

ledTimer1.setStartTime(08:00, 00:00);
ledTimer1.setEndTime(11:00, 00:00);

should be:

ledTimer1.setStartTime( 8, 0);  // start at 8:00am
ledTimer1.setEndTime( 0, 0);   // end at 12am

because it is working as expected when I test it

resume() is if you paused with manualOn or manualOff

I don’t know what SystemOff is…

Found a bug in my code… sorry…I’ll keeping testing but was able to get working.