Help with setting Photon Schedules

I must not be looking at the same library, as I don’t see any examples.

If I could get some guidance using the library to accomplish what I just posted, I think I could take it from there.

The DailyTimerSpark looks interesting, but again, I’m not quite sure how to use it.

Thanks everyone.

1 Like

If you go to the library button in the Web IDE, and type Time into the search field, you should see one called TimeAlarms. If you click on that one, you should see the TimeAlarmsExample.ino example.

I understand the concept of “alarms” in that it is publishing information. However, I’m not sure how to adapt that to actions that happen at those given times.

I'm not sure what you mean by that. You create an alarm with a specific time, and a function that is executed when the alarm times out,

Alarm.alarmRepeat(17,45,10,onAlarm);  // Execute onAlarm at 5:45pm every day 
Alarm.alarmRepeat(18,45,10,offAlarm); // Execute offAlarm at 6:45pm every day 

void  loop(){}


void onAlarm(){
  // do whatever you want to happen at 5:45 PM  (like digitalWrite(led1, HIGH))        
}

void offAlarm(){
  // do whatever you want to happen at 6:45 PM  (like digitalWrite(led1, LOW))           
}
5 Likes

Thank you for all the help @Ric (I was unable to respond since I’m a noobie).

I’ve got alarms working. However, I do have a few questions.

  1. If the device goes offline and/or resets and one of the alarms is “active” (supposed to be on), is there a way to check that and activate?

  2. Is there a way to use sunrise/sunset? I’ve seen another library that can provide DST and do sunrise/sunset but was unable to get that functioning correctly.

  3. How would I accomplish the use of “Auto” mode and “Manual” mode (not to be confused with the default wifi settings) to indicate that in Auto mode the timers/alarms function and in Manual mode I can override the schedules by manually turning on/off the various functions and pins.

To give a little context, I am working on a Pool Controller and I would like to have the pumps, lights, etc run on a schedule, but be able to override that manually when needed.

Thanks again!

If by offline, you mean loses connection to the cloud, then that shouldn't affect your code if you use SYSTEM_THREAD(ENABLED). Your code should continue to run normally while the system tries to reconnect to the cloud. It looks like a TIme Alarm doesn't fire its handler if you miss the time it's supposed to turn on, so if you lose power when one of your relays should be on, it will be off instead. There are various ways around this problem ( a different kind of timer for instance, or saving the state of your relay in a retained variable), but if you're powering your device from a wall outlet, you shouldn't run into this problem very often.

You'd have to show us what you tried, and what results you got from using this library for us to help get that working. There are websites you can access with a webhook that will give you those times, or I have some code that calculates the sunrise and sunset times, so I don't need to go out to the web to get that info. I can share that with you if you want.

Several ways you could do this, but how are you going to interact with your device? Do you have an app, or a web page that allows you to send commands to your device? You could have a variable that you check in all the timer handlers that checks if you're in manual mode, in which case none of the code would get executed in that handler; the timers would still run, but they wouldn't do anything. You could then control the device by sending commands to a Particle.function.

1 Like

I would be very interested to see the code that allows you to calculate sunrise and sunset. I’m aware of pulling the data from an external source (i.e. weather underground), but need to figure out how to get it to work correctly. The DailyTimerSpark library has that capability but I was unable to get it working.

As for my way of interacting, right now I have some rudimentary ability to control via SmartThings, but I’m looking to move to a custom iOS app (another work in progress).

I wrote this library to solve exactly that problem

You can try this library for calculating sunset and sunrise times based on latitude and longitude

1 Like

@BulldogLowell, both of those look much cleaner (to my noobie eyes) than the previous libraries. I don’t recall seeing the DailyTimerParticle library in the Web IDE Library, just the older Spark version.

I’ll do my homework and see how to add from GitHub, which I’m learning is the best way to get up to date libraries anyway :slight_smile:

Thank you!

I never added the DailyTimerParticle library to the Particle list of libraries, mainly because I could not get that to work on my particular mac!

If you are using Particle Dev, you merely need to copy the two library files into the directory where your .ino project file is located.

If you are using the Web IDE you can add the two files using the + icon on the upper right hand side of the screen, it allows you to rename the files you want to add to your project.

Thank you. I didn’t know it was that easy. I will give that a try.

On a related note, how difficult would it be to control the timers (i.e. input the beginning/end times) remotely (i.e. iOS app) and have the data updated in the firmware?

Also, does the Timer library recognize the sunrise and sunset nomenclature?

Well, you’ll probably want to develop a Particle.function() to set the times and days.

Will you have a web front-end or just use the console?

At the moment I’m working on an iOS App, but a simple web app might be an easier place to start.

I'd be happy to help you with that, if you want. Just PM me.

Hi @Ric & @Exit2Studios

Have you finalized your code?

I tried to implement but not working.

In my test code, I send the timer value like ‘5000’ through Particle.function() to HIGH the led D7 after 5 seconds but it doesn’t work.
If I direct assign the value=5000; then it works. Why?

int value;
Timer t(value, action);

void setup()
{   
    pinMode(D7, OUTPUT);
    Particle.variable("value", value);
    Particle.function("status", status);
}

int status(String command)
{   
    t.start();
    value = command.toInt();
    return 0;
}

void action()
{
    digitalWrite(D7, HIGH);
}

void loop() {
}

Double posting is frowned upon in this community (and most others too).

1 Like

Yeah.

First time I did this.

1 Like

And hopefully last :wink:
Did my last post (in the other thread) help solve your issue?

A post was merged into an existing topic: Software Timers period value question?