Timer widget work incorrectly after midnight

Hi,

I need help and comment on the code. My timer will activate few Hours earlier than the setting if the time set to start after midnight. Example: start time on blynk Timer widget was set at 00:00:00hr until 06:00:00hrs. But it will start at around 18++hrs. Also If I start it at 1830hr and stop next day at 0630hrs, it will not OFF at the time set.

Please advise where I do it wrongly on the code below. Thanks.

#include <Blynk.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>

#define BLYNK_TEMPLATE_ID "-----------"
#define BLYNK_DEVICE_NAME "----------"
#define BLYNK_AUTH_TOKEN "-----------"
char auth[] = BLYNK_AUTH_TOKEN;

#define BLYNK_PRINT Serial
EthernetClient client;
BlynkTimer timer;
bool led_set[60];
long timer_start_set[60] = {0xFFFF, 0xFFFF};
long timer_stop_set[60] = {0xFFFF, 0xFFFF};
unsigned char weekday_set[60];
long rtc_sec;
unsigned char day_of_week;
bool led_status[60];
bool update_blynk_status[60];
bool update_switch_status[60];
bool led_timer_on_set[60];
int val0, val1, val2;
//--------------------------------------- Data from Blynk app/Dashboard ------------------------------------------------
// RLY 1
BLYNK_WRITE(V22)
{
  val0 = param.asInt();
  if ( led_timer_on_set[0] == 0 )
    {
    led_set[0] = val0;    
    }
  else
    {
    update_blynk_status[0] = 1;
    led_set[0] = val0; //just changed from led_status[0] = 0;- latest
    }
}

// RLY 2
BLYNK_WRITE(V23)
{
  val1 = param.asInt();
  if ( led_timer_on_set[1] == 0 )
    {
    led_set[1] = val1;    
    }
  else
    {
    update_blynk_status[1] = 1;
    led_set[1] = val1; 
    } 
}
// -----------------------------------  Data from Timer Widget --------------------------------------------------------------------
// Timer 1
BLYNK_WRITE(V0) 
{
  unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[0] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[0] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[0] = week_day;
  }
  else
  {
    timer_start_set[0] = 0xFFFF;
    timer_stop_set[0] = 0xFFFF;
  }
}

// Timer 2
BLYNK_WRITE(V1) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[1] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[1] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[1] = week_day;
  }
  else
  {
    timer_start_set[1] = 0xFFFF;
    timer_stop_set[1] = 0xFFFF;
  }
}
BLYNK_WRITE(InternalPinRTC) 
{
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  unsigned long blynkTime = param.asLong(); 
  
  if (blynkTime >= DEFAULT_TIME) 
  {
    setTime(blynkTime);

    day_of_week = weekday();
  
    if ( day_of_week == 1 )
      day_of_week = 7;
    else
      day_of_week -= 1; 
    
    rtc_sec = (hour()*60*60) + (minute()*60) + second();
   
    Serial.println(blynkTime);
    Serial.println(String("RTC Server: ") + hour() + ":" + minute() + ":" + second());
    Serial.println(String("Day of Week: ") + weekday()); 
  }
}

// ------------------------ GET BLYNK STATUS - SYNC  ---------------------------------------------
BLYNK_CONNECTED() 
{
  Blynk.sendInternal("rtc", "sync"); 
}
void checkTime() 
{
  Blynk.sendInternal("rtc", "sync"); 
}

//========================== OUTPUT MANAGER =========================================
void led_mng()
{
  bool time_set_overflow;
  bool led_status_buf[60];
  int i;
  
  for (i=0; i<60; i++)
  {
    led_status_buf[i] = led_status[i];
    time_set_overflow = 0;
    
    if ( timer_start_set[i] != 0xFFFF && timer_stop_set[i] != 0xFFFF)
    {
      if ( timer_stop_set[i] < timer_start_set[i] ) time_set_overflow = 1;

      if ((((time_set_overflow == 0 && (rtc_sec >= timer_start_set[i]) && (rtc_sec < timer_stop_set[i])) ||
        (time_set_overflow  && ((rtc_sec >= timer_start_set[i]) || (rtc_sec < timer_stop_set[i])))) && 
        (weekday_set[i] == 0x00 || (weekday_set[i] & (0x01 << (day_of_week - 1) )))) )
        {
          led_timer_on_set[i] = 1;
        }
        else
          led_timer_on_set[i] = 0;
    }
    else
      led_timer_on_set[i] = 0;

      if ( led_timer_on_set[i] )
      {
        led_status[i] = 0;//invert - ori 1
        led_set[i] = 1; // ori 0
      }
      else
      {
        led_status[i] = led_set[i];
      }

    if ( led_status_buf[i] != led_status[i] )
      update_blynk_status[i] = 0;  // ori value 1
  }
    // HARDWARE CONTROL
  digitalWrite(22, led_status[0]);     
  digitalWrite(23, led_status[1]);
}
void blynk_update()
{
  if ( update_blynk_status[0] )
  {
      update_blynk_status[0] = 0;
      Blynk.virtualWrite(V22, led_status[0]);
  }  

  if ( update_blynk_status[1] )
  {
      update_blynk_status[1] = 0;
      Blynk.virtualWrite(V23, led_status[1]);
  } 
}
void setup()
{
  Serial.begin(9600);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  digitalWrite(22, HIGH);     
  digitalWrite(23, HIGH);
}

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

I actually wants to make the light switch from RL1 to RLY2 after midnight. Example:

RLY1 ON from 18:30hrs to 23:59:59hrs then RLY2 start ON right after the RLY1 OFF which is at 00:00:01hr untul 06:30:00hrs. RLY1 work fine It OFF at the time set. But The RLY2 Start too early just about a few minutes after the RLY1 started ON. I don’t know why.

I’d suggest that you edit your post and include your full sketch, redacting any sensitive information, and provide as much information as possible about how you’ve configured your app dashboard, what each widget is for, what datastream its attached to, what timezone offset you’ve entered into your Time Input widget(s), and what timezone your device is in.

Pete.

Thanks,

I have updated the code. On the App Time Widget I did not Enable Time zone Selection. Even If I enable and select my Local Time zone. It will still inaccurate. I tried recently, if I start the timer at 00:00:01 and stop at 06:30:00 it will start immediately. But If I set the Stop time to 03:20:00 or earlier it will not start immediately.
The timer is using Virtual Pin datastreams V0 and V1 assigned as STRING, And Blynk Switch Button V22 and V23 an Integer.

The function is when timer is inactive I can ON/OFF use the switch button on app/dashboard.

Your sketch appears to be missing the firmware configuration data necessary for it to compile and function correctly.

I’m unable to follow your description of how your app is configured and what the functionality of the varnish widgets is.

If your references to “timer” are actually referring to Time Input widgets then using the correct terminology would also be useful.

