Simple 4 relay irrigation scheduler

strange I doubled check now by copying from my post and it work on my side.

@hagay
now it’s ok for me
thank you

OK. Made some changes to add system disable button.
To disable the system you have to define number of days to disable (a slider on V33) ,
and press the disable button (V32).
If you set V33 to 1 the system will disable until midnight if you set it to 2 the system will disable until tomorrow midnight …
When pressing the disable button all switches are turned off.
The disable date will be presented on V32 button.
Again this is not fully tested (each iteration is several days to test) so please let me know if you find bugs.

enjoy.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#define WIFI_LED 13

// define configuration (number of switches and number of timers)
#define SWITCH_CNT 4
#define TIME_CNT 4

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxxxxxxxxx";

//byte switch_pins[] = {12 , 5 , 4 , 15}; // number of gpio to be used as switch/relay control
byte switch_pins[] = {2 , 5 , 14 , 15}; // number of gpio to be used as switch/relay control

bool switch_default[] = {LOW,HIGH,LOW,HIGH};
////////////////////////////////////////////////////////////////////////////////////////
// This code can control up to 4 switches                                             //
// For each switch up to 4 schedule start and end times can be configured - (V0..V15) //
// A default duration can be defined per switch                           - (V16..V19)//
// If an end time is not defined at the app the default duration is used              //
// default duration is also used when manually activating the switch       - (V20..V23)//
// for each switch when start time is reached the switch turns on for the duration    //
//                                                                                    //
// maximum duration is used for safety (to limit activation time)         - (V24..V27)//
////////////////////////////////////////////////////////////////////////////////////////
int  start_time_sec[SWITCH_CNT][TIME_CNT];     // array of 4 start times (in seconds) for 4 switches [switch number][schedual timer number]
bool start_valid[SWITCH_CNT][TIME_CNT];        // is the start time valid ?
bool weekdays[SWITCH_CNT][TIME_CNT][8];        // array of 8 days (day 0 not used) for each schedule time
int  active_duration[SWITCH_CNT][TIME_CNT+1];  // duration per switch per time(in sec)
bool end_valid[SWITCH_CNT][TIME_CNT];          // is the end time valid ?
int  max_duration[SWITCH_CNT];                 // max duration per switch

// when activating a switch a timer is set for the configured duration
// when the duration ends the switch is turned off by the timer
// the id of the timer is saved using end_timer_id
// if switch is manually turned off the end_timer_id is used to stop the timer.
// end_timer_id is initialized to 32 (per entry) at the setup section
int end_timer_id[SWITCH_CNT];

// saves id of main timer
int main_timer_id;

// system disable parameters
int  system_disable_days;                      // V33 defines days to disable the system (including today)
int  disable_timer_id = 32;                    // when system is disabled a timer is set to wake the system disable_timer_id saves this timer ID

// timer object declaration
BlynkTimer timer;

// this code use Real Time Clock widget in the blynk app to keep the clock updated from net
WidgetRTC rtc;


BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
  Blynk.syncAll();
}

