TIme input widget

dear all,
im playing with blynk for a while. The timer widget was pretty simple and clear since it sends just high low value at time mentioned . however this time input looks little complicated. if i want to make a pin high/low on the defined start/stop time on defined days of week how would i do it… can somebody help me with code.
thanks in advance

@Nitheesh_Mohan have you done a thorough search of this site as there is code available that would require minor mods at most. Try searching “TimeInput” and “Time Input”.

yea i did… but it is pretty confusing thats y i decided to ask u guys. it would be a great help if anybody could post some example code

Study this thread and the code included within it Timezone conversion for the Time Input widget

hey, i managed to find the code but it is not working,
this is the code i have used … please check it out

********** code starts *********
char auth[] = “";
char ssid[]="
”;
char pass[]="*****";
char Date[16];
char Time[16];
long startsecondswd; // weekday start time in seconds
long stopsecondswd; // weekday stop time in seconds
long nowseconds; // time now in seconds

int btnpin;
SimpleTimer timer;
WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V0);
void setup()
{
Serial.begin(9600);

pinMode(16,OUTPUT);
Blynk.begin(auth,ssid,pass);
Blynk.notify(“connected successfully”);
Serial.print(“done”);
rtc.begin();
timer.setInterval(60000L, activetoday); // check every minute if schedule should run today
timer.setInterval(30000L, reconnectBlynk); // check every 30s if still connected to server
}

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

BLYNK_WRITE(V1) {
TimeInputParam t(param);

Serial.print("Checked schedule at: ");
Serial.println(Time);
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
Serial.println(“Schedule ACTIVE today”);
if (t.hasStartTime()) // Process start time
{
Serial.println(String("Start: ") + t.getStartHour() + “:” + t.getStartMinute());
}
if (t.hasStopTime()) // Process stop time
{
Serial.println(String("Stop : ") + t.getStopHour() + “:” + t.getStopMinute());
}
// Display timezone details, for information purposes only
Serial.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
Serial.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)

