First, if this is not the right forum, please feel free to move this thread.
I need some help with interval timer setup and code. I have a garage monitor using photon and I want to send myself a notification if it is left open for more than X minutes.
The way I am trying to do is to use SparkIntervalTimer like the skeleton code below.
#include "SparkIntervalTimer/SparkIntervalTimer.h"
IntervalTimer statusCheckerTimer;
int checkStatus(String blah);
void setup()
{
...
// allocate getStatus to run every 500ms (1000 * .5ms period)
// statusCheckerTimer.begin(getStatus, 2000, hmSec);
}
void getStatus(void){
...
}
As soon as I compile it and flash it onto the device, the device goes offline. In order to do anything else with the device, I have to connect it to computer and re-install the firmware.
I am sure I am doing something wrong in my code, because device does not do that if I comment all of the timer code. Can anyone help this newbie please?
Thanks!
@cheekoo, I am assuming you are compiling on the web IDE. First, I would need to see your code to provide help. Second, SparkIntervalTimer was really designed to fire interrupts at defined intervals. Your requirement is more suited to using a simple millis() based timer or using Time.now() to do timing instead.
Thanks for the quick reply!
Yes - I am using web ide.
The way I was planning to implement it was -
void openDoorNotifier(void){
int status = checkStatus("blah");
if(status == 0){
if(previousStatus == "OPEN" || lastButPreviousStatus == "OPEN" ){
// send notification that it has been open for last 90 minutes!
shouldAlertVariable = 1;
Blynk.email("me@gmail.com", "Garage door has been open for last 90 minutes!", "Garage door has been open for last 90 minutes!");
}
lastButPreviousStatus = previousStatus;
previousStatus = "OPEN";
Blynk.virtualWrite(virtualPinPreviousStatus, previousStatus);
Blynk.virtualWrite(virtualPinLastButPreviousStatus, lastButPreviousStatus);
} else {
previousStatus = "CLOSED";
lastButPreviousStatus = "CLOSED";
Blynk.virtualWrite(virtualPinPreviousStatus, previousStatus);
Blynk.virtualWrite(virtualPinLastButPreviousStatus, lastButPreviousStatus);
shouldAlertVariable = 0;
}
}
Ok, so if I understand, you want to run openDoorNotifier every 500ms or so (3600000 hmSec = 500 minutes!!). The best and easiest way is to have a non-blocking timer in loop() that call openDoorNotifier() at that interval like this:
unsigned long openDoorNotifierTimer;
setup()
{
... //other code
openDoorNotifierTimer = millis();
}
loop()
{
...
if (millis() - openDoorNotifierTimer > 500) // or whatever value in milliseconds
{
openDoorNotifier();
openDoorNotifierTimer = millis();
}
Blynk.run();
}
We don’t support timers? Coming from Java background, I am so much used to ShceduledThreadPoolExecutor
“SparkIntervalTimer” sounded very exciting!
Nice! thanks for writing that. I think it is great!
The SparkIntervalTimer I had in the beginning was the wrong one. I have 2 in my program.
I still think intervalTimer would be best, as I want to check status every 30 minutes, and if it is open for 3 consecutive times, time to send notifications. We can write our own intervalTimer, like you suggested above, or we can re-use your amazing library - SparkIntervalTimer.
@cheekoo, the interrupt service routine you have does a lot of calls to Blynk functions. Having short ISR code is not only good practice but also wise. The current routine does not follow that practice IMO. What you can do is instead of the full code, simply set a flag that you test in loop() to indicate it’s time to run the code. Then simply reset the flag and wait for it again
I came here looking for a timing solution for my project. Which so happens to be a garage door opener too. I wanted to add a door open reminder as well. The best of all I don’t have to remove it to update the code! Thanks.
BTW - have you tried Blynk? I loved it, and dropped my initial idea of doing a webpage, as I was going to use it on my iphones only, and it was pretty easy to set it up, without having to manage my own webserver somewhere.
I did saw it in the article on Make: and even downloaded it. But the author mentioned that if you did not like the idea of have control of your garage in the cloud you could install it on your own server. I used MODX since I have familiarity with it and I could have more control.
For example I can create another user account and give them a new “virtual” key. No software to download and I can set restrictions for time of day or day of week.