You need to have a web app that can either call a Particle.function (which would set the value of myTime), or publish from the web app, and have your device subscribe to that event, and set myTime in the handler.
I found this code @ScruffR wrote in this post. Can i set values for hh and mm through a webapp. If you can site any examples where this has been done i could learn from that code.
You might look at the “Control LEDs over the 'net” example code in the Particle docs for an example of how to call a function with a web app. Instead of sending “on” or “off” as the arguments to the function, you would need to send something like “10:45”, and parse that in your device code to get the hour and minute values. I can’t offer much more advice, since I’m not a web programmer (I interact with my devices through native iOS apps).
Thank you for your help Ric. I have looked at the LED tutorials and even modified an app in angular for that purpose to call functions. I was looking for an example where this had been done so i could actually copy paste the code and play around with it until i figured out how to use it for my purpose.
In the code you posted above, you have the scheduleA function inside of loop() – it should be moved outside of loop. If you’re going to be setting the firing time for the alarm via a web call of a function, it would be better to setup the alarm in the callback to that function, rather than in setup(). Something like this,
int deviceA = D1;
void setup() {
pinMode(deviceA,OUTPUT);
digitalWrite(deviceA,LOW);
Time.zone(-8);
Time.setFormat("%I:%M%p %d/%m/%Y");
Particle.function("setAlarm", setupAlarm);
}
void loop() {
}
int setupAlarm(String cmd) {
char* stringArgs = strdup(cmd.c_str());
char* s = strtok(stringArgs, ":");
int hour = atoi (s);
int minute = atoi (strtok(NULL, ":"));
Alarm.alarmRepeat(hour, minute, 0, scheduleA); // use the hour, minute, second version of the repeating timer
return 1;
}
void scheduleA() {
Serial.println("print something");
digitalWrite(deviceA,LOW);
digitalWrite(deviceA,HIGH);
delay(15000);
if (deviceA == HIGH) {
Particle.publish("print something");
}else{
Particle.publish("print something");
}
}
The code in setupAlarm will parse a string like “10:45” into its two numeric parts and pass them to the alarmRepeat function. So, as long as you can figure out how to send a string like “10:45” via your web app you should be OK. If you’re still having problems with the web part, you should post that code, and someone with web programming experience should be able to help you out.
Thank you very much. I will try this out and let you know how it went. In any case I will post all the relevant code here once it’s working for anyone else out there who would want to do something similar.
One thing you definetly are missing in order to have TimeAlarm work is the regular call to Alarm.delay(); in loop() (as shown in the linked code).
What device is deviceA?
Since the LOW/HIGH transition in your scheduleA() will be rather short, you need a quite speedy device to catch that.
The delay(15000) on the other hand should be avoided in there, rather use Alarm.delay(15000); in loop().
How would “if (deviceA == HIGH)” ever be not true, with the preceeding digitalWrite(deviceA, HIGH);?
(and I guess you actually mean digitalRead(deviceA) since deviceA == D1 == 1 == HIGH will also never change - usually).
I’d not use strdup() as it does dynamic memory allocation (which I try to avoid where possible on embedded devices) but you already know the expected max length of your data, so I’d rather go for
char stringArgs[8];
strncpy(stringArgs, cmd.c_str(), 7);
...
return hour*100 + minute; // give the return value a useful meaning and report back what was set
BTW: Looking at the thread title, this seems to be a totally unrelated question.
Could you please open a new thread when duscussing other issues. This also makes it easier and more valuable for users with similar problems and might even save us some time by avoiding to answer similar questions over and over again.
Having said this, I’ll split this question off into a new thread (have done)
When you use the TimeAlarms library, you don't call regular delay if you want the alarm to fire--you have to call Alarm.delay(15000); instead to give the library a chance to figure out if it time for the alarm to fire.
I might suggest storing alarm settings in the EEPROM emulator so the setting doesn’t go away even after power cycle or OTA updates. It was handy in certain applications.