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

Switching Relay time Project

Here is my code . I have few bug in below.

  1. When timing is in 4 the Zone LED will be Remain High in automode even mode change from Zone4 to Zone1

  2. If timing Set like 10:00:00 to 11:00:00 It wont work. Work when 10:01 to 10:59 or 10:01 to 11:01

  3. In time Input Widget . for testing purpose i selected particular weekday for example
    zone1: allweekday
    Zone2: all week day
    zone3: diselcted on Wednesday i.e today
    zone 4: all weekdays
    I expected zone1,zone2,zone4 Relay on on respective days .Zone3 must not turn on Relay betwen start time and stop time. IN below code its turing on

     #include"glob.h"
    
     #define BLYNK_PRINT Serial
     #include <ESP8266WiFi.h>
     #include <BlynkSimpleEsp8266.h>
     #include <WidgetRTC.h>
     BlynkTimer timer;
    
     WidgetRTC rtc;  // Set RTC widget.
     char auth[] = "XXXXXXXXXXXXXXX";
     char ssid[] = "XXXX";
     char pass[] = "XXXX";
     WidgetLED led25(25);
     WidgetLED led22(22);
     WidgetLED led23(23);
     WidgetLED led24(24);
    
    
    
     void setup()
     {
       Serial.begin(115200);
    
       Blynk.begin(auth, ssid, pass);
       pinMode(D5, OUTPUT);// Used for Temprature relay
       pinMode(D7, OUTPUT);// Used for SUN_Sat Relay
       pinMode(D6, OUTPUT);// Used for Mon_FRI Relay
       pinMode(D8, OUTPUT);// Used for ALL Day Relay
       rtc.begin();
       setSyncInterval(360);
       timer.setInterval(10000L, TimeZone1_Function);  // Update Time Check every 10 seconds
       timer.setInterval(10000L, TimeZone2_Function);  // Update Time Check every 10 seconds
       timer.setInterval(10000L, TimeZone3_Function); // Update Time Check every 10 seconds
       timer.setInterval(10000L, TimeZone4_Function); // Update Time Check every 10 seconds
       timer.setInterval(1000, Chk_manual_Auto_Status);
    
     }
    
    
     void loop()
     {
       Blynk.run();
       timer.run();
    
    
       sprintf(Date, "%04d/%02d/%02d", year() , month() , day() );
       sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
       Blynk.virtualWrite(V11, Date);
       Blynk.virtualWrite(V10, Time);
    
    
    
     }
    
     BLYNK_CONNECTED()
     {
       Blynk.syncVirtual(V3); // sync timeinput widget
       Blynk.syncVirtual(V4); // sync timeinput widget
       Blynk.syncVirtual(V5); // sync timeinput widget
       Blynk.syncVirtual(V6); // sync timeinput widget
       Blynk.syncVirtual(V2);
       Blynk.syncVirtual(V50);
    
     }
    
    
     BLYNK_WRITE(V3)// TimeZone1 TimeInput Store in below variable
     {
    
       TimeInputParam t(param);
       SThour = t.getStartHour();
       STmin = t.getStartMinute();
       STsec = t.getStartSecond();
       SPhour = t.getStopHour();
       SPmin = t.getStopMinute();
       SPsec = t.getStopSecond();
    
     }
    
    
     BLYNK_WRITE(V4)// TimeZone2 TimeInput Store in below variable
     {
    
    
       TimeInputParam t(param);
       Wk_Emd_SThour = t.getStartHour();
       Wk_Emd_STmin = t.getStartMinute();
       Wk_Emd_STsec = t.getStartSecond();
       Wk_Emd_SPhour = t.getStopHour();
       Wk_Emd_SPmin = t.getStopMinute();
       Wk_Emd_SPsec = t.getStopSecond();
    
     }
    
    
     BLYNK_WRITE(V5)// TimeZone3 TimeInput Store in below variable
     {
    
       TimeInputParam t(param);
       Al_SThour = t.getStartHour();
       Al_STmin = t.getStartMinute();
       Al_STsec = t.getStartSecond();
       Al_SPhour = t.getStopHour();
       Al_SPmin = t.getStopMinute();
       Al_SPsec = t.getStopSecond();
    
     }
    
    
    
     BLYNK_WRITE(V6)// TimeZone4 TimeInput Store in below variable
     {
    
       TimeInputParam t(param);
       TimeZ4__SThour = t.getStartHour();
       TimeZ4__STmin = t.getStartMinute();
       TimeZ4__STsec = t.getStartSecond();
       TimeZ4__SPhour = t.getStopHour();
       TimeZ4__SPmin = t.getStopMinute();
       TimeZ4__SPsec = t.getStopSecond();
    
     }
     BLYNK_WRITE(V51)  // Time Zone1 Button Selected
     {
       if (param.asInt() == 1)
       {
         Serial.println("Time Zone1 is selected ");
         TimeZone1 = 1;
         TimeZone2 = 0;
         TimeZone3 = 0;
         TimeZone4 = 0;
         Blynk.virtualWrite(V52, 0);
         Blynk.virtualWrite(V53, 0);
         Blynk.virtualWrite(V54, 0);
       } else
       {
         TimeZone1 = 0;
         led22.off();
       }
     }
    
    
     BLYNK_WRITE(V52)  // Time Zone2 Button Selected
     {
       if (param.asInt() == 1)
       {
         Serial.println("Time Zone 2 is selected ");
         TimeZone2 = 1;
         TimeZone3 = 0;
         TimeZone1 = 0;
         TimeZone4 = 0;
         Blynk.virtualWrite(V51, 0);
         Blynk.virtualWrite(V53, 0);
         Blynk.virtualWrite(V54, 0);
       } else
       {
         TimeZone2 = 0;
         led23.off();
       }
     }
    
     BLYNK_WRITE(V53)  // Time Zone3 Button Selected
     {
       if (param.asInt() == 1)
       {
         Serial.println("Time Zone3 selected ");
         // timer.setTimeout(50, resetTerminal);
         // timer.setTimeout(50, resetManual);
         // timer.setTimeout(50, checklastbuttonpressed);
         TimeZone3 = 1;
         TimeZone1 = 0;
         TimeZone2 = 0;
         TimeZone4 = 0;
         Blynk.virtualWrite(V51, 0);
         Blynk.virtualWrite(V52, 0);
         Blynk.virtualWrite(V54, 0);
    
       } else
       {
         TimeZone3 = 0;
         led24.off();
    
       }
     }
    
    
    
     BLYNK_WRITE(V54)  // Time Zone4 Button Selected
     {
       if (param.asInt() == 1)
       {
         Serial.println("Time Zone4 selected ");
         TimeZone4 = 1;
         TimeZone1 = 0;
         TimeZone2 = 0;
         TimeZone3 = 0;
         Blynk.virtualWrite(V51, 0);
         Blynk.virtualWrite(V52, 0);
         Blynk.virtualWrite(V53, 0);
    
       } else
       {
         TimeZone4 = 0;
         led25.off();
    
       }
     }
    

    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();
     }
    
       }
    
     }
    
    
    
     void TimeZone2_Function()
     {
    
    
       if (TimeZone2 == 1)
       {
    
         sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
         Serial.print("Current Time: ");
         Serial.println(currentTime);
    
         // Get Time Input Widget time
         sprintf(Sat_Sun_startTime, "%02d:%02d:%02d", Wk_Emd_SThour, Wk_Emd_STmin, Wk_Emd_STsec);
         Serial.print("Start Time: ");
         Serial.println(Sat_Sun_startTime);
    
         sprintf(Sat_Sun_stopTime, "%02d:%02d:%02d", Wk_Emd_SPhour, Wk_Emd_SPmin, Wk_Emd_SPsec);
         Serial.print("Stop Time: ");
         Serial.println(Sat_Sun_stopTime);
    
    
         if (hour() == Wk_Emd_SThour && hour() == Wk_Emd_SPhour)
         {
           if (minute() >= Wk_Emd_STmin && minute() <= Wk_Emd_SPmin)
           {
             Serial.println("RELAY IS ON"); Serial.println("..........................");
             digitalWrite(D7, HIGH); // Turn ON built-in LED
             led23.on();
           } else
           {
             Serial.println("RELAY IS OFF"); Serial.println("..........................");
             digitalWrite(D7, LOW); // Turn ON built-in LED
             led23.off();
           }
         }
         else
         {
           led23.off();
         }
       }
    
    
    
     }
    
    
    
    
     void TimeZone3_Function()
     {
    

    if (TimeZone3 == 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(AllDay_startTime, "%02d:%02d:%02d", Al_SThour, Al_STmin, Al_STsec);
     Serial.print("Start Time: ");
     Serial.println(AllDay_startTime);
    
     sprintf(AllDay_stopTime, "%02d:%02d:%02d", Al_SPhour, Al_SPmin, Al_SPsec);
     Serial.print("Stop Time: ");
     Serial.println(AllDay_stopTime);
    
    
     if (hour() >= Al_SThour && hour() <= Al_SPhour)
     {
       if (minute() >= Al_STmin && minute() <= Al_SPmin)
       {
         Serial.println("RELAY IS ON"); Serial.println("..........................");
         digitalWrite(D6, HIGH); // Turn ON built-in LED
         led24.on();
       } else
       {
         Serial.println("RELAY IS OFF"); Serial.println("..........................");
         digitalWrite(D6, LOW); // Turn ON built-in LED
         led24.off();
    >    }
    
    }
  } else
  {
    led24.off();
  }


}



