How to use Advanced TimeInput to turn on/off an output?

@Ayslan_Carolli take a look at the following code:

// Scheduler.ino by Costas 22 March 2017
// Include library hack so scheduler is off when no days selected
#define BLYNK_PRINT Serial          // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define Relay 2

unsigned int clockHour;
unsigned int clockMinute;
unsigned int clockSecond;
unsigned long daySeconds;
unsigned int dayNumber;  // 1 Monday through to 7 Sunday, like Blynk day numbers
bool activeToday = false;
long start1seconds, stop1seconds;
bool scheduler = true;          // default is scheduler on but it will be checked every 10s
bool schedulerStatusChanged = false; // check if status has changed

char auth[] =   "xxxxxxxx";
char ssid[] =   "xxxxxxxx";
char pass[] =   "xxxxxxxx";
char server[] = "xxxxxxxx";

SimpleTimer timer;

BLYNK_WRITE(V0) {   // Weekday timer
  activeToday = false;
  TimeInputParam t(param);
  unsigned int countondays = 0;
  for (int i = 1; i <= 7; i++) {          // Process weekdays (1. Mon, 2. Tue, 3. Wed, ... 7. Sun)
    if (t.isWeekdaySelected(i)) {
      countondays++;
      if(i == dayNumber){
        activeToday = true;  // set false at start of function
      }
      //Serial.println(String("Day ") + i + " is selected");
    }
  }
  if (countondays == 0) {
    scheduler = false;  
  }
  else{
    scheduler = true;    
  }
  if (scheduler != schedulerStatusChanged){
    schedulerStatusChanged = scheduler;
    if(scheduler == false){
      Serial.println(F("Scheduler1 currently disabled"));
      Blynk.virtualWrite(V1,"Scheduler1 currently disabled\n");        
    }
    if((scheduler == true) && (activeToday == true)){
      Serial.println(F("Scheduler1 active today"));
      Blynk.virtualWrite(V1,"Scheduler1 active today\n");      
    }
    if((scheduler == true) && (activeToday != true)){
      Serial.println(F("Scheduler1 not active today"));
      Blynk.virtualWrite(V1,"Scheduler1 not active today\n");      
    }
  }
  if(scheduler == true){    
    if (t.hasStartTime())                                             // Process start time
    {
      start1seconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    }
    else if (t.isStartSunrise())
    {
      Serial.println(F("Start at sunrise"));
    }
    else if (t.isStartSunset())
    {
      Serial.println(F("Start at sunset"));
    } 
    if (t.hasStopTime())                                              // Process stop time
    {
      stop1seconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);    
    }
    else if (t.isStopSunrise())
    {
      Serial.println(F("Stop at sunrise"));
    }
    else if (t.isStopSunset())
    {
      Serial.println(F("Stop at sunset"));
    }
  }
}

void timeChecker()
{
  if(activeToday == true){
    if((daySeconds >= start1seconds)&&(daySeconds <= start1seconds + 10)){
      digitalWrite(Relay, LOW);          // LED ON or relay active LOW ON
      Serial.println(F("Device ON"));
    }
    if((daySeconds >= stop1seconds)&&(daySeconds <= stop1seconds + 10)) {
      digitalWrite(Relay, HIGH);         // LED or relay active LOW OFF
      Serial.println(F("Device OFF"));
    }  
  }
}

void setup(){
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW); 
  Blynk.begin(auth, ssid, pass, server); 
  timer.setInterval(10000L, timeChecker);   // check every 10s if any weekday scheduled events
}

void loop()
{
  Blynk.run();
  timer.run();
}
2 Likes