Time input widget and eventor

Hopefully your Grandma can use the Time Input widget as per the following revamped sketch. It’s really quite straightforward and easy to incorporate into your own projects.

/********************************************************************************* 
  EziSchedulerV2.ino   by Costas, 7 Sept 2017
  Scheduler with Time Input and obligatory OTA update facility for ESP's
  RTC widget (no pin allocation)
  LED V0
  Time Input V1
  To add more schedules simply copy BLYNK_WRITE(V1) function and change V1 to V2 etc
  and in activetoday() function add: Blynk.syncVirtual(V2); // sync scheduler #2 
***********************************************************************************/
#define BLYNK_PRINT Serial 
#include <ArduinoOTA.h>             
#include <ESP8266WiFi.h>        
#include <BlynkSimpleEsp8266.h> 
BlynkTimer timer;

#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetRTC rtc;
char currentTime[9];

char auth[]   = "************"; 
char ssid[]   = "GargoyleTest";                     
char pass[]   = "************";                     
char server[] = "blynk-cloud.com"; 
bool clockSync = false;    

void setup() {
  Serial.begin(115200);
  Serial.println();
  Blynk.begin(auth, ssid, pass, server);
  ArduinoOTA.setHostname("EziSchedulerV2");       
  ArduinoOTA.begin();    
  timer.setInterval(60000L, activetoday);  // check every 60s if ON / OFF trigger time has been reached
  timer.setInterval(1000L, clockDisplay);  // check every second if time has been obtained from the server
}

BLYNK_CONNECTED() {
  rtc.begin();
}

void activetoday(){         // check if schedule #1 should run today
  if(year() != 1970){
    Blynk.syncVirtual(V1);  // sync scheduler #1  
  }
}

void clockDisplay(){  // only needs to be done once after time sync
  if((year() != 1970) && (clockSync == false)){ 
    sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
    Serial.println(currentTime);
    clockSync = true;
  } 
}    

BLYNK_WRITE(V1) {   // Scheduler #1 Time Input widget  
  TimeInputParam t(param);
  unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);  
  unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
  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))){ //Time library starts week on Sunday, Blynk on Monday  
    //Schedule is ACTIVE today 
    if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){    // 62s on 60s timer ensures 1 trigger command is sent
      Blynk.virtualWrite(V0, 255);  // turn on virtual LED
      Serial.println("Schedule 1 started");
    }                  
    if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){   // 62s on 60s timer ensures 1 trigger command is sent
      Blynk.virtualWrite(V0, 0);   // turn OFF virtual LED
      Serial.println("Schedule 1 finished");
    }               
  }
}

void loop() {
  if(Blynk.connected()){ 
    Blynk.run();        
  }                     
  ArduinoOTA.handle(); 
  timer.run();
}
3 Likes