Pete.

You may check the timer process in “void led_mng()” Function. The BLYNK_WRITE (V0)/(V1) is to capture the time input from Time Input Widget, I think. Anywhere I got the code from someone’s project and try to apply it to my project. I actually have no idea how to use the New Blynk Time Input Widget. I need help.

Try this example

Thanks.

I also want to use both Time input widget and Switch button widget to control same Output at Pin 22 and 23 which is assigned in Datastreams as VPin22 and 23. But in that sample, it only shows code for Time input Widget only.

My full project will be including input from Serial pin TX RX from Keypad on Arduino UNO. They will be 3 way of controlling the each outputs.

I don’t think anyone is going to spend time trying to figure-out someone else’s incomplete code.
There is no way that the code you have posted is performing in the way that you’ve described in post #1, because it’s impossible for your code to connect to the Blynk cloud servers.

So, any time spent looking at this code, other than the code you are actually running is going to be wasted time.

In addition, for someone to make sense of this code they need to know what type of widgets are attached to each of the virtual datastreams, how those widgets are configured, and most importantly what the purpose of those widgets are.
Despite me asking you for this information several times, you seem unwilling or unable to provide it.

If you want help from the community members you need to present the information in a format that doesn’t turn people off as soon as they look at it, otherwise they will simply skip over your topic and spend their time helping other people who have taken the time to provide sufficient information to enable the code, and the problem, to be understood.

If the code was written by someone else and it’s not functioning as expected, and you don’t have the skills to fix that issue, then maybe your best bet is to contact the code-s author and ask for assistance?

Pete.

I cannot post my whole code as it exceeded the character limit. Let me try to break it into 2 parts.

You can try automation, which requires no coding.

