BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Time input widget and eventor


#5

Time Input doesn’t do anything unless you provide coding for it i.e coding to check current time against ON and OFF times set in the widget.

Sample code exists on this site, check out the “Scheduler” project by @psoro


#6

oh I see… could have been cool if the time input also returned a 0/1…

I’ll try and look into Psoro’s scheduler, thx for pointing it out.


#7

I got it working with help from analyzing psoro’s scheduler project.

I got it boiled down to this… dont know if I could cut more out but its working with the time input widget.

// #define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "";  // Put your Auth Token here. (see Step 3 above)
SimpleTimer timer;
WidgetRTC rtc;

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



void setup() 
{
  WiFi.mode(WIFI_STA);
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, "", ""); //insert here your SSID and password
  rtc.begin();
  delay(10);
  
  timer.setInterval(100000L, sendWifi);
  timer.setInterval(60000L, syncRelay);
  timer.setInterval(1000,showCurrentTime);

}



BLYNK_WRITE(V50)//Fauntain 
{  
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
    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
          
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        Blynk.virtualWrite(V51, 1);
        // code here to switch the relay ON
      }      
    }
    else
    {
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      Blynk.virtualWrite(V51, 0);
      
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        
       Blynk.virtualWrite(V51, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        Blynk.virtualWrite(V51, 1);
        
       
      }          
    }
  }
  else
  {
   // nothing to do today, check again in 30 SECONDS time    
  }
}



BLYNK_WRITE(V53)//Gardenlight 
{  
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
    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
          
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        Blynk.virtualWrite(V52, 1);
        // code here to switch the relay ON
      }      
    }
    else
    {
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      Blynk.virtualWrite(V52, 0);
      
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        
       Blynk.virtualWrite(V52, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        Blynk.virtualWrite(V52, 1);
        
       
      }          
    }
  }
  else
  {
   // nothing to do today, check again in 30 SECONDS time    
  }
}



void syncRelay()
{
  Blynk.syncAll();
  
  Serial.println("sync relay");
}


void showCurrentTime()
{
  String CurrentDate = String(day()) + '-' + monthShortStr(month()) + '-' + year();
  String CurrentTime = String(hour()) + ':' + minute() + ':' + second();
  String formattedDate = CurrentDate + String(" | ") + CurrentTime;
  Blynk.virtualWrite(V11,formattedDate);
}



void sendWifi() 
{
  Blynk.virtualWrite(1, map(WiFi.RSSI(), -105, -40, 0, 100) );
}



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

#8

funny after proclaiming I got it solved, I found some errors:

  • after midnight it wont sync relays
  • I cant turn of “automatic” setting.

#9

Gonna move the project to WIP :slight_smile:


#10

I am having myself similar problems with timers. Blynk guys are doing really a great job with the application upgrading, adding very interested and useful widgets all time. (specially for Android).
However, if here is something i could suggest to get improved is the timer widget. It would be great if it could work like a time input widget, which to be used as a timer requires a lot of coding in the hardware.
It is totally unpractical that to change the time parameter in the timer you have to stop the app running and get into the “programming” to change the time. Besides, I have not discovered yet about why the timers work some times and do not in many other times. Is totally unreliable.
Regards
rmerlob


#11

In this I can input time, or change it to my needs on the go, is that what you are looking for?


#12

Thanks tjohansen, Looks good at first sight.
I will try it.
What would it be the response if one the time inputs have all days disabled ?
Still, I thought main objective of Blynk was supposedly to reduce the need of coding to the maximum so its accessible to people with non programming expertise.
It would be great if “time input” writes a 0 or 1 if conditions are met.
Regards


#13

I believe that was an initial marketing strategy (and still valid)… And Blynk has many features that can be easily done with minimal or no coding.

But anything with this much flexibility and customizable options will always require some degree of skill to maximise it’s potential.

Based on what I have seen and read… I think the main objective is as a customisable Graphical Control Interface for IOT. And that it is :stuck_out_tongue:

"Blynk is a Platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet.

It’s a digital dashboard where you can build a graphic interface for your project by simply dragging and dropping widgets."

This is effectively what the Time option in the Eventor Widget does… without needing code :wink:


#14