void TimeZone4_Function()
{


  if (TimeZone4 == 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(TimeZone4SRTTime, "%02d:%02d:%02d", TimeZ4__SThour, TimeZ4__STmin, TimeZ4__STsec);
    Serial.print("Start Time: ");
    Serial.println(TimeZone4SRTTime);

    sprintf(TimeZone4STPTime, "%02d:%02d:%02d", TimeZ4__SPhour, TimeZ4__SPmin, TimeZ4__SPsec);
    Serial.print("Stop Time: ");
    Serial.println(TimeZone4STPTime);


    if (hour() >= TimeZ4__SThour && hour() <= TimeZ4__SPhour)
    {
      if (minute() >= TimeZ4__STmin && minute() <= TimeZ4__SPmin)
      {
        Serial.println("RELAY IS ON"); Serial.println("..........................");
        digitalWrite(D8, HIGH); // Turn ON built-in LED
        led25.on();
      } else
      {
        Serial.println("RELAY IS OFF"); Serial.println("..........................");
        digitalWrite(D8, LOW); // Turn ON built-in LED
        led25.off();
      }
    }
  }     else
      {
        led25.off();
      }


    }






    /* void checklastbuttonpressed ()
      {
      if ((TimeZone1 == 1) && (TimeZone2 == 0)) {
       oldstatus = 1;
      }
      if ((TimeZone1 == 0) && (TimeZone2 == 1)) {
       oldstatus = 2;
      }
      if ((TimeZone1 == 1) && (TimeZone2 == 1)) {
       oldstatus = 3;
      }
      if (TimeZone3 == 1) {
       oldstatus = 4;
      }
      // if(uptoyou==1){ oldstatus=5; }
      if ((TimeZone1 == 0) && (TimeZone2 == 0) && (TimeZone3 == 0)) {
       oldstatus = 5;
      }
      }*/

    /*
      void restorelastbuttonpressed ()
      {
      if (oldstatus == 1) {
        TimeZone1 = 1;
        Blynk.virtualWrite(V51, 1);
      }
      if (oldstatus == 2) {
        TimeZone2 = 1 ;
        Blynk.virtualWrite(V52, 1);
      }
      if (oldstatus == 3)
      {
        TimeZone2 = 1;
        TimeZone1 = 1;
        Blynk.virtualWrite(V51, 1);
        Blynk.virtualWrite(V52, 1);
      }
      if (oldstatus == 4) {
        TimeZone3 = 1;
        Blynk.virtualWrite(V53, 1);
      }

      if (oldstatus == 5)
      {
        TimeZone1 = 0;
        TimeZone2 = 0;
        TimeZone3 = 0;
        //uptoyou = 0;
        Blynk.virtualWrite(V51, 0);
        Blynk.virtualWrite(V52, 0);
        Blynk.virtualWrite(V53, 0);

      }
      }


    */


    BLYNK_WRITE(V50)  // Manual/Auto selection
    {
      if (param.asInt() == 0)

      {
        manual = 1;
        Serial.println("Manual mode is selected ");
        // restorelastbuttonpressed ();



      } else
      {
        manual = 0;
        Serial.println("AUTO mode is selected ");
        AutoMode_Function();

        //checklastbuttonpressed();
      }
    }


    void Chk_manual_Auto_Status()
    {
      if (manual == 0)
      {
        AutoMode_Function();
      }

    }




    void AutoMode_Function()
    {
      sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
      Serial.print("Current Time: ");
      Serial.println(currentTime);

      if (hour() >= SThour &&  hour() <= SPhour)
      {
        if (minute() >= STmin && minute() <= SPmin)
        {
          TimeZone1 = 1;
          Blynk.virtualWrite(V51, 1);
          Blynk.virtualWrite(V52, 0);
          Blynk.virtualWrite(V53, 0);
          Blynk.virtualWrite(V54, 0);
        }

      }

      if (hour() >= Wk_Emd_SThour && hour() <= Wk_Emd_SPhour)
      {
        if (minute() >= Wk_Emd_STmin && minute() <= Wk_Emd_SPmin)
        {
          TimeZone2 = 1;
          Blynk.virtualWrite(V51, 0);
          Blynk.virtualWrite(V52, 1);
          Blynk.virtualWrite(V53, 0);
          Blynk.virtualWrite(V54, 0);
        }
      }
      if (hour() >= Al_SThour && hour() <= Al_SPhour)
      {
        if (minute() >= Al_STmin && minute() <= Al_SPmin)
        {
          TimeZone3 = 1;
          Blynk.virtualWrite(V51, 0);
          Blynk.virtualWrite(V52, 0);
          Blynk.virtualWrite(V53, 1);
          Blynk.virtualWrite(V54, 0);
        }
      }
      if (hour() >= TimeZ4__SThour && hour() <= TimeZ4__SPhour)
      {
        if (minute() >= TimeZ4__STmin && minute() <= TimeZ4__SPmin)
        {
          TimeZone4 = 1;
          Blynk.virtualWrite(V51, 0);
          Blynk.virtualWrite(V52, 0);
          Blynk.virtualWrite(V53, 0);
          Blynk.virtualWrite(V54, 1);
        }
      } else if (TimeZone1 == 0 && TimeZone2 == 0 && TimeZone3 == 0 && TimeZone4 == 0)
      {
        Blynk.virtualWrite(V51, 0);
        Blynk.virtualWrite(V52, 0);
        Blynk.virtualWrite(V53, 0);
        Blynk.virtualWrite(V54, 0);
      }


    }

Please repost your code properly…
Thank you.


https://community.blynk.cc/uploads/default/original/2X/3/364b0639df751f9346f9ea7559d0c03b903c3298.PNG