Getting Data from TimeInput Widget

I am using DS1307 as Real time RTC and Reading Input From RTC and Displaying on Blynk app. Its working fine with below code. Now i would like to use TimeInput Widget from virtual port. how to read Date,time , weekday from Virtual port

#include "Wire.h"

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>

#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
SimpleTimer timer;
char Date[16];
char Time[16];

//Local time date variables
static  int local_day;
static  int local_month;
static  int local_year;
static  int local_s;
static  int local_h;
static  int local_m;

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  Blynk.run();
  timer.run();
  GetDateTime1();
  sprintf(Date, "%04d/%02d/%02d", local_day , local_month , local_year);
  sprintf(Time, "%02d:%02d:%02d", local_h, local_m, local_s);


  Blynk.virtualWrite(V11, Date);
  Blynk.virtualWrite(V10, Time);
}



byte decToBcd(byte val) {
  // Convert normal decimal numbers to binary coded decimal
  return ( (val / 10 * 16) + (val % 10) );
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val / 16 * 10) + (val % 16) );
}



void GetDateTime1() {

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  local_s = bcdToDec(Wire.read());
  local_m = bcdToDec(Wire.read());
  local_h = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  local_day = bcdToDec(Wire.read());
  local_month = bcdToDec(Wire.read());
  local_year = bcdToDec(Wire.read());
}

Now i need Below part Need to be changed