//////////////////////////////////////////////////////////
// get schedual parameters from App                     //
//////////////////////////////////////////////////////////
// this function is called when ever a timer is set from the APP (V0..V15)
// it receive the start (and optionally end) time from the time input widget
//  and saves them in the appropriate arrays 
void set_time(BlynkParam param, byte switch_no, byte time_no){

     TimeInputParam t(param);
  // Process start time

  if (t.hasStartTime())
  {
    
    Serial.println(String("Start: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
    Serial.println(String("Start in sec: ") + param[0].asLong() + String(" for switch") + switch_no + String(" time_no: ") + time_no);
    start_time_sec[switch_no][time_no]= param[0].asLong();
    start_valid[switch_no][time_no] = true;

  }
  else
  {
    // Do nothing
    Serial.println(String("No Start Time Given for switch: ") + switch_no + String(" time_no: ") + time_no);
    start_valid[switch_no][time_no] = false;
  }



  //////////////////////////////////////////////////////////////////////////////
  // check if end time is received convert and save it as day time in seconds //
  //////////////////////////////////////////////////////////////////////////////
  if (t.hasStopTime())
  {
    
    Serial.println(String("Stop: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
    Serial.println(String("Stop in sec: ") + param[1].asLong() + String(" for switch") + switch_no + String(" time_no: ") + time_no);

    active_duration[switch_no][time_no] = (param[1].asLong()-start_time_sec[switch_no][time_no]);
    
    // if end time is smaller than start time this means end time is at next day
    if (active_duration[switch_no][time_no]<0) active_duration[switch_no][time_no]=86400+active_duration[switch_no][time_no];
    
    Serial.println(String("Stop duration: ") + active_duration[switch_no][time_no]);

    end_valid[switch_no][time_no] = true;


  }
  else // if end time is not defined //
  {
    // Do nothing
    Serial.println(String("No Stop Time Given for switch: ") + switch_no + String(" time_no: ") + time_no);
    end_valid[switch_no][time_no] = false;
  }

 
  // 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");
      weekdays[switch_no][time_no][i] = true;
    }
    else {
      weekdays[switch_no][time_no][i] = false;
    }
    
  }

    Serial.println();

}

// V0..V3 for switch 0, V4..V7 for switch 1 ...
BLYNK_WRITE(V0)  { set_time(param, 0,0);  }
BLYNK_WRITE(V1)  { set_time(param, 0,1);  }
BLYNK_WRITE(V2)  { set_time(param, 0,2);  }
BLYNK_WRITE(V3)  { set_time(param, 0,3);  }

BLYNK_WRITE(V4)  { set_time(param, 1,0);  }
BLYNK_WRITE(V5)  { set_time(param, 1,1);  }
BLYNK_WRITE(V6)  { set_time(param, 1,2);  }
BLYNK_WRITE(V7)  { set_time(param, 1,3);  }

BLYNK_WRITE(V8)  { set_time(param, 2,0);  }
BLYNK_WRITE(V9)  { set_time(param, 2,1);  } 
BLYNK_WRITE(V10) { set_time(param, 2,2);  }
BLYNK_WRITE(V11) { set_time(param, 2,3);  }

BLYNK_WRITE(V12) { set_time(param, 3,0);  }
BLYNK_WRITE(V13) { set_time(param, 3,1);  }
BLYNK_WRITE(V14) { set_time(param, 3,2);  }
BLYNK_WRITE(V15) { set_time(param, 3,3);  }




// use a slider to define default activation duration (slider count in minute)
BLYNK_WRITE(V16) { active_duration[0][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V17) { active_duration[1][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V18) { active_duration[2][TIME_CNT] = param.asInt()*60; }
BLYNK_WRITE(V19) { active_duration[3][TIME_CNT] = param.asInt()*60; }

// use a slider to define default max duration (slider count in minute)
BLYNK_WRITE(V24) { max_duration[0] = param.asInt()*60; }
BLYNK_WRITE(V25) { max_duration[1] = param.asInt()*60; }
BLYNK_WRITE(V26) { max_duration[2] = param.asInt()*60; }
BLYNK_WRITE(V27) { max_duration[3] = param.asInt()*60; }

/////////////////////////////////////////////////////////////////
// Handle switch events (from app or from scheduler )          //
/////////////////////////////////////////////////////////////////

// turn off switch after active duration ends
// duration number is not important here 
void turn_off_switch_no_0(){ turn_on_off(0,0,0); Blynk.virtualWrite(V20,0); Serial.println(String("timer turn off switch 0 ") );}
void turn_off_switch_no_1(){ turn_on_off(0,1,0); Blynk.virtualWrite(V21,0); Serial.println(String("timer turn off switch 1 ") );}
void turn_off_switch_no_2(){ turn_on_off(0,2,0); Blynk.virtualWrite(V22,0); Serial.println(String("timer turn off switch 2 ") );}
void turn_off_switch_no_3(){ turn_on_off(0,3,0); Blynk.virtualWrite(V23,0); Serial.println(String("timer turn off switch 3 ") );}

// handle switch state
void turn_on_off(int on_off, byte switch_no , byte time_no){
    long active_duration_ms ;
    char Time_print[16];
    
    if (on_off==1) 
    {
      // create time as string to print on activation button
      sprintf(Time_print, "%02d:%02d", hour(), minute());

      // turn on the switch (or off if default is on)
      digitalWrite(switch_pins[switch_no],!switch_default[switch_no]);

      // if end time is valid use the active duration assigned to this time
      // (change to msec will be done later)
      if (end_valid[switch_no][time_no])
         active_duration_ms = ((long)active_duration[switch_no][time_no]);
      else // otherwise use the general time duration
         active_duration_ms = ((long)active_duration[switch_no][TIME_CNT]);

      
      // max duration smaller than two min is not valid
      if ( (max_duration[switch_no]< 120) | (max_duration[switch_no]>active_duration_ms) )
         active_duration_ms = active_duration_ms*1000;
      else
        active_duration_ms = ((long)max_duration[switch_no])*1000;
       

      // if new timer is set before another one ended then disable previous timer
      if (end_timer_id[switch_no]!=32) timer.deleteTimer(end_timer_id[switch_no]);

      // turn on switch and set timer 
      switch (switch_no) {
        case 0: 
          Blynk.setProperty(V20, "onLabel", String(Time_print));
          Blynk.virtualWrite(V20,1);
          end_timer_id[0]=timer.setTimeout(active_duration_ms, turn_off_switch_no_0);
          
         break;
        case 1: 
          Blynk.setProperty(V21, "onLabel", String(Time_print));
          Blynk.virtualWrite(V21,1);
          end_timer_id[1]=timer.setTimeout(active_duration_ms, turn_off_switch_no_1);
         break;
        case 2: 
          Blynk.setProperty(V22, "onLabel", String(Time_print));
          Blynk.virtualWrite(V22,1);
          end_timer_id[2]=timer.setTimeout(active_duration_ms, turn_off_switch_no_2);
         break;
        case 3: 
          Blynk.setProperty(V23, "onLabel", String(Time_print));
          Blynk.virtualWrite(V23,1);
          end_timer_id[3]=timer.setTimeout(active_duration_ms, turn_off_switch_no_3);
         break;    
      } 
      Serial.println(String("turn ON switch: ") + switch_no + String(" for duration: ") + active_duration_ms/60000 + String("min "));
    }
    else 
    {
      digitalWrite(switch_pins[switch_no],switch_default[switch_no]);
      timer.deleteTimer(end_timer_id[switch_no]);
      end_timer_id[switch_no]=32;
      Serial.println(String("turn OFF switch: ") + switch_no);
    }   
}

// set switch state from APP
BLYNK_WRITE(V20) { turn_on_off(param.asInt(),0,TIME_CNT); }
BLYNK_WRITE(V21) { turn_on_off(param.asInt(),1,TIME_CNT); }
BLYNK_WRITE(V22) { turn_on_off(param.asInt(),2,TIME_CNT); }
BLYNK_WRITE(V23) { turn_on_off(param.asInt(),3,TIME_CNT); }


/////////////////////////////////////////////////////////////////////////
// System Disable                                                      //
/////////////////////////////////////////////////////////////////////////
// when V32 is set the system is entering sleep mode
// all switches are turned off and the system main time tick (the timer that calls activetoday function)
//  is disabled


// following function is used to activate main time tick after disable days are over
void restart_system() {
        timer.enable(main_timer_id);  
        Blynk.virtualWrite(V32,0);
}

// V33 is used to define number of days to disable
BLYNK_WRITE(V33) {system_disable_days = param.asInt()*86400000;}

// V32 is used to disable the system main time tick and to deactivate all switches
BLYNK_WRITE(V32) {
  if (param.asInt()!=0)
    { 
      if (system_disable_days!=0)
       {
        // save current time in msec. this is used to calculate msecs until end of today
        unsigned int nowmseconds = ((hour() * 3600) + (minute() * 60) + second())*1000;

        // current data is printed on V32 button when system is disabled 
        String currentDate = String(day()) + "\\" + month() + "\\" + year();

        Serial.println(String("Disable all"));
        // disable main time tick 
        timer.disable(main_timer_id);       

        // create a timer to wake the system up after system_disable_days are over
        disable_timer_id = timer.setTimeout(system_disable_days-nowmseconds,restart_system);
        // print current data on V32 button
        Blynk.setProperty(V32, "onLabel", currentDate);
       }
       else
       {
        // if system_disable_days is zero then do nothing
        Blynk.virtualWrite(V32,0);
       }

      // turn off all switches regardless of system_disable_days
      turn_off_switch_no_0();
      turn_off_switch_no_1();
      turn_off_switch_no_2();
      turn_off_switch_no_3();  
    }
  else
    {
      // if system is manually enabled delete disable timer and enable system time tick
      timer.deleteTimer(disable_timer_id);
      timer.enable(main_timer_id);             
    }
  
  }


/////////////////////////////////////////////////////////////////////////////////////////////
// the following function is called every 60 seconds by a timer                            //
//  the function checks if a start time is reached and if yes it will call                 //
//   the turn_on_off function (to turn on the switch and set timer for turning off switch) //
/////////////////////////////////////////////////////////////////////////////////////////////
void activetoday(){         // check if schedule #1 should run today

  if(Blynk.connected()) // set wifi led if no connection
  {
    digitalWrite(WIFI_LED,LOW); 
  }
  else
  {
    digitalWrite(WIFI_LED,HIGH); 
  }
  
  if(year() != 1970){
    unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment = 6; // needed for Sunday Time library is day 1 and Blynk is day 7
    }
   
 
     for (int switch_cnt = 0;  switch_cnt< SWITCH_CNT; switch_cnt++) {
         for (int timer_cnt = 0;  timer_cnt< TIME_CNT; timer_cnt++) {
              if (start_valid[switch_cnt][timer_cnt] == true) {
                if (weekdays[switch_cnt][timer_cnt][weekday() + dayadjustment]==true){
                  
                   if (nowseconds >= start_time_sec[switch_cnt][timer_cnt]){
                      
                      if(nowseconds < start_time_sec[switch_cnt][timer_cnt]+90){
                        turn_on_off(1,switch_cnt,timer_cnt);
                        Serial.println(String("turn ON switch: ") + switch_cnt);

                      }
                   }
                }
              }
         }
      
     }
 
  } 
}

/////////////////////////////////////
// BLYNK 
void setup()
{
  // Debug console
  Serial.begin(9600);
  
  // reset output pins used and system variables
  for (int i = 0; i<SWITCH_CNT ; i++){
    pinMode(switch_pins[i], OUTPUT); // reset output pins
    digitalWrite(switch_pins[i],switch_default[i]); // set switch to default
    end_timer_id[i] = 32;            // reset end timer id's

    for (int j = 0; j<TIME_CNT ; j++)
    {
      end_valid[i][j] = false;       // reset end valid array
    }
    
  }

  
  pinMode(WIFI_LED, OUTPUT);

  
  Blynk.begin(auth, ssid, pass);
  main_timer_id = timer.setInterval(60000L, activetoday);  // check every 60s if ON / OFF trigger time has been reached
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
  

}

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

3 Likes

good job @hagay
do you plan to include a “self-run” when no wifi signal is detected? (wifi modem router down)

As far as I know (and it seams to be working on my garden) the Blynk RTC will keep time when no wifi connection.
if no wifi you will not be able to change timers but everything that was configured during wifi up time will be maintained.

ok, I’will simulate that kind off issue , just down my local server and see what happens

many thanks
:wink:

Thanks mate. This is a great addition to your project @hagay. I’ve almost finished running my irrigation hoses and have the sprinkler heads in place. Hopefully by the end of next week it should all be done.

Hagay—Thanks again. This new version works great. Solves the issue I was having with using multiple times. I like the efficiency of the code. I really like the way you write the button label with the start time.

I know you said you did not fully test this. I did notice that when I cycle thru the two time cycles the button label will pickup the wrong start time every other start. Not a big deal. This only affects button label. The time cycle is still correct. Manually starting always labels button correctly.

I study your code and I am learning a lot. Thank you

I like the new buttons Blynk added

Button label shows start time

1 Like

I Like Your GUI!!
Can you post the QR code ?

About the label I think the problem is with the way activetoday checks current time.
There is a guard time of 90 sec that makes activetoday fire twice for every start time.
Solving it will require some changes (RTC time and active today calls are not sync since RTC may get some time aliments from the web).

I am currently making some big changes to the code to make number of switches and timers configurable using defines.
Not sure if I post the new code here or maybe open a new project (since it is no longer 4 relay and not so simple).

In any case the fix will be in the new code.

2 Likes

This is what I use to control my system. LinkNode R4. It is about $10. Uses ESP 8266 chip. I flash it with Node MCU to blow out the original “junk” programming. I adapted Hagay’s program to work with this device. It uses different GPIO and different LED settings

2 Likes

QRC. Please keep in mind my code is adapted from hagay’s code. I modified it to work with my relay board.

From code I can set timerInput in time use ok work. But can not choose a week. How to fix it to make choose a week it work.

#define RELAY_PIN            D0    // R1 output pin
#define RELAY_PIN2           D1    // R2 output pin
//#include "DHT.h"
#include <SPI.h>
//#include <WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon). 

//********************* blobal variable ************************
// Your WiFi credentials.
// Set password to "" for open networks.
char auth[] = "xxxxx";
char server[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";
int port = 8080;

//BlynkTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V1);
WidgetLED led1(V8);
WidgetLED led2(V7);
char _HIGH = LOW;
char _LOW = HIGH;

//********************* End blobal variable ************************

//********************* helper function ****************************

class TimeInterval{
  public:
     int Start = 0;
     int End = 0;
     int RelayID = 1;
};

TimeInterval t1;
TimeInterval t2;
TimeInterval t3;
TimeInterval t4;

TimeInterval checkTimes[] = {t1,t2,t3,t4};

void requestTime() {
  Blynk.sendInternal("rtc", "sync");
}



bool curState = false;
bool curState2 = false;

int selectedMode = 1; // manual
bool after1stCycle = false;

// Check current time with timer array value
bool IsONTIME(int id, int cTime)
{
   TimeInterval t = checkTimes[id];

   bool ret = (t.Start <= cTime)&&(cTime <= t.End);
   return ret;
}

//Display rtc time to label widget
void ShowTimeFromRTC()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + "/" + month() + "/" + year();
  String v = currentDate + " " + currentTime;
  Blynk.virtualWrite(V3, v);
}

// validate current time with timers
void validateTimer()
{
  checkRelayState();
  after1stCycle = true;
  ShowTimeFromRTC();
  if(selectedMode == 2){return;} // not allow to change state in manual mode
   
   int hnow = (hour() * 60) + minute();
   bool nState = false;
   
   Serial.print("Time =: ");
   Serial.print(hnow);

   int relay = 0;
   for (int i = 0;  i < 4; i++)
   {
     
     if(IsONTIME(i, hnow))
     {
      // present time is inside checkTimes[i] interval
        nState = true;
        Serial.print(" Valid to timer ");
        Serial.print(i);
        relay = checkTimes[i].RelayID;
        break;
     }
     else
     {
       // present time is not inside checkTimes[i] interval
     }
   }

   if(!nState)
   {
    // no timer active at this time then reset all output relay
      Serial.print(" not Valid to any timer ");
      Serial.println(" ->OFF time");
      digitalWrite(RELAY_PIN, _LOW);   //send active low to OFF relay
      //Blynk.virtualWrite(V8, 0);
      led1.off();
      digitalWrite(RELAY_PIN2, _LOW);   //send active low to OFF relay
      //Blynk.virtualWrite(V7, 0);
      led2.off();
   }
   else
   {

    //Some timer active at this time
     Serial.print(" ->ON time");
     Serial.print(" ->Relay ");
     Serial.print(relay);
   }

    if(relay == 1)
    {
       Serial.print(" -> current state [");
       Serial.print(curState);
       Serial.print("] ");
       if(nState != curState)
       {
         Serial.println(" state Changed.");
         curState = nState;
    
         if(nState)
         {
          digitalWrite(RELAY_PIN, _HIGH);  //send active high to ON relay
          //Blynk.virtualWrite(V1, 1);
          Blynk.virtualWrite(V8, 1);
          led1.on();
         }
         else
         {
          digitalWrite(RELAY_PIN, _LOW);   //send active low to OFF relay
          //Blynk.virtualWrite(V1, 0);
          Blynk.virtualWrite(V8, 0);
          led1.off();
         }
       }
       else
       {
          Serial.println(" state not Change.");
       }
    }
    else if(relay == 2)
    {
       Serial.print(" -> current state [");
       Serial.print(curState2);
       Serial.print("] ");
       if(nState != curState2)
       {
         Serial.println(" state Changed.");
         curState2 = nState;
    
         if(nState)
         {
          digitalWrite(RELAY_PIN2, _HIGH);  //send active high to ON relay
          //Blynk.virtualWrite(V2, 1);
          Blynk.virtualWrite(V7, 1);
          led2.on();
         }
         else
         {
          digitalWrite(RELAY_PIN2, _LOW);   //send active low to OFF relay
          //Blynk.virtualWrite(V2, 0);
          Blynk.virtualWrite(V7, 0);
          led2.off();
         }
       }
       else
       {
          Serial.println(" state not Change.");
       }
    }

   
}

// Check output pins state and update to LED widget
void checkRelayState()
{
  boolean pressed = (digitalRead(RELAY_PIN) == _HIGH);
  if (pressed) {
      led1.on();
    } else {
      led1.off();
    }
  curState = pressed;

  boolean pressed2 = (digitalRead(RELAY_PIN2) == _HIGH);
  if (pressed2) {
      led2.on();
    } else {
      led2.off();
    }
  curState2 = pressed2;
}

// initial timers interval
void SetTimeIntervals()
{
  checkTimes[0].Start = 1 *60;
  checkTimes[0].End = 4 * 60;
  checkTimes[1].Start = 7 *60;
  checkTimes[1].End = 10 * 60;
  checkTimes[2].Start = 13 *60;
  checkTimes[2].End = 16 * 60;
  checkTimes[2].RelayID = 2;
  checkTimes[3].Start = 19 *60;
  checkTimes[3].End = 22 * 60;
  checkTimes[3].RelayID = 2;
}


//*********************************************************************

//*********************** Blynk Handler********************************
BLYNK_CONNECTED() {
  
  Blynk.syncAll();
  // Synchronize time on connection
  rtc.begin();
}


int tStart = 0;
int tEnd = 0;
int timerID = 0;
int relayID = 1;
// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget

// Terminal widget
BLYNK_WRITE(V0)
{

  // if you type "Marco" into Terminal Widget - it will respond: "Polo:"
  if (String("Marco") == param.asStr()) 
  {
    terminal.println("You said: 'Marco'") ;
    terminal.println("I said: 'Polo'") ;
  } 
  else 
  {
    // Send it back
    terminal.print("You said:");
    terminal.write(param.getBuffer(), param.getLength());
    terminal.println();
  }

  // Ensure everything is sent
   //terminal.flush();
}

//Button Widget for Relay1
BLYNK_WRITE(V1) 
{
  if(selectedMode == 1){return;} // not allow to change state in auto mode
  int s = param.asInt();

  curState = s > 0;

   if(curState)
   {
    digitalWrite(RELAY_PIN, _HIGH);  //send active high to ON relay
    led1.on();
    terminal.print("Relay 1 On by user in manual mode");
   }
   else
   {
    digitalWrite(RELAY_PIN, _LOW);   //send active low to OFF relay
    led1.off();
    terminal.print("Relay 1 Off by user in manual mode");
   }
}

//Button Widget for relay2
BLYNK_WRITE(V2) 
{
  if(selectedMode == 1){return;} // not allow to change state in auto mode
  int s = param.asInt();

  curState2 = s > 0;

   if(curState2)
   {
    digitalWrite(RELAY_PIN2, _HIGH);  //send active high to ON relay
    led2.on();
    terminal.print("Relay 2 On by user in manual mode");
   }
   else
   {
    digitalWrite(RELAY_PIN2, _LOW);   //send active low to OFF relay
    led2.off();
    terminal.print("Relay 2 Off by user in manual mode");
   }
}

// Menu Widget  for mode select
BLYNK_WRITE(V9) 
{
  //if(!after1stCycle){return;}
  switch (param.asInt())
  {
    case 1: // Item 1
      selectedMode = 1;
      Serial.println("Mode = Auto");
      terminal.println("Mode = Auto") ;
      break;
    case 2: // Item 2
      selectedMode = 2;
      Serial.println("Mode = Manual");
      terminal.println("Mode = Manual") ;
      break;
    default:
      selectedMode = -1;
      Serial.println("Mode = N/A");
      terminal.println("Mode = N/A") ;
  }
}

String tz = "";

//Time Input widget 
BLYNK_WRITE(V10) 
{
  if(!after1stCycle){return;}
  // handle start end time
  TimeInputParam t(param);
  tStart = (t.getStartHour() * 60) + t.getStartMinute();
  tEnd = (t.getStopHour() * 60) + t.getStopMinute();

  tz = t.getTZ();
  Serial.println(tz);
  Serial.println(tStart);
  Serial.println(tEnd);
}

// Menu Widget  for relay selection
BLYNK_WRITE(V11) 
{
  if(!after1stCycle){return;}
  switch (param.asInt())
  {
    case 1: // Item 1
      relayID = 1;
      Serial.println("Relay select = R1");
      terminal.println("Relay select = R1");
      break;
    case 2: // Item 2
      relayID = 2;
      Serial.println("Relay select = R2");
      terminal.println("Relay select = R2");
      break;
    default:
      selectedMode = -1;
      Serial.println("Relay select = None");
      terminal.println("Relay select = None") ;
  }
}

// Menu Widget  for timer selection
BLYNK_WRITE(V12) 
{

  if(!after1stCycle){return;}
  switch (param.asInt())
  {
    case 1: // Item 1
      timerID = 0;
      Serial.println("Timer 0 selected");
      terminal.println("Timer 0 selected") ;
      Blynk.setProperty(V13, "label", "Set Timer0");
      Blynk.virtualWrite(V10, checkTimes[0].Start * 60, checkTimes[0].Start * 60, tz);
      Blynk.virtualWrite(V11, checkTimes[0].RelayID);
      break;
    case 2: // Item 2
      timerID = 1;
      Serial.println("Timer 1 selected");
      terminal.println("Timer 1 selected") ;
      Blynk.setProperty(V13, "label", "Set Timer1");
      Blynk.virtualWrite(V10, checkTimes[1].Start * 60, checkTimes[1].Start * 60, tz);
      Blynk.virtualWrite(V11, checkTimes[1].RelayID);
      break;
    case 3: // Item 3
      timerID = 2;
      Serial.println("Timer 2 selected");
      terminal.println("Timer 2 selected") ;
      Blynk.setProperty(V13, "label", "Set Timer2");
      Blynk.virtualWrite(V10, checkTimes[2].Start * 60, checkTimes[2].Start * 60, tz);
      Blynk.virtualWrite(V11, checkTimes[2].RelayID);
      break;
    case 4: // Item 3
      timerID =3;
      Serial.println("Timer 3 selected");
      terminal.println("Timer 3 selected") ;
      Blynk.setProperty(V13, "label", "Set Timer3");
      Blynk.virtualWrite(V10, checkTimes[3].Start * 60, checkTimes[3].Start * 60, tz);
      Blynk.virtualWrite(V11, checkTimes[3].RelayID);
      break;
    default:
      timerID = -1;
      Serial.println("No Timer selected");
      terminal.println("No Timer selected") ;
      Blynk.setProperty(V13, "label", "Set Timer");
  }
}

// Button widget for Set Timer value
BLYNK_WRITE(V13) 
{
  if(!after1stCycle){return;} // not update timer when blynk sync at begin
  if(timerID < 0){return;} // if timer id < 0,  not update
  int s = param.asInt();

  if(s == 1)
  {

     checkTimes[timerID].Start = tStart;
     checkTimes[timerID].End = tEnd; 
     checkTimes[timerID].RelayID = relayID;
     String s = "Timer " + String(timerID) + ".Start = " + String(checkTimes[timerID].Start) + " .End = " + String(checkTimes[timerID].End) + " .Relay = " + String(checkTimes[timerID].RelayID);
     Serial.println(s); 
  }
}

//*********************************************************************


void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial.println("Connecting to Blynk Server");
  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();
  Serial.println("\Starting");
  int mytimeout = millis() / 1000;
   while (Blynk.connect() == false) { // try to connect to server for 10 seconds
    if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
       break;
    }
  }
  
  //Blynk.begin(auth, ssid, pass); // Blynk Server

  //Blynk.begin(auth, ssid, pass,IPAddress(XX,XX,XX,XX), 8080); // Local Server

  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  SetTimeIntervals();
  
  // check hour segment every 10 seconds
  timer.setInterval(1000L, validateTimer);

  pinMode(RELAY_PIN, OUTPUT); // set pin as output to relay R1
  pinMode(RELAY_PIN2, OUTPUT); // set pin as output to relay R2

  // Display current setting time interval in combination of ( hour * 60 + minute)
  for(int i = 0; i < 4; i++)
  {
    String s = "Timer " + String(i) + ".Start = " + String(checkTimes[i].Start) + " .End = " + String(checkTimes[i].End)+ " .Relay = " + String(checkTimes[timerID].RelayID);
    Serial.println(s);
  }
  checkRelayState();
  //ShowTimeFromRTC();
}

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

35347263_1971391442931233_7477284294193315840_n

what do you mean ?
do you need to program a full week or a paticular week in the year ?

1 Like

I mean choose days of week
such as choose a Sun, Tue, Thu and Sat Timer On Reray But Mon, Wed and Fri Timer not work.

Untitled

I need add choose days of week to my code

you have to use isWeekdaySelected function

//**************** TIMER 1 ***********************
BLYNK_WRITE(V10) {   // Scheduler #1 Time Input widget

  Serial.println("*******************************************");
  Serial.println("timers");
  Serial.println("*******************************************");
  Serial.println("");

  TimeInputParam t(param);

  nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
  stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);



  if (t.isWeekdaySelected((weekday() + dayadjustment))) {
    //Schedule is ACTIVE today
    if (nowseconds >= startseconds && nowseconds <= stopseconds) {
      Timer1 = true;
     
      Blynk.virtualWrite(Vx, 255);  // turn on virtual LED
    }
    else {
      Timer1 = false;
           Blynk.virtualWrite(Vx, 0);   // turn OFF virtual LED
    }
  }
  else {
    Timer1 = false;
       Blynk.virtualWrite(Vx, 0);   // turn OFF virtual LED
  }
}
int relay = 0;
   for (int i = 0;  i < 4; i++)
   {
     
     if(IsONTIME(i, hnow))
     {
      // present time is inside checkTimes[i] interval
        nState = true;
        Serial.print(" Valid to timer ");
        Serial.print(i);
        relay = checkTimes[i].RelayID;


        break;
     }
     else
     {
       // present time is not inside checkTimes[i] interval
     }
   }

@hagay I need add a isWeekdaySelected function to save a value “Weekday” to array type.
such as

  for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
      weekdays[switch_no][time_no][i] = true;
    }
    else {
      weekdays[switch_no][time_no][i] = false;
    }
    
  }

It seems correct

I really like this code and system, but cannot seem to get it to verify. I keep getting this:

Users/craig/Documents/Arduino/fona_4valve-v3/fona_4valve-v3.ino: In function 'void restart_system()':
fona_4valve-v3:326: error: 'V32' was not declared in this scope
         Blynk.virtualWrite(V32,0);
                            ^
/Users/craig/Documents/Arduino/fona_4valve-v3/fona_4valve-v3.ino: In function 'void BlynkWidgetWriteV32(BlynkReq&, const BlynkParam&)':
fona_4valve-v3:351: error: 'V32' was not declared in this scope
         Blynk.setProperty(V32, "onLabel", currentDate);
                           ^
fona_4valve-v3:356: error: 'V32' was not declared in this scope
         Blynk.virtualWrite(V32,0);
                            ^
exit status 1
'V32' was not declared in this scope