Wrong character. Triple backticks look like this ```

1st Part

#include <Blynk.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include "DHT.h" 

#define BLYNK_TEMPLATE_ID "My ID"
#define BLYNK_DEVICE_NAME "My Proj"
#define BLYNK_AUTH_TOKEN "My Token"
#define OnlineLED 12
#define OfflineLED 13
#define W5100_CS  10
#define SDCARD_CS 4
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#define DHTPIN 9
#define DHTTYPE DHT11 
#define TRELAY 8 

//int status = LAN_IDLE_STATUS;
char Web[] = "blynk.cloud";
char auth[] = BLYNK_AUTH_TOKEN;
EthernetClient client;
BlynkTimer timer;
bool led_set[60];
long timer_start_set[60] = {0xFFFF, 0xFFFF};
long timer_stop_set[60] = {0xFFFF, 0xFFFF};
unsigned char weekday_set[60];
long rtc_sec;
unsigned char day_of_week;
bool led_status[60];
bool update_blynk_status[60];
bool update_switch_status[60];
bool led_timer_on_set[60];
int val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, val10, val11, val12, val13, val14, val15, val16, val17,val18, val19, val20 =0;
int val21, val22, val23, val24, val25, val26, val27,val28, val29, val30 =0;
DHT dht(DHTPIN, DHTTYPE);

int S = 0;
String Q;

//--------------------------------------- Data from Blynk app/Dashboard ------------------------------------------------
// RLY 1
BLYNK_WRITE(V22)
{
  val0 = param.asInt();
  if ( led_timer_on_set[0] == 0 )
    {
    led_set[0] = val0;    
    }
  else
    {
    update_blynk_status[0] = 1;
    led_set[0] = val0; //just changed from led_status[0] = 0;- latest
    }
}

// RLY 2
BLYNK_WRITE(V23)
{
  val1 = param.asInt();
  if ( led_timer_on_set[1] == 0 )
    {
    led_set[1] = val1;    
    }
  else
    {
    update_blynk_status[1] = 1;
    led_set[1] = val1; 
    } 
}

// RLY 3
BLYNK_WRITE(V24)
{
  val2 = param.asInt();
  if ( led_timer_on_set[2] == 0 )
    {
    led_set[2] = val2;    
    }
  else
    {
    update_blynk_status[2] = 1;
    led_set[2] = val2; 
    }  
}

// RLY 4
BLYNK_WRITE(V25)
{
  val3 = param.asInt();
  if ( led_timer_on_set[3] == 0 )
    {
    led_set[3] = val3;    
    }
  else
    {
    update_blynk_status[3] = 1;
    led_set[3] = val3; 
    }   
}

// RLY 5
BLYNK_WRITE(V26)
{
  val4 = param.asInt();
  if ( led_timer_on_set[4] == 0 )
    {
    led_set[4] = val4;    
    }
  else
    {
    update_blynk_status[4] = 1;
    led_set[4] = val4; 
    }     
}

//RLY 6
BLYNK_WRITE(V27)
{
  val5 = param.asInt();
  if ( led_timer_on_set[5] == 0 )
    {
    led_set[5] = val5;    
    }
  else
    {
    update_blynk_status[5] = 1;
    led_set[5] = val5; 
    }     
}

//RLY 7
BLYNK_WRITE(V28)
{
  val6 = param.asInt();
  if ( led_timer_on_set[6] == 0 )
    {
    led_set[6] = val6;    
    }
  else
    {
    update_blynk_status[6] = 1;
    led_set[6] = val6; 
    }     
}

//RLY 8
BLYNK_WRITE(V29)
{
  val7 = param.asInt();
  if ( led_timer_on_set[7] == 0 )
    {
    led_set[7] = val7;    
    }
  else
    {
    update_blynk_status[7] = 1;
    led_set[7] = val7; 
    }     
}

//RLY 9
BLYNK_WRITE(V30)
{
  val8 = param.asInt();
  if ( led_timer_on_set[8] == 0 )
    {
    led_set[8] = val8;    
    }
  else
    {
    update_blynk_status[8] = 1;
    led_set[8] = val8; 
    }     
}

//RLY 10
BLYNK_WRITE(V31)
{
  val9 = param.asInt();
  if ( led_timer_on_set[9] == 0 )
    {
    led_set[9] = val9;    
    }
  else
    {
    update_blynk_status[9] = 1;
    led_set[9] = val9; 
    }     
}

//RLY 11
BLYNK_WRITE(V32)
{
  val10 = param.asInt();
  if ( led_timer_on_set[10] == 0 )
    {
    led_set[10] = val10;    
    }
  else
    {
    update_blynk_status[10] = 1;
    led_set[10] = val10; 
    }     
}

//RLY 12
BLYNK_WRITE(V33)
{
  val11 = param.asInt();
  if ( led_timer_on_set[11] == 0 )
    {
    led_set[11] = val11;    
    }
  else
    {
    update_blynk_status[11] = 1;
    led_set[11] = val11; 
    }     
}

//RLY 13
BLYNK_WRITE(V34)
{
  val12 = param.asInt();
  if ( led_timer_on_set[12] == 0 )
    {
    led_set[12] = val12;    
    }
  else
    {
    update_blynk_status[12] = 1;
    led_set[12] = val12; 
    }     
}

//RLY 14
BLYNK_WRITE(V35)
{
  val13 = param.asInt();
  if ( led_timer_on_set[13] == 0 )
    {
    led_set[13] = val13;    
    }
  else
    {
    update_blynk_status[13] = 1;
    led_set[13] = val13; 
    }     
}

//RLY 15
BLYNK_WRITE(V36)
{
  val14 = param.asInt();
  
  if (led_timer_on_set[14] == 0 )
    {
    led_set[14] = val14;    
    }
  else
    {
    update_blynk_status[14] = 1;
    led_set[14] = val14; 
    }   
}
//RLY 16
BLYNK_WRITE(V37)
{
  val15 = param.asInt();
  
  if (led_timer_on_set[15] == 0 )
    {
    led_set[15] = val15;    
    }
  else
    {
    update_blynk_status[15] = 1;
    led_set[15] = val15; 
    }   
}

//RLY 17
BLYNK_WRITE(V38)
{
  val16 = param.asInt();
  
  if (led_timer_on_set[16] == 0 )
    {
    led_set[16] = val16;    
    }
  else
    {
    update_blynk_status[16] = 1;
    led_set[16] = val16; 
    }   
}
//RLY 18
BLYNK_WRITE(V39)
{
  val17 = param.asInt();
  
  if (led_timer_on_set[17] == 0 )
    {
    led_set[17] = val17;    
    }
  else
    {
    update_blynk_status[17] = 1;
    led_set[17] = val17; 
    }   
}
//RLY 19
BLYNK_WRITE(V40)
{
  val18 = param.asInt();
  
  if (led_timer_on_set[18] == 0 )
    {
    led_set[18] = val18;    
    }
  else
    {
    update_blynk_status[18] = 1;
    led_set[18] = val18; 
    }   
}
//RLY 20
BLYNK_WRITE(V41)
{
  val19 = param.asInt();
  
  if (led_timer_on_set[19] == 0 )
    {
    led_set[19] = val19;    
    }
  else
    {
    update_blynk_status[19] = 1;
    led_set[19] = val19; 
    }   
}
//RLY 21
BLYNK_WRITE(V42)
{
  val20 = param.asInt();
  
  if (led_timer_on_set[20] == 0 )
    {
    led_set[20] = val20;    
    }
  else
    {
    update_blynk_status[20] = 1;
    led_set[20] = val20; 
    }   
}
//RLY 22 - Power Socket 1
BLYNK_WRITE(V43)
{
  val21 = param.asInt();
  
  if (led_timer_on_set[21] == 0 )
    {
    led_set[21] = val21;    
    }
  else
    {
    update_blynk_status[21] = 1;
    led_set[21] = val21; 
    }   
}
//RLY 23 - Power Socket 2
BLYNK_WRITE(V44)
{
  val22 = param.asInt();
  
  if (led_timer_on_set[22] == 0 )
    {
    led_set[22] = val22;    
    }
  else
    {
    update_blynk_status[22] = 1;
    led_set[22] = val22; 
    }   
}
//RLY 24 - Power Socket 3
BLYNK_WRITE(V45)
{
  val23 = param.asInt();
  
  if (led_timer_on_set[23] == 0 )
    {
    led_set[23] = val23;    
    }
  else
    {
    update_blynk_status[23] = 1;
    led_set[23] = val23; 
    }   
}
//RLY 25 - Power Socket 4
BLYNK_WRITE(V46)
{
  val24 = param.asInt();
  
  if (led_timer_on_set[24] == 0 )
    {
    led_set[24] = val24;    
    }
  else
    {
    update_blynk_status[24] = 1;
    led_set[24] = val24; 
    }   
}
//RLY 26 - Power Socket 5
BLYNK_WRITE(V47)
{
  val25 = param.asInt();
  
  if (led_timer_on_set[25] == 0 )
    {
    led_set[25] = val25;    
    }
  else
    {
    update_blynk_status[25] = 1;
    led_set[25] = val25; 
    }   
}
//RLY 27 - Power Socket 6
BLYNK_WRITE(V48)
{
  val26 = param.asInt();
  
  if (led_timer_on_set[26] == 0 )
    {
    led_set[26] = val26;    
    }
  else
    {
    update_blynk_status[26] = 1;
    led_set[26] = val26; 
    }   
}
//RLY 28 - Power Socket 7
BLYNK_WRITE(V49)
{
  val27 = param.asInt();
  
  if (led_timer_on_set[27] == 0 )
    {
    led_set[27] = val27;    
    }
  else
    {
    update_blynk_status[27] = 1;
    led_set[27] = val27; 
    }   
}
//RLY 29 - Power Socket 8
BLYNK_WRITE(V50)
{
  val28 = param.asInt();
  
  if (led_timer_on_set[28] == 0 )
    {
    led_set[28] = val28;    
    }
  else
    {
    update_blynk_status[28] = 1;
    led_set[28] = val28; 
    }   
}
//RLY 30 - Power Socket 9
BLYNK_WRITE(V51)
{
  val29 = param.asInt();
  
  if (led_timer_on_set[29] == 0 )
    {
    led_set[29] = val29;    
    }
  else
    {
    update_blynk_status[29] = 1;
    led_set[29] = val29; 
    }   
}
//RLY 31 - Power Socket 9
BLYNK_WRITE(V52)
{
  val30 = param.asInt();
  
  if (led_timer_on_set[30] == 0 )
    {
    led_set[30] = val30;    
    }
  else
    {
    update_blynk_status[30] = 1;
    led_set[30] = val30; 
    }   
}

// -----------------------------------  Data from Timer --------------------------------------------------------------------
// Timer 1
BLYNK_WRITE(V0) 
{
  unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[0] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[0] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[0] = week_day;
  }
  else
  {
    timer_start_set[0] = 0xFFFF;
    timer_stop_set[0] = 0xFFFF;
  }
}

// Timer 2
BLYNK_WRITE(V1) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[1] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[1] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[1] = week_day;
  }
  else
  {
    timer_start_set[1] = 0xFFFF;
    timer_stop_set[1] = 0xFFFF;
  }
}

// Timer 3
BLYNK_WRITE(V2) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[2] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[2] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[2] = week_day;
  }
  else
  {
    timer_start_set[2] = 0xFFFF;
    timer_stop_set[2] = 0xFFFF;
  }
}

// Timer 4
BLYNK_WRITE(V3) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[3] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[3] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[3] = week_day;
  }
  else
  {
    timer_start_set[3] = 0xFFFF;
    timer_stop_set[3] = 0xFFFF;
  }
}

// Timer 5
BLYNK_WRITE(V4) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[4] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[4] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[4] = week_day;
  }
  else
  {
    timer_start_set[4] = 0xFFFF;
    timer_stop_set[4] = 0xFFFF;
  }
}

// Timer 6
BLYNK_WRITE(V5) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[5] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[5] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[5] = week_day;
  }
  else
  {
    timer_start_set[5] = 0xFFFF;
    timer_stop_set[5] = 0xFFFF;
  }
}

// Timer 7
BLYNK_WRITE(V6) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[6] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[6] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[6] = week_day;
  }
  else
  {
    timer_start_set[6] = 0xFFFF;
    timer_stop_set[6] = 0xFFFF;
  }
}

// Timer 8
BLYNK_WRITE(V7) 
{
unsigned char week_day;
 
  TimeInputParam t(param);

  if (t.hasStartTime() && t.hasStopTime() ) 
  {
    timer_start_set[7] = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
    timer_stop_set[7] = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
    
    Serial.println(String("Start Time: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
                   
    Serial.println(String("Stop Time: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
                   
    for (int i = 1; i <= 7; i++) 
    {
      if (t.isWeekdaySelected(i)) 
      {
        week_day |= (0x01 << (i-1));
        Serial.println(String("Day ") + i + " is selected");
      }
      else
      {
        week_day &= (~(0x01 << (i-1)));
      }
    } 

    weekday_set[7] = week_day;
  }
  else
  {
    timer_start_set[7] = 0xFFFF;
    timer_stop_set[7] = 0xFFFF;
  }
}

//========================= Clock ===================================================================

BLYNK_WRITE(InternalPinRTC) 
{
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  unsigned long blynkTime = param.asLong(); 
  
  if (blynkTime >= DEFAULT_TIME) 
  {
    setTime(blynkTime);

    day_of_week = weekday();
  
    if ( day_of_week == 1 )
      day_of_week = 7;
    else
      day_of_week -= 1; 
    
    rtc_sec = (hour()*60*60) + (minute()*60) + second();
   
    Serial.println(blynkTime);
    Serial.println(String("RTC Server: ") + hour() + ":" + minute() + ":" + second());
    Serial.println(String("Day of Week: ") + weekday()); 
  }
}

// ------------------------ GET BLYNK STATUS - SYNC  ---------------------------------------------
BLYNK_CONNECTED() 
{
  Blynk.sendInternal("rtc", "sync"); 
  Blynk.syncVirtual(V22);  // will cause BLYNK_WRITE(V22) to be executed
  Blynk.syncVirtual(V23);
  Blynk.syncVirtual(V24);
  Blynk.syncVirtual(V25);
  Blynk.syncVirtual(V26);
  Blynk.syncVirtual(V27);
  Blynk.syncVirtual(V28);
  Blynk.syncVirtual(V29);
  Blynk.syncVirtual(V30);
  Blynk.syncVirtual(V31);
  Blynk.syncVirtual(V32);
  Blynk.syncVirtual(V33); Blynk.syncVirtual(V34);   Blynk.syncVirtual(V35); Blynk.syncVirtual(V36); Blynk.syncVirtual(V37);
  //Blynk.syncVirtual(V38);  Blynk.syncVirtual(V39);   Blynk.syncVirtual(V40); Blynk.syncVirtual(V41); Blynk.syncVirtual(V42);
 // Blynk.syncVirtual(V43);  Blynk.syncVirtual(V44);   Blynk.syncVirtual(V45); Blynk.syncVirtual(V46); Blynk.syncVirtual(V47);
 // Blynk.syncVirtual(V48);  Blynk.syncVirtual(V49);   Blynk.syncVirtual(V50); Blynk.syncVirtual(V51); Blynk.syncVirtual(V52);
 
}

//----------------------------------- Clock read -------------------------------------
void checkTime() 
{
  Blynk.sendInternal("rtc", "sync"); 
}

//========================== OUTPUT MANAGER =========================================
void led_mng()
{
  bool time_set_overflow;
  bool led_status_buf[60];
  int i;
  
  for (i=0; i<60; i++)
  {
    led_status_buf[i] = led_status[i];
    time_set_overflow = 0;
    
    if ( timer_start_set[i] != 0xFFFF && timer_stop_set[i] != 0xFFFF)
    {
      if ( timer_stop_set[i] < timer_start_set[i] ) time_set_overflow = 1;

      if ((((time_set_overflow == 0 && (rtc_sec >= timer_start_set[i]) && (rtc_sec < timer_stop_set[i])) ||
        (time_set_overflow  && ((rtc_sec >= timer_start_set[i]) || (rtc_sec < timer_stop_set[i])))) && 
        (weekday_set[i] == 0x00 || (weekday_set[i] & (0x01 << (day_of_week - 1) )))) )
        {
          led_timer_on_set[i] = 1;
        }
        else
          led_timer_on_set[i] = 0;
    }
    else
      led_timer_on_set[i] = 0;

      if ( led_timer_on_set[i] )
      {
        led_status[i] = 0;//invert - ori 1
        led_set[i] = 1; // ori 0
      }
      else
      {
        led_status[i] = led_set[i];
      }

    if ( led_status_buf[i] != led_status[i] )
      update_blynk_status[i] = 0;  // ori value 1
  }
    // HARDWARE CONTROL
  digitalWrite(22, led_status[0]);     
  digitalWrite(23, led_status[1]);
  digitalWrite(24, led_status[2]);
  digitalWrite(25, led_status[3]);
  digitalWrite(26, led_status[4]);
  digitalWrite(27, led_status[5]);     
  digitalWrite(28, led_status[6]);   
  digitalWrite(29, led_status[7]);
  digitalWrite(30, led_status[8]);
  digitalWrite(31, led_status[9]);
  digitalWrite(32, led_status[10]);
  digitalWrite(33, led_status[11]); 
  digitalWrite(34, led_status[12]); digitalWrite(35, led_status[13]); digitalWrite(36, led_status[14]); digitalWrite(37, led_status[15]);   
  digitalWrite(38, led_status[16]); digitalWrite(39, led_status[17]); digitalWrite(40, led_status[18]); digitalWrite(41, led_status[19]);
  digitalWrite(42, led_status[20]); digitalWrite(43, led_status[21]); digitalWrite(44, led_status[22]); digitalWrite(45, led_status[23]);
  digitalWrite(46, led_status[24]); digitalWrite(47, led_status[25]); digitalWrite(48, led_status[26]); digitalWrite(49, led_status[27]);
  digitalWrite(50, led_status[28]); digitalWrite(51, led_status[29]); digitalWrite(52, led_status[30]);
}

part 2

void blynk_update()
{
  if ( update_blynk_status[0] )
  {
      update_blynk_status[0] = 0;
      Blynk.virtualWrite(V22, led_status[0]);
  }  

  if ( update_blynk_status[1] )
  {
      update_blynk_status[1] = 0;
      Blynk.virtualWrite(V23, led_status[1]);
  } 

  if ( update_blynk_status[2] )
  {
      update_blynk_status[2] = 0;
      Blynk.virtualWrite(V24, led_status[2]);
  } 
  
   if ( update_blynk_status[3] )
  {
      update_blynk_status[3] = 0;
      Blynk.virtualWrite(V25, led_status[3]);
  } 
  if ( update_blynk_status[4] )
  {
      update_blynk_status[4] = 0;
      Blynk.virtualWrite(V26, led_status[4]);
  } 
  if ( update_blynk_status[5] )
  {
      update_blynk_status[5] = 0;
      Blynk.virtualWrite(V27, led_status[5]);
  } 
   if ( update_blynk_status[6] )
  {
      update_blynk_status[6] = 0;
      Blynk.virtualWrite(V28, led_status[6]);
  } 
  if ( update_blynk_status[7] )    
  {
      update_blynk_status[7] = 0;
      Blynk.virtualWrite(V29, led_status[7]);
  } 

   if ( update_blynk_status[8] )    
  {
      update_blynk_status[8] = 0;
      Blynk.virtualWrite(V30, led_status[8]);
  } 
   
   if ( update_blynk_status[9] )    
  {
      update_blynk_status[9] = 0;
      Blynk.virtualWrite(V31, led_status[9]);
  }  

    if ( update_blynk_status[10] )    
  {
      update_blynk_status[10] = 0;
      Blynk.virtualWrite(V32, led_status[10]);
  }

  if ( update_blynk_status[11] )    
  {
      update_blynk_status[11] = 0;
      Blynk.virtualWrite(V33, led_status[11]);
  }  

  if ( update_blynk_status[12] )
  {
      update_blynk_status[12] = 0;
      Blynk.virtualWrite(V34, led_status[12]);
  }   

  if ( update_blynk_status[13] )
  {
      update_blynk_status[13] = 0;
      Blynk.virtualWrite(V35, led_status[13]);
  }   

  if ( update_blynk_status[14] )
  {
      update_blynk_status[14] = 0;
      Blynk.virtualWrite(V36, led_status[14]);
  } 

   if ( update_blynk_status[15] )
  {
      update_blynk_status[15] = 0;
      Blynk.virtualWrite(V37, led_status[15]);
  } 

if ( update_blynk_status[16] )
  {
      update_blynk_status[16] = 0;
      Blynk.virtualWrite(V38, led_status[16]);
  } 

   if ( update_blynk_status[17] )
  {
      update_blynk_status[17] = 0;
      Blynk.virtualWrite(V39, led_status[17]);
  } 
  
   if ( update_blynk_status[18] )
  {
      update_blynk_status[18] = 0;
      Blynk.virtualWrite(V40, led_status[18]);
  } 

  if ( update_blynk_status[19] )
  {
      update_blynk_status[19] = 0;
      Blynk.virtualWrite(V41, led_status[19]);
  }   
  if ( update_blynk_status[20] )
  {
      update_blynk_status[20] = 0;
      Blynk.virtualWrite(V42, led_status[20]);
  }   

  if ( update_blynk_status[21] )
  {
      update_blynk_status[21] = 0;
      Blynk.virtualWrite(V43, led_status[21]);
  }   

  if ( update_blynk_status[22] )
  {
      update_blynk_status[22] = 0;
      Blynk.virtualWrite(V44, led_status[22]);
  }   

  if ( update_blynk_status[23] )
  {
      update_blynk_status[23] = 0;
      Blynk.virtualWrite(V45, led_status[23]);
  }   

  if ( update_blynk_status[24] )
  {
      update_blynk_status[24] = 0;
      Blynk.virtualWrite(V46, led_status[24]);
  } 

   if ( update_blynk_status[25] )
  {
      update_blynk_status[25] = 0;
      Blynk.virtualWrite(V47, led_status[25]);
  } 
 if ( update_blynk_status[26] )
  {
      update_blynk_status[26] = 0;
      Blynk.virtualWrite(V48, led_status[26]);
  } 

   if ( update_blynk_status[27] )
  {
      update_blynk_status[27] = 0;
      Blynk.virtualWrite(V49, led_status[27]);
  } 
  
   if ( update_blynk_status[28] )
  {
      update_blynk_status[28] = 0;
      Blynk.virtualWrite(V50, led_status[28]);
  } 

  if ( update_blynk_status[29] )
  {
      update_blynk_status[29] = 0;
      Blynk.virtualWrite(V51, led_status[29]);
  }   
  if ( update_blynk_status[30] )
  {
      update_blynk_status[30] = 0;
      Blynk.virtualWrite(V52, led_status[30]);
  }   

}

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  pinMode(24, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT); pinMode(35, OUTPUT);  pinMode(36, OUTPUT);pinMode(37, OUTPUT); pinMode(38, OUTPUT); pinMode(39, OUTPUT);
  pinMode(40, OUTPUT); pinMode(41, OUTPUT);  pinMode(42, OUTPUT);pinMode(43, OUTPUT); pinMode(44, OUTPUT); pinMode(45, OUTPUT);
  pinMode(46, OUTPUT); pinMode(47, OUTPUT);  pinMode(48, OUTPUT);pinMode(49, OUTPUT); pinMode(50, OUTPUT); pinMode(51, OUTPUT);
  pinMode(52, OUTPUT); 
  pinMode(OnlineLED, OUTPUT);
  pinMode(OfflineLED, OUTPUT);
  digitalWrite(13, HIGH);
  timer.setInterval(10000L, checkTime);  
  pinMode(TRELAY,OUTPUT);

  digitalWrite(TRELAY,HIGH);
  digitalWrite(22, HIGH);     
  digitalWrite(23, HIGH);
  digitalWrite(24, HIGH);
  digitalWrite(25, HIGH);
  digitalWrite(26, HIGH);
  digitalWrite(27, HIGH);   
  digitalWrite(28, HIGH); 
  digitalWrite(29, HIGH); 
  digitalWrite(30, HIGH);
  digitalWrite(31, HIGH);
  digitalWrite(32, HIGH);
  digitalWrite(33, HIGH);
  digitalWrite(34, HIGH); digitalWrite(35, HIGH);  digitalWrite(36, HIGH);digitalWrite(37, HIGH); digitalWrite(38, HIGH); digitalWrite(39, HIGH);
  digitalWrite(40, HIGH); digitalWrite(41, HIGH);  digitalWrite(42, HIGH);digitalWrite(43, HIGH); digitalWrite(44, HIGH); digitalWrite(45, HIGH);
  digitalWrite(46, HIGH); digitalWrite(47, HIGH);  digitalWrite(48, HIGH);digitalWrite(49, HIGH); digitalWrite(50, HIGH); digitalWrite(51, HIGH); digitalWrite(52, HIGH);
  //Blynk.begin();
  //Blynk.begin(auth);  
  // You can also specify server:
  Blynk.begin(auth, "blynk.cloud", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);

}

void loop() 
{
  String readString;
  Q;

//-------------------------------Check Serial0 Port---------------------------------------
 while (Serial.available()) {
      delay(1);
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
    if (isControl(c)) {
      //'Serial.println("it's a control character");
 break;
    }
      readString += c; //makes the string readString    
    }
 }   
//-------------------------------Check Serial1 Port---------------------------------------
  while (Serial1.available()) 
 {
   delay(1);
    if (Serial1.available() >0) 
    {
      char c = Serial1.read();  //gets one byte from serial buffer
      if (isControl(c)) 
      {
        //'Serial.println("it's a control character");
        break;
      }
      readString += c; //makes the string readString    
    }
 } 
//-------------------------------Check Serial2 Port---------------------------------------
 while (Serial2.available()) 
 {
   delay(1);
    if (Serial2.available() >0) 
    {
      char c = Serial2.read();  //gets one byte from serial buffer
      if (isControl(c)) 
      {
        //'Serial.println("it's a control character");
        break;
      }
      readString += c; //makes the string readString    
    }
 } 
//-------------------------------Check Serial3 Port---------------------------------------
 while (Serial3.available()) 
 {
   delay(1);
    if (Serial3.available() >0) 
    {
      char c = Serial3.read();  //gets one byte from serial buffer
      if (isControl(c)) 
      {
        //'Serial.println("it's a control character");
        break;
      }
      readString += c; //makes the string readString    
    }
 } 

 Q = readString;
//------------------------------------- selecting Serial input ---------------------------------------------------

if(Q=="01")
  { //Serial.print("val0 = ");
    //Serial.println(val0);
    
      if (val0 == 0)
        {           
          update_blynk_status[0] = 1;
           led_set[0]= 1;
          val0=1; // new added - finaly works
          Serial1.write("L1 ON");
          Serial2.write("L1 ON");
          Serial3.write("L1 ON");
        }
       else
        {              
          update_blynk_status[0] = 0;
          led_set[0]= 0; 
          val0= 0;   
          Serial1.write("L1 OFF");
          Serial2.write("L1 OFF"); 
          Serial3.write("L1 OFF");         
        }
  }

if(Q=="02")
  { //Serial.print("val1 = ");
    //Serial.println(val1);
    
      if (val1 == 0)
        {           
          update_blynk_status[1] = 1;
          led_set[1]= 1; 
          val1=1; 
          Serial1.write("L2 ON");
          Serial2.write("L2 ON");
          Serial3.write("L2 ON");          
        }
       else
        {              
          update_blynk_status[1] = 0;
          led_set[1]= 0;     
          val1= 0;    
          Serial1.write("L2 OFF");
          Serial2.write("L2 OFF");   
          Serial3.write("L2 OFF");        
        }
  }

if(Q=="03")
  { //Serial.print("val2 = ");
    //Serial.println(val2);
    
      if (val2 == 0)
        {           
          update_blynk_status[2] = 1;
          led_set[2]= 1; 
          val2=1; 
          Serial1.write("L3 ON");
          Serial2.write("L3 ON");
          Serial3.write("L3 ON");
        }
       else
        {              
          update_blynk_status[2] = 0;
          led_set[2]= 0;     
          val2= 0;    
          Serial1.write("L3 OFF");
          Serial2.write("L3 OFF"); 
          Serial3.write("L3 OFF"); 
        }
  }
  
  if(Q=="04")
  { //Serial.print("val3 = ");
    //Serial.println(val3);
    
      if (val3 == 0)
        {           
          update_blynk_status[3] = 1;
          led_set[3]= 1; 
          val3=1; 
          Serial1.write("L4 ON");
          Serial2.write("L4 ON"); 
          Serial3.write("L4 ON");
        }
       else
        {              
          update_blynk_status[3] = 0;
          led_set[3]= 0;     
          val3= 0;  
          Serial1.write("L4 OFF");
          Serial2.write("L4 OFF"); 
          Serial3.write("L4 OFF");   
        }
  }
  if(Q=="05")
  { //Serial.print("val4 = ");
    //Serial.println(val4);
    
      if (val4 == 0)
        {           
          update_blynk_status[4] = 1;
          led_set[4]= 1; 
          val4=1; 
          Serial1.write("L5 ON");
          Serial2.write("L5 ON"); 
          Serial3.write("L5 ON"); 
        }
       else
        {              
          update_blynk_status[4] = 0;
          led_set[4]= 0;     
          val4= 0;     
          Serial1.write("L5 OFF");
          Serial2.write("L5 OFF"); 
          Serial3.write("L5 OFF");
        }
  }

  if(Q=="06")
  { //Serial.print("val5 = ");
    //Serial.println(val5);
    
      if (val5 == 0)
        {           
          update_blynk_status[5] = 1;
          led_set[5]= 1; 
          val5=1; 
          Serial1.write("L6 ON");
          Serial2.write("L6 ON");  
          Serial3.write("L6 ON");        
        }
       else
        {              
          update_blynk_status[5] = 0;
          led_set[5]= 0;  
          val5= 0;  
          Serial1.write("L6 OFF");
          Serial2.write("L6 OFF"); 
          Serial3.write("L6 OFF");  
        }
  }        

  
  if(Q=="08")
  { //Serial.print("val6 = ");
    //Serial.println(val6);
    
      if (val7 == 0)
        {           
          update_blynk_status[7] = 1;
          led_set[7]= 1;
          val7=1; 
          Serial1.write("L8 ON");
          Serial2.write("L8 ON");  
          Serial3.write("L8 ON");        
        }
       else
        {              
          update_blynk_status[7] = 0;
          led_set[7]= 0;
          val7= 0;  
          Serial1.write("L8 OFF");
          Serial2.write("L8 OFF"); 
          Serial3.write("L8 OFF");  
        }
  }        

  if(Q=="09")
  { //Serial.print("val6 = ");
    //Serial.println(val6);
    
      if (val8 == 0)
        {           
          update_blynk_status[8] = 1;
          led_set[8]= 1;
          val8=1; 
          Serial1.write("L9 ON");
          Serial2.write("L9 ON");  
          Serial3.write("L9 ON");        
        }
       else
        {              
          update_blynk_status[8] = 0;
          led_set[8]= 0;
          val8= 0;  
          Serial1.write("L9 OFF");
          Serial2.write("L9 OFF"); 
          Serial3.write("L9 OFF");  
        }
  }        

  
  if(Q=="10")
  { //Serial.print("val6 = ");
    //Serial.println(val6);
    
      if (val9 == 0)
        {           
          update_blynk_status[9] = 1;
          led_set[9]= 1;
          val9=1; 
          Serial1.write("L10 ON");
          Serial2.write("L10 ON");  
          Serial3.write("L10 ON");        
        }
       else
        {              
          update_blynk_status[9] = 0;
          led_set[9]= 0;
          val9= 0;  
          Serial1.write("L10 OFF");
          Serial2.write("L10 OFF"); 
          Serial3.write("L10 OFF");  
        }
  }  

   if(Q=="11")
  { //Serial.print("val6 = ");
    //Serial.println(val6);
    
      if (val10 == 0)
        {           
          update_blynk_status[10] = 1;
          led_set[10]= 1;
          val10=1; 
          Serial1.write("L11 ON");
          Serial2.write("L11 ON");  
          Serial3.write("L11 ON");        
        }
       else
        {              
          update_blynk_status[10] = 0;
          led_set[10]= 0;
          val10= 0;  
          Serial1.write("L11 OFF");
          Serial2.write("L11 OFF"); 
          Serial3.write("L11 OFF");  
        }
  }        

  if(Q=="12")
  { //Serial.print("val6 = ");
    //Serial.println(val6);
    
      if (val11 == 0)
        {           
          update_blynk_status[11] = 1;
          led_set[11]= 1;
          val11=1; 
          Serial1.write("L12 ON");
          Serial2.write("L12 ON");  
          Serial3.write("L12 ON");        
        }
       else
        {              
          update_blynk_status[11] = 0;
          led_set[11]= 0;
          val11= 0;  
          Serial1.write("L12 OFF");
          Serial2.write("L12 OFF"); 
          Serial3.write("L12 OFF");  
        }
  }            

  if(Q=="13")
  { 
      if (val12 == 0)
        {           
          update_blynk_status[12] = 1;
          led_set[12]= 1; 
          val12=1; 
          Serial1.write("L13 ON");
          Serial2.write("L13 ON");
          Serial3.write("L13 ON");
        }
       else
        {              
          update_blynk_status[12] = 0;
          led_set[12]= 0;     
          val12= 0;    
          Serial1.write("L13 OFF");
          Serial2.write("L13 OFF"); 
          Serial3.write("L13 OFF"); 
        }
  }
  
  if(Q=="14")
  {     
      if (val13 == 0)
        {           
          update_blynk_status[13] = 1;
          led_set[13]= 1; 
          val13=1; 
          Serial1.write("L14 ON");
          Serial2.write("L14 ON"); 
          Serial3.write("L14 ON");
        }
       else
        {              
          update_blynk_status[13] = 0;
          led_set[13]= 0;     
          val13= 0;  
          Serial1.write("L14 OFF");
          Serial2.write("L14 OFF"); 
          Serial3.write("L14 OFF");   
        }
  }
  if(Q=="15")
  { 
      if (val14 == 0)
        {           
          update_blynk_status[14] = 1;
          led_set[14]= 1; 
          val14=1; 
          Serial1.write("L15 ON");
          Serial2.write("L15 ON"); 
          Serial3.write("L15 ON"); 
        }
       else
        {              
          update_blynk_status[14] = 0;
          led_set[14]= 0;     
          val14= 0;     
          Serial1.write("L15 OFF");
          Serial2.write("L15 OFF"); 
          Serial3.write("L15 OFF");
        }
  }

  if(Q=="16")
  { 
      if (val15 == 0)
        {           
          update_blynk_status[15] = 1;
          led_set[15]= 1; 
          val15=1; 
          Serial1.write("L16 ON");
          Serial2.write("L16 ON");  
          Serial3.write("L16 ON");        
        }
       else
        {              
          update_blynk_status[15] = 0;
          led_set[15]= 0;     
          val15= 0;  
          Serial1.write("L16 OFF");
          Serial2.write("L16 OFF"); 
          Serial3.write("L16 OFF");  
        }
  }        

  if(Q=="17")
  { 
      if (val16 == 0)
        {           
          update_blynk_status[16] = 1;
          led_set[16]= 1; 
          val16=1; 
          Serial1.write("L17 ON");
          Serial2.write("L17 ON");  
          Serial3.write("L17 ON");        
        }
       else
        {              
          update_blynk_status[16] = 0;
          led_set[16]= 0;     
          val16= 0;  
          Serial1.write("L17 OFF");
          Serial2.write("L17 OFF"); 
          Serial3.write("L17 OFF");  
        }
  }        

  if(Q=="18")
  {     
      if (val17 == 0)
        {           
          update_blynk_status[17] = 1;
          led_set[17]= 1; 
          val17=1; 
          Serial1.write("L18 ON");
          Serial2.write("L18 ON");  
          Serial3.write("L18 ON");        
        }
       else
        {              
          update_blynk_status[17] = 0;
          led_set[17]= 0;     
          val17= 0;  
          Serial1.write("L18 OFF");
          Serial2.write("L18 OFF"); 
          Serial3.write("L18 OFF");  
        }
  }        

 if(Q=="19")
  {     
      if (val18 == 0)
        {           
          update_blynk_status[18] = 1;
          led_set[18]= 1; 
          val18=1; 
          Serial1.write("L19 ON");
          Serial2.write("L19 ON");  
          Serial3.write("L19 ON");        
        }
       else
        {              
          update_blynk_status[18] = 0;
          led_set[18]= 0;     
          val18= 0;  
          Serial1.write("L19 OFF");
          Serial2.write("L19 OFF"); 
          Serial3.write("L19 OFF");  
        }
  }     

   if(Q=="00")
  {     
      if (val20 == 0)
        {           
          update_blynk_status[20] = 1;
          led_set[20]= 1; 
          val20=1; 
          Serial1.write("L21 ON");
          Serial2.write("L21 ON");  
          Serial3.write("L21 ON");        
        }
       else
        {              
          update_blynk_status[20] = 0;
          led_set[20]= 0;     
          val20= 0;  
          Serial1.write("L21 OFF");
          Serial2.write("L21 OFF"); 
          Serial3.write("L21 OFF");  
        }
  } 
  
  //--------------------------------------------- POWER SOCKET ----------------------------------------------

     if(Q=="21")
  {     
      if (val21 == 0)
        {           
          update_blynk_status[21] = 1;
          led_set[21]= 1; 
          val21=1; 
          Serial1.write("PP1 ON");
          Serial2.write("PP1 ON");  
          Serial3.write("PP1 ON");        
        }
       else
        {              
          update_blynk_status[21] = 0;
          led_set[21]= 0;     
          val21= 0;  
          Serial1.write("PP1 OFF");
          Serial2.write("PP1 OFF"); 
          Serial3.write("PP1 OFF");  
        }
  }   

  if(Q=="22")
  {     
      if (val22 == 0)
        {           
          update_blynk_status[22] = 1;
          led_set[22]= 1; 
          val22=1; 
          Serial1.write("PP2 ON");
          Serial2.write("PP2 ON");  
          Serial3.write("PP2 ON");        
        }
       else
        {              
          update_blynk_status[22] = 0;
          led_set[22]= 0;     
          val22= 0;  
          Serial1.write("PP2 OFF");
          Serial2.write("PP2 OFF"); 
          Serial3.write("PP2 OFF");  
        }
  }                       
  
  if(Q=="23")
  {     
      if (val23 == 0)
        {           
          update_blynk_status[23] = 1;
          led_set[23]= 1; 
          val23=1; 
          Serial1.write("PP3 ON");
          Serial2.write("PP3 ON");  
          Serial3.write("PP3 ON");        
        }
       else
        {              
          update_blynk_status[23] = 0;
          led_set[23]= 0;     
          val23= 0;  
          Serial1.write("PP3 OFF");
          Serial2.write("PP3 OFF"); 
          Serial3.write("PP3 OFF");  
        }
  }    

  if(Q=="24")
  {     
      if (val24 == 0)
        {           
          update_blynk_status[24] = 1;
          led_set[24]= 1; 
          val24=1; 
          Serial1.write("PP4 ON");
          Serial2.write("PP4 ON");  
          Serial3.write("PP4 ON");        
        }
       else
        {              
          update_blynk_status[24] = 0;
          led_set[24]= 0;     
          val24= 0;  
          Serial1.write("PP4 OFF");
          Serial2.write("PP4 OFF"); 
          Serial3.write("PP4 OFF");  
        }
  } 

  if(Q=="25")
  {     
      if (val25 == 0)
        {           
          update_blynk_status[25] = 1;
          led_set[25]= 1; 
          val25=1; 
          Serial1.write("PP5 ON");
          Serial2.write("PP5 ON");  
          Serial3.write("PP5 ON");        
        }
       else
        {              
          update_blynk_status[25] = 0;
          led_set[25]= 0;     
          val25= 0;  
          Serial1.write("PP5 OFF");
          Serial2.write("PP5 OFF"); 
          Serial3.write("PP5 OFF");  
        }
  }    

   if(Q=="26")
  {     
      if (val26 == 0)
        {           
          update_blynk_status[26] = 1;
          led_set[26]= 1; 
          val26=1; 
          Serial1.write("PP6 ON");
          Serial2.write("PP6 ON");  
          Serial3.write("PP6 ON");        
        }
       else
        {              
          update_blynk_status[26] = 0;
          led_set[26]= 0;     
          val26= 0;  
          Serial1.write("PP6 OFF");
          Serial2.write("PP6 OFF"); 
          Serial3.write("PP6 OFF");  
        }
  } 

  if(Q=="27")
  {     
      if (val27 == 0)
        {           
          update_blynk_status[27] = 1;
          led_set[27]= 1; 
          val27=1; 
          Serial1.write("PP7 ON");
          Serial2.write("PP7 ON");  
          Serial3.write("PP7 ON");        
        }
       else
        {              
          update_blynk_status[27] = 0;
          led_set[27]= 0;     
          val27= 0;  
          Serial1.write("PP7 OFF");
          Serial2.write("PP7 OFF"); 
          Serial3.write("PP7 OFF");  
        }
  }   

  if(Q=="28")
  {     
      if (val28 == 0)
        {           
          update_blynk_status[28] = 1;
          led_set[28]= 1; 
          val28=1; 
          Serial1.write("PP8 ON");
          Serial2.write("PP8 ON");  
          Serial3.write("PP8 ON");        
        }
       else
        {              
          update_blynk_status[28] = 0;
          led_set[28]= 0;     
          val28= 0;  
          Serial1.write("PP8 OFF");
          Serial2.write("PP8 OFF"); 
          Serial3.write("PP8 OFF");  
        }
  }                             

  if(Q=="29")
  {     
      if (val29 == 0)
        {           
          update_blynk_status[29] = 1;
          led_set[29]= 1; 
          val29=1; 
          Serial1.write("PP9 ON");
          Serial2.write("PP9 ON");  
          Serial3.write("PP9 ON");        
        }
       else
        {              
          update_blynk_status[29] = 0;
          led_set[29]= 0;     
          val29= 0;  
          Serial1.write("PP9 OFF");
          Serial2.write("PP9 OFF"); 
          Serial3.write("PP9 OFF");  
        }
  }                             

if(Q=="30")
  {     
      if (val30 == 0)
        {           
          update_blynk_status[30] = 1;
          led_set[30]= 1; 
          val30=1; 
          Serial1.write("PP10 ON");
          Serial2.write("PP10 ON");  
          Serial3.write("PP10 ON");        
        }
       else
        {              
          update_blynk_status[30] = 0;
          led_set[30]= 0;     
          val30= 0;  
          Serial1.write("PP10 OFF");
          Serial2.write("PP10 OFF"); 
          Serial3.write("PP10 OFF");  
        }
  }                        
    
  Blynk.run();
  timer.run();
  led_mng();
  blynk_update();
    

   if (client.connect(Web, 80)) 
  {
    //Serial.print("Blynk is ONLINE ");
    digitalWrite(OnlineLED, HIGH);
    digitalWrite(OfflineLED, LOW);
  }
  else 
  {
    //Serial.println("Blynk is OFFLINE");
    digitalWrite(OnlineLED, LOW);
    digitalWrite(OfflineLED, HIGH);
  }

  float t = dht.readTemperature();
  Blynk.virtualWrite(V100,t);
  
  if (isnan(dht.readTemperature())) 
{ 
    Serial.println(F("Error Reading Temperature")); 
}
  else 
{
    Serial.println(F("Temp: ")); 
    Serial.print(dht.readTemperature()); 
    Serial.println(F("C")); 
    
    if(dht.readTemperature() >= 17.5){ 
      digitalWrite(TRELAY,HIGH); 
      Serial.print(F("T-RELAY"));
      Serial.println(F("  ON"));
    }
    else{
      digitalWrite(TRELAY,LOW);
      Serial.print(F("T-RELAY"));
      Serial.println(F("  OFF"));
    }
  }

}

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

The serial input working fine. The Button Switch Widget also works well. The only problem is the Time Input Widget.

If you have this code running on a device then please take that device offline now.
You are calling a function which contains Blynk.virtualWrite() commands directly from your void loop. This will flood the server.

Pete.

1 Like

Please show me how I can modify it so it won’t flood the server. Do you mean the one for the DHT.

Read the link that @John93 provided in post#15

Pete.

The only Blynk.virtualWrite() in Void Loop () id The DHT output to Blynk. The Others is in the Function block.

Pete.