TIme input widget

@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

is there any way to make that simpler or it is not worth it and i should follow my familiar copy-paste programming tactic?..with another function or alternating the blynk_write function…?

What hardware are you using?

I would go with copy and paste for now, you can always look to improve the coding later.

esp8266 nodemcu

thats what i thought about copy paste…have you tested other boards?any suggestions?

I use WeMos’ most of the time.

My sketch for Wemos D1 Mini doesnt work properly.

I have added these strings:

#include <WidgetTimeInput.h>
#include <WidgetRTC.h>

But I see errors while compiling:

In file included from /Users/vl/Documents/Arduino/perepel_cell/perepel_cell.ino:21:0:
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h: In constructor ‘TimeInputParam::TimeInputParam(const BlynkParam&)’:
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h:44:24: error: ‘class BlynkParam::iterator’ has no member named ‘isEmpty’
} else if (!it.isEmpty()) {
^
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h:58:24: error: ‘class BlynkParam::iterator’ has no member named ‘isEmpty’
} else if (!it.isEmpty()) {
^
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h:73:17: error: ‘class BlynkParam::iterator’ has no member named ‘isEmpty’
if (!it.isEmpty()) {
^
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h:79:51: error: ‘BlynkBitSet’ was not declared in this scope
BlynkBitSet(mWeekdays, c - ‘1’);
^
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h: In member function ‘bool TimeInputParam::isWeekdaySelected(int) const’:
/Users/vl/Documents/Arduino/libraries/Blynk/src/WidgetTimeInput.h:114:53: error: ‘BlynkBitRead’ was not declared in this scope
return BlynkBitRead(mWeekdays, (day - 1) % 7);
^
In file included from /Users/vl/Documents/Arduino/perepel_cell/perepel_cell.ino:22:0:
/Users/vl/Documents/Arduino/libraries/Cayenne/WidgetRTC.h: In member function ‘void WidgetRTC::setVPin(int)’:
/Users/vl/Documents/Arduino/libraries/Cayenne/WidgetRTC.h:35:36: error: invalid conversion from ‘long unsigned int ()()’ to 'getExternalTime {aka long int ()()}’ [-fpermissive]
setSyncProvider(requestTimeSync);
^
In file included from /Users/vl/Documents/Arduino/libraries/Time/Time.h:1:0,
from /Users/vl/Documents/Arduino/perepel_cell/perepel_cell.ino:10:
/Users/vl/Documents/Arduino/libraries/Time/TimeLib.h:134:9: error: initializing argument 1 of ‘void setSyncProvider(getExternalTime)’ [-fpermissive]
void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider
^
Multiple libraries were found for “Ethernet.h”
Used: /Users/vl/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/Ethernet
Not used: /Applications/Arduino.app/Contents/Java/libraries/Ethernet
Multiple libraries were found for “BlynkSimpleEsp8266.h”
Used: /Users/vl/Documents/Arduino/libraries/Cayenne
Not used: /Users/vl/Documents/Arduino/libraries/Blynk
Not used: /Users/vl/Documents/Arduino/libraries/Blynk
Not used: /Users/vl/Documents/Arduino/libraries/Blynk
Not used: /Users/vl/Documents/Arduino/libraries/Blynk
exit status 1
Error compiling for board WeMos D1 R2 & mini.

Please don’t interject your project issue into an old post… create your own new posting and we will take it from there.

2 posts were split to a new topic: Need assistance to program relay control loops