First of all, the time input scheduler works fine for my case, my problem is if I pressed button to stop relay during scheduler start & stop time (let’s say started at 10:00, stop at 10:10), it stopped right away, but after few seconds, relay turned on again because of stop time is not reached yet, I could not figure out how to exit scheduler and not allow relay turn on again, I did search the forum, maybe my key words and English issues, I did not find the similar topic to illustrate that, hope someone kindly give me a hint, thank you for reading my request.
Best regards
BLYNK_WRITE(V1) {
int Btn = param.asInt();
if (Btn == 1 && flag == 0) {
flag = 1;
relayON();
}
if (Btn == 0 && flag == 1) {
flag = 0;
relayOFF();
}
}
void activetoday() {
if (year() != 1970) {
Blynk.syncVirtual(V2);
}
}
BLYNK_WRITE(V2) {
sprintf(Date, " % 02d / % 02d / % 04d", day(), month(), year());
sprintf(Time, " % 02d: % 02d: % 02d", hour(), minute(), second());
TimeInputParam t(param);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
if (t.hasStartTime()) {
}
if (t.hasStopTime()) {
}
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
if (nowseconds >= startsecondswd) {
if (nowseconds <= startsecondswd + 90) {
ontime = 1;
if (ontime == 1 && flag2 == 0) {
flag2 = 1;
relayON();
}
}
}
else {
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
if (nowseconds >= stopsecondswd) {
if (nowseconds <= stopsecondswd + 90) { // 90s on 60s timer ensures 1 trigger command is sent
ontime = 0;
if (ontime == 0 && flag2 == 1) {
flag2 = 0;
relayOFF();
}
}
}
else {
if (nowseconds >= startsecondswd) {
}
}
}
else {
}
}
Try a virtual button that changes this flag2 to 0.
BLYNK_WRITE(V1)
{
if (param.asInt()) //this means if the app is 1 or (default on) it will trigger.
{
flag2 = 0;
}
}
You might also want to add a
Blynk.syncVirtual(V1);
in your Blynk_Connected function. I didn’t understand this one for a long time… If you have a Blynk_Connected function, it calls anything inside it once as soon as it connects. If you have a virtual sync it will take the app state and use that. In case the device has been restarted or something.
Now I am working on how to get rid of blynk notify when pressing button to stop scheduler. I put Blynk notify inside ontime = 0, no matter stop scheduler or not, Blynk.notify still send a notification to me.
Use another flag and if check. I usually set a timer that turns it back true after 1hrs so if the problem still exists in an hour it will notify me again.
But here is another problem, let’s say scheduler started at 08:00, stopped at 08:01, stopped scheduler by button at 08:00:30, and then setup new scheduler time from 08:01 to 08:02, scheduler won’t be started at 08:01. I am thinking about solution now.
If put reset flag in the time input, will ontime = 1 and relay on again right after scheduler reach stop time? Sometimes scheduler reaches stop time and relay turn off and then turn on relay again in very short time, seems like 100ms, then turn off finally.