BLYNK_WRITE(V4)//Monday-Friday
{  
  if (mondayfriday==1) {         
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("M-F Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    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
    terminal.println("Monday-Friday ACTIVE today");
    terminal.flush();
    if (t.hasStartTime()) // Process start time
    {
      terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
    }
    if (t.hasStopTime()) // Process stop time
    {
      terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
    }
    // Display timezone details, for information purposes only 
    terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
  //  terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
    terminal.flush();
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        terminal.println(String("Day ") + i + " is selected");
        terminal.flush();
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      terminal.print("Monday-Friday STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      terminal.println("Monday-Friday Device NOT STARTED today");
      terminal.flush();
   
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      terminal.print("Monday-Friday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED, HIGH); // set LED ON    test
        Blynk.virtualWrite(V2, 1);
        terminal.println("Monday-Friday is ON");
        terminal.flush();
      
      }          
    }
  }
  else{
    terminal.println("Monday-Friday INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 30 SECONDS time    
  }
  terminal.println();
}
}

With below code i could able to read data from timeWidget . But i am facing issue while turnon & turn of LED
Here i could able to Get RTC time and Time input from timeInputwidget . When time set between range LED remain HIgh . If time elapsed LED will Not turnoff. can someone tell issue in code.

BLYNK_WRITE(V3)  // MonDay->Friday Time Function
{
  TimeInputParam t(param);
  sprintf(Date, "%04d/%02d/%02d", local_day , local_month , local_year);
  sprintf(Time, "%02d:%02d:%02d", local_h, local_m, local_s);

  if (t.hasStartTime()) // Process start time
  {
    Serial.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
    //Serial.println( t.getStartMinute());
    Serial.flush();
  }
  if (t.hasStopTime()) // Process stop time
  {
    Serial.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
  // Serial.println(t.getStopMinute());
    Serial.flush();
  }

 /* if (( t.getStartHour() >= local_h) || local_h<= t.getStopHour())
  {
    if ((t.getStartMinute() >= local_m) || (local_m < t.getStopMinute()))
    {
      digitalWrite(D4, HIGH);
      led22.on();
    } else
    {
      digitalWrite(D4, LOW);
      led22.off();
    }
  }
*/

  if((local_h>= t.getStartHour())|| local_h<t.getStopHour())
   {
    
    if((local_m>= t.getStartMinute())|| local_m<t.getStopMinute())
    {
      digitalWrite(D4, HIGH);
      led22.on();  
      
    }else if(local_m>t.getStopMinute())
    {
       digitalWrite(D4, LOW);
      led22.off();
    }
    
   }



}

Well… you’re not turning you led off after time elapsed.

You need something at the end to turn the led off…

BLYNK_WRITE is only executed when you press the time input, so you will need to turn off the led inside somewhere else in your code, preferably inside a timer

Since I am taking RTC time and comparing with input from Time widget It should always try to compare RTC time. assume if i use timer interrupt how can i trigger the digital IO based on RTC time

AS example shown by other if i run application using timer interrupt weather The App will allow the trigger based on TimeInput Widget . I am assuming TimeInput widget allow u to Settime . Once time being set in the weekdays Every weekdays within time range relay get triger . weather this function is executed or not.

BLYNK_WRITE will only executed when time input widget changes on your widget. You need to save the start and stop time in a variable and compare it outside the BLYNK_WRITE loop

I would like to know how can I use it. I wil set some timeinput from time widget. I won’t any changes in widget input for week as set time . After a week when time has reached I.e Monday assume my set time is 10 am to 11am. Time show start time10 and stop time 11. Weather app allow to trigger the relay based on previous setting. Or I need to change every time. To get triggered.

Is there any other way without time input wizard I can set time. Like test input

Can u show same example how to do in timer interrupt. I have done with function call but not with blynk write function

As I pasted my first code example. Where people used timerinterrupt for rtc. I never got rtc time. I would like to know using ntp why widget rtc done.

My project is based on rtc. So kindly suggest me rtc code that can track real time date and time. The library used in blynk can’t able to set date and time. And etc show timing from 0:0:0 instead of actual rtc showing. Example if sys showing 10:clock1030: then it showing 00:00:05 and count sec time increasing…

Can u show some simple example. How can get time from time widget and once data taken use it my code.
Obce variable access outside I can use timer interrupt or function in loop

Thanks for support.It started working.I need clarification on below.
I am following below code https://community.blynk.cc/t/time-clock-wont-read-correct/17689

I have 2 TimeWidgetinput . One from Monday_FRI & Sat_sun. I would like to choose weekday. if Its weekdays Relay1 will be operated if its weekend relay 2 is operated…

In below code How can i decide weather its weekday or weekend,Or Based on RTC date & time it judge weekday or weekend

void MonDay_FriDay_TimeCheck()
{

  sprintf(Date, "%04d/%02d/%02d", year() , month() , day() );
  sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  Blynk.virtualWrite(V11, Date);
  Blynk.virtualWrite(V10, Time);

  if (mondayfriday == 1)
  {
    // call with timer every 30 seconds or so
    // Get RTC time
    sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
    Serial.print("Current Time: ");
    Serial.println(currentTime);

    // Get Time Input Widget time
    sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
    Serial.print("Start Time: ");
    Serial.println(startTime);

    sprintf(startTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
    Serial.print("Stop Time: ");
    Serial.println(startTime);


    if (hour() == SThour || hour() == SPhour)
    {
      if (minute() >= STmin && minute() <= SPmin)
      {
        Serial.println("RELAY IS ON");
        digitalWrite(D7, HIGH); // Turn ON built-in LED
        led22.on();
      } else
      {
        Serial.println("RELAY IS OFF");
        digitalWrite(D7, LOW); // Turn ON built-in LED
        led22.off();
      }
    }
  }


}

How can i set timing more than 2 hours
If I check condition if hour>Sthour && hour <SPhour

it will do wired thing it always on or off

assume my RTC is 13:00:00
Start time:12.00.00
stop time : 14.00…0
Below condition will be failed .

If i try to check (12>=13 and 14<=13 ) This condition also it wont take

How can i check my timing in range or map operation in arduino.

 if (hour() == SThour || hour() == SPhour)
    {
      if (minute() >= STmin && minute() <= SPmin)
      {
        Serial.println("RELAY IS ON");
        digitalWrite(D7, HIGH); // Turn ON built-in LED
        led22.on();
      } else
      {
        Serial.println("RELAY IS OFF");
        digitalWrite(D7, LOW); // Turn ON built-in LED
        led22.off();
      }
    }
  }else
  {
     led22.off();
  }

This statement doesn’t make a lot of sense.

Consider storing the start and stop time as minutes for example StartTime = t.getStartHour() * 60 + t.getStartMinute(); do the same for stop time using, obiviously the stop values, and then compare then later on, will be easier and simpler for you.

I’m pretty sure you will find examples elsewhere in the forum already.

1 Like
I have tested the function. I am using same function for both Monday_friday and All_day .  

Relay will be activated for some time or deactivated for some time.

If i trigger any LED & switch to other timewizard led will be on.
Sometime as soon as Blynk start Even it time is in limit led will not work.

I have tried putting hour()> setitme & lessthan stop time LED try to remain High

I have attached images for reference,

void MonDay_FriDay_TimeCheck()
    {

      if (mondayfriday == 1)
      {


        // call with timer every 30 seconds or so
        // Get RTC time
        sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
        Serial.print("Current Time: ");
        Serial.println(currentTime);

        // Get Time Input Widget time
        sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
        Serial.print("Start Time: ");
        Serial.println(startTime);

        sprintf(startTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
        Serial.print("Stop Time: ");
        Serial.println(startTime);


        if (hour() >= SThour || hour() <= SPhour)
        {
          if (minute() >= STmin && minute() <= SPmin)
          {
            Serial.println("RELAY IS ON");
            digitalWrite(D7, HIGH); // Turn ON built-in LED
            led22.on();
          } else
          {
            Serial.println("RELAY IS OFF");
            digitalWrite(D7, LOW); // Turn ON built-in LED
            led22.off();
          }
        }
      } else
      {
        led22.off();
      }


    }

if time Widget Input is more than 2 hours range Relay will be off . if time input range is between 1 hour it will work fine.

In time Widget there is option of selecting weekday . How to use them If i select the time MON wed sat those days only within timing range relay must work how can i judge it???

Both the second and third block of your code use a variable called startTime. Shouldn‘t the third block use stopTime? (But I don‘t know if it matters.)

Here is my code for time zone function

void TimeZone1_Function()
{

  if (TimeZone1 == 1)
  {
    // call with timer every 30 seconds or so
    // Get RTC time
    sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
    Serial.print("Current Time: ");
    Serial.println(currentTime);

    // Get Time Input Widget time
    sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
    Serial.print("Start Time: ");
    Serial.println(startTime);

    sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
    Serial.print("Stop Time: ");
    Serial.println(stopTime);

    if (hour() >= SThour &&  hour() <= SPhour)
    {
      if (minute() >= STmin && minute() <= SPmin)
      {
        Serial.println("RELAY IS ON");
        Serial.println("..........................");
        digitalWrite(D5, HIGH); // Turn ON built-in LED
        led22.on();
      }
      else
      {
        Serial.println("RELAY IS OFF"); Serial.println("..........................");
        digitalWrite(D5, LOW); // Turn ON built-in LED
        led22.off();
        
      }
    }

    else
    {
      led22.off(); digitalWrite(D5, LOW); 
        Blynk.virtualWrite(V51, 0);
    }

  }

}

The code will work fine if time between 1 hour range if its beyond 1 hours . The code wont work
Here is example.

Time Zone1 is selected
Current Time: 09:02:55
Start Time: 08:40:00
Stop Time: 10:40:00
RELAY IS OFF

AUTO mode is selected
Current Time: 09:03:06
Current Time: 09:03:07
Current Time: 09:03:08
Current Time: 09:03:09
Current Time: 09:03:10
Current Time: 09:03:11
Current Time: 09:03:12
Current Time: 09:03:13
Current Time: 09:03:14
Current Time: 09:03:15
Current Time: 09:03:16
Current Time: 09:03:17
Current Time: 09:03:18
Current Time: 09:03:19
Current Time: 09:03:20
Current Time: 09:03:21
Current Time: 09:03:22
Current Time: 09:03:23
Current Time: 09:03:24
Current Time: 09:03:25
Current Time: 09:03:26
Current Time: 09:03:27
Current Time: 09:03:28
Current Time: 09:03:29
Current Time: 09:03:30
Current Time: 09:03:31
Current Time: 09:03:32
Current Time: 09:03:33
Current Time: 09:03:34
Current Time: 09:03:35
Current Time: 09:03:36
Current Time: 09:03:37
Current Time: 09:03:38
Current Time: 09:03:39
Current Time: 09:03:40
Current Time: 09:03:41
Current Time: 09:03:42
Current Time: 09:03:43
Current Time: 09:03:44
Current Time: 09:03:45
Start Time: 09:03:00
Stop Time: 09:59:00
RELAY IS ON

In your example, the current minute (2) is not >= minute starttime (40). So the if- function comparing minutes returns false.

I am quite new at this, but my guess is that you need to use a different time-format for the comparison and only convert to hh:mm:ss format to make it understandable for us humans.

Update: A timer library might help you to do this.

I am reading data based on timer library itself. But i could not able to achieve this.
How can i check minute condition here,

Like ldb mentioned: convert h:mm to minutes and only compare minutes.

Thanks for suggestion . I converted time into total hour & then comapre itself total start hour & stop hour & its working. I would like to another feature in Time widget. Time widget allow u to select weekdays and weekends.

If my relay want to operate only on MON,WED,FRI i.e selected weekdays from time widget function are MON,WED,FRI . how can i detect in my code

It’s all out there. https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=Widgets%2FTimeInput%2FAdvancedTimeInput

for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
    }
  }