Thanks Gunner for your comments.
Yeah, from your point of view or my point of view that is perfectly doable, however, if the “timer input” could work as a real timer, my grandma could set the parameters, while I don not see her being able to adjust timer and events.
Thanks again
Regards,


#15

@rmerlob also when you publish your app to the world Eventor will not function for the the end users.


#16

This issue with making the Input Timer the way many seem to want it, is that it relies on the App being online (like the Eventor) in order to switch those pins.

The current Timer takes the input, sends the data immediately to the Sketch, for processing via code, and then the timing is ready and availed for action, each and every time… even if server connection goes down (assuming coded properly for that).

So there are differing options, with differing levels of complexity, that offer differing levels of connection independence.


#17

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();
}

Missing something with attempt to understand Advanced Time Input
Missing something with attempt to understand Advanced Time Input
#18

Thanks a lot Costas,
I will definitely try it.


#19

thats the one i wanted but never fin until today big thanks


#20

What it’s this?


#21

That is part of the Over The Air update option for ESP (basicly uploding your sketch over the WiFi network instead of plugging it into USB) - see here for example C++ Blynk - Code Examples for Basic Tasks (Work in Progress) Nothing to do with Timers :stuck_out_tongue:


#23

Thanks again @Gunner I realize that I don’t need to code this in my case, 'cos I using ethernet shield :slight_smile: I’ll try to use this code to my project and see if it’s work for me.


#24

This is my code, but still have issues, please help me!!!

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>

#define BLYNK_PRINT Serial
#define echoPin 3 // Echo Pin
#define trigPin 5 // Trigger Pin
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

BlynkTimer timer;            // define a timer for use by SimpleTimer library
WidgetRTC rtc;

int WM1=6;
int EncWV1=6;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=6;                // Global variable used in Slider widget and runEveryMinute()

int WM2=7;
int EncWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int ApagWV2=7;                // Global variable used in Slider widget and runEveryMinute()
int x, y, z, a;

int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;

int CounterOn2 = 0;
int CounterOff2 = 0;
int ledStatus2 = 0;

int AutoRelleno=8;
int SumpLamp=9;

float Celsius = 0;
float Fahrenheit = 0;

char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

long duration, distance; // Duration used to calculate distance

int NivelMax=0;
int NivelMin=0;

char currentTime[9];
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;


BLYNK_CONNECTED() 
  {
    Blynk.syncAll();  // Synchronize hardware with App widgets when connected
    Blynk.syncVirtual(V10); //  Synchronize Time Input Widget when connected 
  }

BLYNK_WRITE(V0) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    x = param.asInt();  // set variable as Slider value
    EncWV1 = x;
    Serial.print("Wave Maker 1 On Timer set to: ");
    Serial.print(EncWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V1) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    y = param.asInt();  // set variable as Slider value 
    ApagWV1 = y;  
    Serial.print("Wave Maker 1 Off Timer set to: ");
    Serial.print(ApagWV1);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V2) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    z = param.asInt();  // set variable as Slider value
    EncWV2 = z;
    Serial.print("Wave Maker 2 On Timer set to: ");
    Serial.print(EncWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V3) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    a = param.asInt();  // set variable as Slider value 
    ApagWV2 = a;  
    Serial.print("Wave Maker 2 Off Timer set to: ");
    Serial.print(ApagWV2);
    Serial.println(" seconds");
  }

BLYNK_WRITE(V7) 
  {   // add a slider to your project on V1 range 0 to 120 (seconds)
    NivelMax = param.asInt();  // set variable as Slider value  
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMax);
    Serial.println(" segundos");
  }

BLYNK_WRITE(V8) 
  {   // add a slider to your project on V0 range 0 to 120 (seconds)
    NivelMin = param.asInt();  // set variable as Slider value
    
    Serial.print("Encender bomba en: ");
    Serial.print(NivelMin);
    Serial.println(" segundos");
  }


BLYNK_WRITE(V10) {  // Called whenver setting Time Input Widget
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
}

