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

Hello,

Recently I found Blynk to start my projects with Arduino, I liked it so much that I bought the biggest “energy” package to expand my project. But I’m having problems with the TimerInput widget, I can’t getting to turn on or off the Arduino outputs what I choose the widget.

Can you help me?

1 Like

TimeInput is not a control widget. It just allows you to input a time to use in logic on your hardware.

You might want to look at the Timer or Eventor widget.

That’s what I thought. But, Timer and Eventor, the app user (not the developer) can’t change the times.

We are all App users (and thus can change everything). Do you mean when you share your project, you want the “run only” user to have the ability to change something in the timer or eventor? That kind of goes against the whole “run only” aspect.

Assuming that is what you are refering too, what kind of changes to time and/or events do you want those “run only” users to adjust? Perhaps those options can simply be pre-programmed choices made by the Menu Widget or other button/slider changes.

Exactly, when I referred to “App User”, I meant “run only”.
But the TimerInput gives the “Run Only” right to change days of the week, start time and end time. This would give freedom to those who are using the shared project to make their decisions.

If the TimerInput sets a start and end time, it could be compared to the RTC, for example, it could execute a command externally.

So… are you looking assistance in a way to do something like that for a “run only” user? or putting in a Widget idea request for a larger scale group of “run only” users?

Yeah. I’m working on a project for home automation. Some customers ask me to add this feature. For example, “On Saturday at 10:00 PM, the alarm is on,” but they want control so they can change, date and time when needed without needing to call me.

Well, then you are getting more into the business side of app customisation… check over here http://www.blynk.io/

Then your initial intend to use TimeInput is correct.

With the TimeInput widget you can have a variable in your code that will hold a user choice and then execute a logic when needed. So, what is the problem?

I believe, you could even find a real code example. Check for instance this post:

@Eugene I think that what @Ayslan_Carolli is looking for (and I was suggesting) is a user selectable (and while app running) start/stop timer (like TimeInput) but one that simply toggles a virtual pin HIGH/LOW when the start/stop time is reached.

Much easier to implement for simple tasks… say an alarm clock that simply activates a buzzer when a Vpin goes high… no storing start stop time in seconds of the day in variables and running code calculations etc.

This however would obviously require the App be actively running, at least in background.

@Ayslan_Carolli The way the current TimeInput widget works is that it sends the start and stop time in seconds of the day as parameters associated with a Vpin… but it sends this data as soon as someone sets the timer and hits OK, not as the start/stop time happens.

For example the TimerInput, on V0, is set for every day of the week but starts at Midnight (0 seconds into the day) and runs for one hour (3600 seconds into the day).

As soon as that info is entered into the widget and the OK button is pressed, this code will provide two numbers, one for the start time and one for the stop time:

BLYNK_WRITE(V0) {
  long startTimeInSecs = param[0].asLong();  // this would result in startTimeInSecs = 0
  long endTimeInSecs = param[1].asLong();   // this would result in endTimeInSecs = 3600
}

From there you need to write some code logic to break down the seconds of the day into whatever actions you wish to happen at the appropriate time.

Hope this helps you understand it better.

Got it, I guess. I’ll keep trying, also using parts of the code that you cited.

If I have advances, I will post here for those who have the same doubts as I do.

1 Like

I can join the RTC Widget and Widget TimerInput, I created Strings to make comparisons, “currentTime”, “currentDate”, “startTime” and “stopTime”, but when I compare the Strings in void loop, it has no value in those Strings.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <WidgetTimeInput.h>
char auth[] = "xxxxxxxxxx";
String currentDate;
String currentTime;
String startTime;
String stopTime;
SimpleTimer timer;
WidgetRTC rtc;

BLYNK_WRITE(V100) {
  TimeInputParam t(param);

  // Process start time

  if (t.hasStartTime())
  {
    startTime = String(t.getStartHour()) + ":" + t.getStartMinute() + ":" + t.getStartSecond();
    Serial.println("Start: " + startTime);

  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
  }
  else
  {
    // Do nothing
  }

  // Process stop time

  if (t.hasStopTime())
  {
    stopTime = String(t.getStopHour()) + ":" + t.getStopMinute() + ":" + t.getStopSecond();

    Serial.println("Start: " + stopTime);
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());

  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)

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


  Serial.println();
}


void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  currentTime = String(hour()) + ":" + minute() + ":" + second();
  currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V101, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V102, currentDate);

}
void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth);
  rtc.begin();


  //  timer.setInterval(10000L, clockDisplay);
}

void loop()
{
  Blynk.run();
  timer.run();
  delay(2000);
  Serial.println(currentTime);
  delay(2000);
  Serial.println(currentDate);
  delay(2000);
  Serial.println(startTime);
  delay(2000);
  Serial.println(stopTime);

}

@Ayslan_Carolli you can’t have 8000ms delay’s in loop() because you are likely to miss the heartbeat between the MCU and the server. Remove all reference to time from the loop() as it’s simply a duplication of the correctly structured clockDisplay() function called with SimpleTimer.

I’ll dig out a piece of code that you might find useful.

Yahoo, I did it! I was able to do Timer Input to trigger my outputs.
In this code below I’m using pins 30 and 32.

It’s works!!

Ps. Now we have only to compare the days of the week. Hehe

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <WidgetTimeInput.h>
char auth[] = "xxxxxxxxxxxxxxxx";
String currentDate;
String currentTime;
String startTime;
String stopTime;
SimpleTimer timer;
WidgetRTC rtc;

BLYNK_WRITE(V100) {
  TimeInputParam t(param);

  // Process start time

  if (t.hasStartTime())
  {
    startTime = String(t.getStartHour())  + ":" + t.getStartMinute()  + ":" + t.getStartSecond();
    Serial.println("Start: " + startTime);

  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
  }
  else
  {
    // Do nothing
  }

  // Process stop time

  if (t.hasStopTime())
  {
    stopTime = String(t.getStopHour()) + ":" + t.getStopMinute() + ":" + t.getStopSecond();

    Serial.println("Start: " + stopTime);
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());

  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)

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


  Serial.println();
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth);
  rtc.begin();
  pinMode(30, OUTPUT); //SAIDAS
  pinMode(32, OUTPUT); //SAIDAS

}

void loop()
{
  Blynk.run();
  timer.run();
 
  //TEMPO
  currentTime = String(hour()) + ":" + minute() + ":" + second();
  currentDate = String(day()) + " " + month() + " " + year();

  // Send time to the App
  Blynk.virtualWrite(V101, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V102, currentDate);

  //TEMPO

  
  delay(1000);
if (currentTime == startTime){
  digitalWrite(30, HIGH);
    digitalWrite(32, HIGH);
}
else if (currentTime == stopTime){
  digitalWrite(30, LOW);
    digitalWrite(32, LOW);
}
}

@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

Hi @Costas this is for a timer? o for a simple time input?

@AdrianStivalet as per the title and referenced throughout this topic… this is about the Time Input Widget

http://docs.blynk.cc/#widgets-interface-time-input