for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, …)
if (t.isWeekdaySelected(i)) {
Serial.println(String("Day “) + i + " is selected”);
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
//Serial.println(startsecondswd); // used for debugging
if(nowseconds >= startsecondswd){

Serial.println(String(" ") + t.getStartHour() + “:” + t.getStartMinute());
if(nowseconds <= startsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent

digitalWrite(16, HIGH);
Blynk.virtualWrite(V2,1);
// code here to switch the relay ON
}
}
else{
Serial.println(“relay not on”);
// nothing more to do here, waiting for relay to be turned on later today
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
//Serial.println(stopsecondswd); // used for debugging

if(nowseconds >= stopsecondswd){

Serial.println(String(" ") + t.getStopHour() + “:” + t.getStopMinute());
if(nowseconds <= stopsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent

digitalWrite(16, LOW); //
Blynk.virtualWrite(V2,0);
// code here to switch the relay OFF
}
}
else{
if(nowseconds >= startsecondswd){ // only show relay has already started today
Serial.println(“relay is still ON”);
// nothing more to do here, waiting for motor to be turned off later today
}
}
}
else{
Serial.println(“Schedule INACTIVE today”);
// nothing to do today, check again in 1 minutes time
}
Serial.println();
}

void reconnectBlynk() {
if (!Blynk.connected()) {
if(Blynk.connect()) {
BLYNK_LOG(“Reconnected”);
} else {
BLYNK_LOG(“Not reconnected”);
}
}
}

BLYNK_WRITE(V2) {
btnpin=param.asInt();
digitalWrite(16,btnpin);
}

void loop()
{

if (Blynk.connected()) {

Blynk.run();
}
timer.run();

}

`

I am not prepared to look at your code until it is complete (showing libraries) and formatted.

Alright here it is…


 #include <SPI.h>
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h>
 #include <SimpleTimer.h>
 #include  <Time.h>
 #include <TimeLib.h>
 #include <WidgetRTC.h>

char auth[] = "********";
char ssid[]="******";
char pass[]="*******";
char Date[16];
char Time[16];

long startsecondswd;            // weekday start time in seconds
long stopsecondswd;             // weekday stop  time in seconds
long nowseconds;                // time now in seconds

int btnpin;
SimpleTimer timer;
WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V0);

void setup(){
  
    Serial.begin(9600);
    pinMode(16,OUTPUT);
    Blynk.begin(auth,ssid,pass);
    Blynk.notify("connected successfully");
    Serial.print("done");
    rtc.begin();
    timer.setInterval(60000L, activetoday);  // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server
}

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

BLYNK_WRITE(V1) {

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    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
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startsecondswd){
                   

                   if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2,1);
                            
                    }
          }

          else{
              Serial.println("relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopsecondswd){

                  
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopsecondswd + 90){   
                          // code here to switch the relay OFF
                          digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2,0);

                   }
          }
          else{
                  if(nowseconds >= startsecondswd){  // only show if motor has already started today
                              Serial.println("relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
    }
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();

}

void reconnectBlynk() {
  
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}

BLYNK_WRITE(V2) {
  
     btnpin=param.asInt();
     digitalWrite(16,btnpin);
}

void loop()
{
  
    if (Blynk.connected()) {
         Blynk.run();
    }
    timer.run();
  
}

See Blynk.begin seems to change my long values. without Blynk.begin it work ok

done

@Nitheesh_Mohan look through all the items marked with // COSTAS to see what you were missing out. Tested here and all fine on a WeMos D1 Mini Pro.

Few changes done by me:

  1. SPI.H is not needed for ESP’s
  2. 115200 baud is generally fine for ESP’s
  3. Must wait for Blynk to be connected or RTC may be skipped until next 5 minute cycle
  4. the 2 sprintf statements are important
  5. Vital you have #define BLYNK_PRINT Serial for debugging unless you are a perfect coder
  6. Recommend you define pins rather than using “16” as you only have one line of code to change if you want to change the pin.
 // #include <SPI.h>          // COSTAS
 //#define BLYNK_DEBUG        // COSTAS
 #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space // COSTAS
 #include <ESP8266WiFi.h>
 #include <BlynkSimpleEsp8266.h>
 #include <SimpleTimer.h>
 #include  <Time.h>
 #include <TimeLib.h>
 #include <WidgetRTC.h>

char auth[] = "xxx";
char ssid[]=  "xxx";
char pass[]=  "xxx";
char Date[16];
char Time[16];

long startseconds;            // start time in seconds
long stopseconds;             // stop  time in seconds
long nowseconds;              // time now in seconds

int btnpin;
SimpleTimer timer;
WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V0);  

void setup(){
    Serial.begin(115200);         // COSTAS
    Serial.println("\nStarted");  // COSTAS
    pinMode(16, OUTPUT);
    Blynk.begin(auth, ssid, pass);
    while (Blynk.connect() == false) {     // COSTAS
      // Wait until connected              // COSTAS
    }                                      // COSTAS
    Blynk.notify("Connected successfully");
    Serial.println("Done");
    rtc.begin();
    timer.setInterval(60000L, activetoday);     // check every minute if schedule should run today
    timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server
}

 void activetoday(){        // check if schedule should run today   
     if(year() != 1970){
        Blynk.syncVirtual(V1); // sync timeinput widget
        sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     // COSTAS
        sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());  // COSTAS
     }
}

BLYNK_WRITE(V1) {  

    TimeInputParam t(param);
    Serial.print("Checked schedule at: ");
    Serial.println(Time);
    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
         Serial.println("Schedule ACTIVE today");

         
          nowseconds = ((hour() * 3600) + (minute() * 60) + second());
          
          startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
          
          if(nowseconds >= startseconds){
                   
                   if(nowseconds <= startseconds + 90){    // 90s on 60s timer ensures 1 trigger command is sent
                            // code here to switch the relay ON
                            digitalWrite(16, HIGH); // set Relay ON
                            Blynk.virtualWrite(V2, 1);              
                    }
          }

          else{
              Serial.println("Relay not on");
              // nothing more to do here, waiting for relay to be turned on later today
          }
         
          stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
          if(nowseconds >= stopseconds){                
                  // 90s on 60s timer ensures 1 trigger command is sent
                  if(nowseconds <= stopseconds + 90){   
                          // code here to switch the relay OFF
                          digitalWrite(16, LOW); // set Relay OFF
                          Blynk.virtualWrite(V2, 0);
                   }
          }
          else{
                  if(nowseconds >= startseconds){  // only show if motor has already started today
                              Serial.println("Relay is still ON");
                              // nothing more to do here, waiting for motor to be turned off later today
                  }
          }
    }
    else{
        Serial.println("Schedule INACTIVE today");
        // nothing to do today, check again in 1 minutes time
    }
    Serial.println();
}

void reconnectBlynk() {
    if (!Blynk.connected()) {
      if(Blynk.connect()) {
          BLYNK_LOG("Reconnected");
      }
      else {
          BLYNK_LOG("Not reconnected");
      }
 }
}

BLYNK_WRITE(V2) {
     btnpin = param.asInt();
     digitalWrite(16, btnpin);
}

void loop()
{  
    if (Blynk.connected()) {
         Blynk.run();
    }
    timer.run();
}
1 Like

@Costas thank u soo much! its working fine now…

what widget is V2?

Button in SWITCH mode.

Just adding the following extract as I was asked about a fix for when TimeInput doesn’t have any active days. As pointed out by Blynk when they released the widget if(t.isWeekdaySelected(…) requires at least one active day.

Using the countactivedays variable (globally if required) you can skip the if(t.isWeekdaySelected(…).

for (int i = 1; i <= 7; i++) {
  if (t.isWeekdaySelected(i)) {
    countactivedays++;
  }     
}
if(countactivedays == 0){
  Serial.println("No active days in your schedule");
}
else{
  Serial.print(countactivedays);
  Serial.println(" active day(s) in your schedule");      
}
if(countactivedays !=0){
  if(t.isWeekdaySelected((weekday() + dayadjustment))){
    // etc, etc
  }
}

Edit: this doesn’t fix the problem as no days still comes back with 7 days. 1 to 7 days are ok but not zero days.

As the t.isWeekdaySelected() function is unable to distinguish between no days selected and seven days selected the only fix that I can suggest it with the use of an additional widget, which is not ideal but would work.

If you add TimeInput widget to your project then the assumption is that you want to schedule events during some of the 7 days of a week. Therefore most of the time you will always have at least one day selected so it shouldn’t be a problem.

However if you are perhaps going on holiday there may be a 7 or more day period when you don’t want that part of your project to run but you can’t simply stop the project as other parts of the project might be needed.

So with a simple button in SWITCH mode you can ensure that a particular TimeInput widget knows that you have selected zero days with something like this in place of most of the code in my last post.

bool scheduleactive = true;  // global variable

BLYNK_WRITE(V3){  // button in SWITCH mode
  if(param.asInt()){
    scheduleactive = true;      
  }
  else{
    scheduleactive = false;   
  }
}

And then in the TimeInput code it is simply:

if(scheduleactive){
   // all the TimeInput code here
}

may I know what is V0?

It was the RTC widget but in the last few days the Blynk libraries have been changed for this widget. A pin is no longer required for the widget and the code to use the widget is slightly different. See latest RTC example provided by Blynk.

two simple questions…your code as is , it doesnt compile unless i move activetoday and reconnectBlynk functions before setup…then it works fine…why?
second…if i want to add a second time input widget that controls the same output.this widget has the exact same code as the first…i add the same code again or can i declare blynk_write for both virtual pins?
hope i make sense

What IDE version are you using? There was a buggy version that had to have functions in a specific order. Latest IDE is fine.

If I understand you correctly then yes the same code but a different virtual pin for the Time Input widget.
Just to be clear you want 2 time settings or to control 2 devices?

2 time settings…of course different virtual pins…
i m using an older version cause i had some problems with other libraries…i thought it would be a problem of this kind but thought i could ask…
tnanx again for responding so fast