void led_control()
  {
    //First Wave Maker
      
    if ((CounterOn > 0) && (ledStatus == 0))
      {
        digitalWrite(WM1,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 1: ON\n");
        ledStatus = 1;
        CounterOff = 0;
      }
   
  if ((CounterOn > 0) && (ledStatus == 1))
      {
      CounterOn = (CounterOn - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
    {
      CounterOff = ApagWV1;
    }

  if ((CounterOff > 0) && (ledStatus == 1))
  {
      digitalWrite(WM1,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 1: OFF\n");
      ledStatus = 0;
      CounterOn = 0;
   }
   
  if ((CounterOff > 0) && (ledStatus == 0))
      {
      CounterOff = (CounterOff - 1);
      }
      
  if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
    {
      CounterOn = EncWV1+1;
    }

  //Second Wave Maker

  if ((CounterOn2 > 0) && (ledStatus2 == 0))
      {
        digitalWrite(WM2,HIGH);   //write to pin to turn the LED on.
        Serial.print("Wave Maker 2: ON\n");
        ledStatus2 = 1;
        CounterOff2 = 0;
      }
   
  if ((CounterOn2 > 0) && (ledStatus2 == 1))
      {
      CounterOn2 = (CounterOn2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 1))
    {
      CounterOff2 = ApagWV2;
    }

  if ((CounterOff2 > 0) && (ledStatus2 == 1))
  {
      digitalWrite(WM2,LOW);   //write to pin to turn the LED off.
      Serial.print("Wave Maker 2: OFF\n");
      ledStatus2 = 0;
      CounterOn2 = 0;
   }
   
  if ((CounterOff2 > 0) && (ledStatus2 == 0))
      {
      CounterOff2 = (CounterOff2 - 1);
      }
      
  if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 0))
    {
      CounterOn2 = EncWV2+1;
    }

}

void MeasureCm()
  {
   /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */ 
  
       
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
  
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); 
       
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
       
   //Calculate the distance (in cm) based on the speed of sound.
   distance = duration/58.2;
  
   Serial.print("Distance: ");
   Serial.println(distance); 
   Blynk.virtualWrite(4,distance);   

   if (distance>NivelMax)
     {
      digitalWrite(AutoRelleno, HIGH);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 1.
      AR.on();
      Serial.print("Bomba de Autorelleno Encendida");
     }
   if (distance<NivelMin)
     {
      digitalWrite(AutoRelleno, LOW);
      WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 5.
      AR.off();
      Serial.print("Bomba de Autorelleno Apagada");
     }
   }

void Temperatura()
  {
    sensors.requestTemperatures();
    Celsius = sensors.getTempCByIndex(0);
    Blynk.virtualWrite(6,Celsius);
    Serial.print("Temperatura: ");
    Serial.print(Celsius);
    Serial.print(" C");
  }

void EncendidoSumpLamp() {  // 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) {
    if (minute() == STmin) {
      Serial.println("Doing something now");
      digitalWrite(SumpLamp, HIGH); // Turn ON built-in LED
    } else if (minute() < STmin) {
      Serial.println("Will do something");
    } else if (minute() > STmin) {
      Serial.println("Did something");
    } else {
      Serial.println("Clueless");
    }
  }

  if (hour() == SPhour) {
    if (minute() == SPmin) {
      Serial.println("Stopping something now");
      digitalWrite(SumpLamp, LOW); // Turn OFF built-in LED
    } else if (minute() < SPmin) {
      Serial.println("Will stop something");
    } else if (minute() > SPmin) {
      Serial.println("Stopped something");
    } else {
      Serial.println("Clueless");
    }
  }
  Serial.println("----------");
}


void setup()
{
  if (EncWV1== 0)
  {
    EncWV1=1;
  }
  if (ApagWV1== 0)
  {
    ApagWV1=1;
  }

  sensors.begin();
  Serial.begin(9600); 
  Blynk.begin(auth);
  rtc.begin();
  
  CounterOn = EncWV1+1;
  // run timer every 1 second to check for led On/Off action
  timer.setInterval(1000L, led_control);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(12, OUTPUT);
     
  timer.setInterval(1000L, MeasureCm);
  timer.setInterval(20000L, Temperatura);
  timer.setInterval(1000L, EncendidoSumpLamp); // EncendidoSumpLamp Time Check     
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates Timer to ensure the timer keeps running  
} 

Works fine for a time, then wen the advanced input timmer run out it crash everything and lost conection.


#25