//////////////////////////////////////////////////////////////////////////////
// check if end time is received convert and save it as day time in seconds //
//////////////////////////////////////////////////////////////////////////////
if (t.hasStopTime())
{
Serial.println(String("Stop: ") +
t.getStopHour() + ":" +
t.getStopMinute() + ":" +
t.getStopSecond());
Serial.println(String("Stop in sec: ") + param[1].asLong() + String(" for switch") + switch_no + String(" time_no: ") + time_no);
active_duration[switch_no][time_no] = (param[1].asLong()-start_time_sec[switch_no][time_no]);
// if end time is smaller than start time this means end time is at next day
if (active_duration[switch_no][time_no]<0) active_duration[switch_no][time_no]=86400+active_duration[switch_no][time_no];
Serial.println(String("Stop duration: ") + active_duration[switch_no][time_no]);
end_valid[switch_no][time_no] = true;
}
else // if end time is not defined //
{
// Do nothing
Serial.println(String("No Stop Time Given for switch: ") + switch_no + String(" time_no: ") + time_no);
end_valid[switch_no][time_no] = false;
}
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
for (int i = 1; i <= 7; i++) {
if (t.isWeekdaySelected(i)) {
Serial.println(String("Day ") + i + " is selected");
weekdays[switch_no][time_no][i] = true;
}
else {
weekdays[switch_no][time_no][i] = false;
}
}
Serial.println();
}
//this reads status of on/off mode and displays on screen
BLYNK_WRITE(V50) { int Vpin50 = param.asInt();
auto_off = Vpin50;}
// V0..V3 for switch 0, V4..V7 for switch 1 ...
BLYNK_WRITE(V0) { set_time(param, 0,0); }
BLYNK_WRITE(V1) { set_time(param, 0,1); }
BLYNK_WRITE(V2) { set_time(param, 0,2); }
BLYNK_WRITE(V3) { set_time(param, 0,3); }
BLYNK_WRITE(V4) { set_time(param, 1,0); }
BLYNK_WRITE(V5) { set_time(param, 1,1); }
BLYNK_WRITE(V6) { set_time(param, 1,2); }
BLYNK_WRITE(V7) { set_time(param, 1,3); }
BLYNK_WRITE(V8) { set_time(param, 2,0); }
BLYNK_WRITE(V9) { set_time(param, 2,1); }
BLYNK_WRITE(V10) { set_time(param, 2,2); }
BLYNK_WRITE(V11) { set_time(param, 2,3); }
BLYNK_WRITE(V12) { set_time(param, 3,0); }
BLYNK_WRITE(V13) { set_time(param, 3,1); }
BLYNK_WRITE(V14) { set_time(param, 3,2); }
BLYNK_WRITE(V15) { set_time(param, 3,3); }
// use a slider to define default activation duration (slider count in minute)
BLYNK_WRITE(V16) { active_duration[0][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V17) { active_duration[1][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V18) { active_duration[2][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V19) { active_duration[3][TIME_CNT] = param.asInt()*60; }
// use a slider to define default max duration (slider count in minute) ***HARD CODE THIS*** NL
BLYNK_WRITE(V24) { max_duration[0] = param.asInt()*60; }
BLYNK_WRITE(V25) { max_duration[1] = param.asInt()*60; }
BLYNK_WRITE(V26) { max_duration[2] = param.asInt()*60; }
BLYNK_WRITE(V27) { max_duration[3] = param.asInt()*60; }
/////////////////////////////////////////////////////////////////
// Handle switch events (from app or from scheduler ) //
/////////////////////////////////////////////////////////////////
// turn off switch after active duration ends
// duration number is not important here
void turn_off_switch_no_0(){ turn_on_off(0,0,0); Blynk.virtualWrite(V20,0); Serial.println(String("timer turn off switch 0 ") );}
void turn_off_switch_no_1(){ turn_on_off(0,1,0); Blynk.virtualWrite(V21,0); Serial.println(String("timer turn off switch 1 ") );}
void turn_off_switch_no_2(){ turn_on_off(0,2,0); Blynk.virtualWrite(V22,0); Serial.println(String("timer turn off switch 2 ") );}
void turn_off_switch_no_3(){ turn_on_off(0,3,0); Blynk.virtualWrite(V23,0); Serial.println(String("timer turn off switch 3 ") );}
// handle switch state
void turn_on_off(int on_off, byte switch_no , byte time_no){
long active_duration_ms ;
char Time_print[16];
if ((on_off==1) && (auto_off == 1)) //auto_off is a slider in app to shut off the program
{
// create time as string to print on activation butten
sprintf(Time_print, "%02d:%02d", hour(), minute());
// turn on the switch (or off if default is on)
digitalWrite(switch_pins[switch_no],!switch_default[switch_no]);
// if end time is valid use the active duration assigned to this time
// (change to msec will be done later)
if (end_valid[switch_no][time_no])
active_duration_ms = ((long)active_duration[switch_no][time_no]);
else // otherwise use the general time duration
active_duration_ms = ((long)active_duration[switch_no][4]);
// max duration smaller than two min is not valid
if ( (max_duration[switch_no]< 120) | (max_duration[switch_no]>active_duration_ms) )
active_duration_ms = active_duration_ms*1000;
else
active_duration_ms = ((long)max_duration[switch_no])*1000;
// if new timer is set before another one ended then disable previous timer
if (end_timer_id[switch_no]!=32) timer.deleteTimer(end_timer_id[switch_no]);
// turn on switch and set timer
switch (switch_no) {
case 0:
Blynk.setProperty(V20, "onLabel", String(Time_print));
Blynk.virtualWrite(V20,1);
end_timer_id[0]=timer.setTimeout(active_duration_ms, turn_off_switch_no_0);
break;
case 1:
Blynk.setProperty(V21, "onLabel", String(Time_print));
Blynk.virtualWrite(V21,1);
end_timer_id[1]=timer.setTimeout(active_duration_ms, turn_off_switch_no_1);
break;
case 2:
Blynk.setProperty(V22, "onLabel", String(Time_print));
Blynk.virtualWrite(V22,1);
end_timer_id[2]=timer.setTimeout(active_duration_ms, turn_off_switch_no_2);
break;
case 3:
Blynk.setProperty(V23, "onLabel", String(Time_print));
Blynk.virtualWrite(V23,1);
end_timer_id[3]=timer.setTimeout(active_duration_ms, turn_off_switch_no_3);
break;
}
Serial.println(String("turn ON switch: ") + switch_no + String(" for duration: ") + active_duration_ms/60000 + String("min "));
}
else
{
digitalWrite(switch_pins[switch_no],switch_default[switch_no]);
timer.deleteTimer(end_timer_id[switch_no]);
end_timer_id[switch_no]=32;
Serial.println(String("turn OFF switch: ") + switch_no);
}
}
// set switch state from APP
BLYNK_WRITE(V20) { turn_on_off(param.asInt(),0,TIME_CNT); }
BLYNK_WRITE(V21) { turn_on_off(param.asInt(),1,TIME_CNT); }
BLYNK_WRITE(V22) { turn_on_off(param.asInt(),2,TIME_CNT); }
BLYNK_WRITE(V23) { turn_on_off(param.asInt(),3,TIME_CNT); }