BLYNK
BLYNK.IO       πŸ“² GETTING STARTED       πŸ“— DOCS       πŸ‘‰ SKETCH BUILDER

Getting additional Seconds on My CODE with blynk 2

Need some help with my project Code.
we are Aiming for consistent timing with the Millis function when using Blynk, but as we use the Blynk we are getting additional second on timing.

we set the Timing for the relay to open for 1 min and close for 1 min as a trial but instead we get additional 10 seconds on the timing for both on and off.

please help badly wanted to graduate T_T

#include <Wire.h>
#include <dht.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

///////////////IOT
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "";
char ssid[] = ""; //wifiname
char pass[] = ""; //pass
#define EspSerial Serial2
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer; 
/////////////////

#define ONE_WIRE_BUS 2
#define DHT1_PIN 3
#define DHT2_PIN 4
#define DHT3_PIN 8
#define DHT4_PIN 9
#define ldr_pin2 6
#define Relay8 52 //Pump 
#define Relay7 50 //Peristaltic
#define Relay6 48 //pH up 
#define Relay5 46 //ph Down
#define Relay4 44 //peltier
#define Relay3 42 //Growlights

dht DHT1;
dht DHT2;
dht DHT3;
dht DHT4;

OneWire oneWire(ONE_WIRE_BUS);        
DallasTemperature sensors(&oneWire); 
LiquidCrystal_I2C lcd(0x27, 16, 2);  

 float Celcius=0;
 float Fahrenheit=0;
 float calibration_value = 21.34;
 int phval = 0;
 unsigned long int avgval;
 int buffer_arr[10], temp;
 float ph_act;

 int Relay8state= LOW;
 int Relay7State= LOW;
 int Relay6State= LOW; 
 int Relay5State= LOW;   
 unsigned long previouspump = 0; 
 unsigned long previoustemp = 0; 
 unsigned long previoushumi = 0;
 unsigned long previouspup = 0; 
 unsigned long previousdown = 0;
 long OnTime = 60000;      // 1min often outburst
 long OffTime = 60000;    // 1mins often outburst
 long OnTime1 = 90000;     // 1min normal
 long OffTime1 = 90000;   // 7mins normal
 long OT = 3000;           // 5 sec temp
 long FT = 3000;           // 5 sec temp
 long POT = 1000;          // 1 sec ph

 
void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
  timer.setInterval(1000L, sendSensors);


  sensors.begin();    
  lcd.init();         
  lcd.backlight();  
  
  pinMode(DHT1_PIN, INPUT);
  pinMode(DHT2_PIN, INPUT);
  pinMode(DHT3_PIN, INPUT);
  pinMode(DHT4_PIN, INPUT);
  pinMode(Relay8, OUTPUT);
  pinMode(Relay7, OUTPUT);
  pinMode(Relay6, OUTPUT);
  pinMode(Relay5, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay3, OUTPUT);

  pinMode(ldr_pin2,INPUT);  

  
  digitalWrite(Relay8, HIGH);
  digitalWrite(Relay7, HIGH);
  digitalWrite(Relay6, HIGH);
  digitalWrite(Relay5, HIGH);
  digitalWrite(Relay4, HIGH);
  digitalWrite(Relay3, HIGH);
}

void sendSensors(){
  Blynk.virtualWrite(V0, millis() / 1000);
 }
 
void loop()
{
  Blynk.run();
  timer.run();
   
///////////////////////////////////////////////////////////////////////////////////  
if(Fahrenheit >71){  //temparature codition code
  unsigned long currenttemp = millis();
  if((Relay7State == HIGH) && (currenttemp - previoustemp >= OT))
        {
        Relay7State = LOW;  // Turn it off
        previoustemp = currenttemp;  // Remember the time
        digitalWrite(Relay7, Relay7State);  // Update
        }
  else if ((Relay7State == LOW) && (currenttemp - previoustemp >= FT))
        {
        Relay7State = HIGH;  // turn it on
        previoustemp = currenttemp;   // Remember the time
        digitalWrite(Relay7, Relay7State);    // Update 
        }
   }//end temp


   
///////////////////////////////////////////////////////////////////////////////////     if humidity sensor is less than 60 more frequenct outburst
if ((DHT1.humidity<=60)&&(DHT2.humidity <= 60)&&(DHT3.humidity <= 60)&&(DHT4.humidity <= 60)){ //start humidity sensors
    unsigned long currenthumi = millis();
    if((Relay8state == LOW) && (currenthumi - previoushumi >= OnTime))
        {
        Relay8state = HIGH;  // Turn it off
        previoushumi = currenthumi;  // Remember the time
        digitalWrite(Relay8, Relay8state);  // Update 
        Blynk.virtualWrite(V5,"Normal Humidity");
        }
  
   else if ((Relay8state == HIGH) && (currenthumi - previoushumi >= OffTime))
        {
        Relay8state = LOW;  // turn it on
        previoushumi = currenthumi;   // Remember the time
        digitalWrite(Relay8, Relay8state);    // Update 
        Blynk.virtualWrite(V5,"Misters on due to low humidity");
        }
  }//end humidity
//////////////////////////////////////////////////////////////////////////////////////// if humidity sensor is greater than 60 Less frequenct outburst
if ((DHT1.humidity>=60)&&(DHT2.humidity >= 60)&&(DHT3.humidity >= 60)&&(DHT4.humidity >= 60)){ //start humidity sensors
    unsigned long currenthumi = millis();
    if((Relay8state == LOW) && (currenthumi - previoushumi >= OnTime1))
        {
        Relay8state = HIGH;  // Turn it off
        previoushumi = currenthumi;  // Remember the time
        digitalWrite(Relay8, Relay8state);  // Update 
        Blynk.virtualWrite(V5,"Normal Humidity");
        }
  
   else if ((Relay8state == HIGH) && (currenthumi - previoushumi >= OffTime1))
        {
        Relay8state = LOW;  // turn it on
        previoushumi = currenthumi;   // Remember the time
        digitalWrite(Relay8, Relay8state);    // Update 
        Blynk.virtualWrite(V5,"Misters on due to low humidity");
        }
  }//end humidity

////////////////////////////////////////////////////////////////////////////////////////
  TEMPSENSOR();
  Growlights();
  WATERLVLSENSOR();
  DHT();
  PH();
  PHUPDOWN();
  coolingsystem();

}//loop

void TEMPSENSOR(){  

  sensors.requestTemperatures();  
  Celcius=sensors.getTempCByIndex(0);           
  Fahrenheit=sensors.toFahrenheit(Celcius); 
  Blynk.virtualWrite(V7,Fahrenheit);
  lcd.setCursor(1, 0);  
  lcd.print("Reservoir Temp");
  lcd.setCursor(5, 1);       
  lcd.print( Fahrenheit);
  lcd.print((char)223);      
  lcd.print("F");
  delay(1000);
  lcd.clear(); 
}
/////////////
void coolingsystem(){
  if(Fahrenheit >71){
    digitalWrite(Relay4, LOW);
    Blynk.virtualWrite(V8,"High Temperature - Cooling system on");
  }
  else {
    digitalWrite(Relay4, HIGH);
    digitalWrite(Relay7, HIGH);
    Blynk.virtualWrite(V8,"Normal Temperature");
  }

}
///////
void PHUPDOWN(){
  if (ph_act <= 5.9){//start pH Up Peristaltic
        
        digitalWrite(Relay6, Relay6State);  // Update 
        Blynk.virtualWrite(V10,"pH lvl low-pH - up ON");
        delay(POT);
        digitalWrite(Relay6, HIGH);
        delay(POT);
}

if (ph_act >= 7.1){//start pH Down Peristaltic
        digitalWrite(Relay5, Relay5State);  // Update 
        Blynk.virtualWrite(V10,"pH lvl High_pH - down ON");
        delay(POT);
        digitalWrite(Relay5, HIGH);
        delay(POT);
        }
}
////////////////////////////////////////////
   /////////////////////////////////////
   void PH(){

   for (int i = 0; i < 10; i++)
  {
    buffer_arr[i] = analogRead(A1);
    delay(30);
  }
  for (int i = 0; i < 9; i++)
  {
    for (int j = i + 1; j < 10; j++)
    {
      if (buffer_arr[i] > buffer_arr[j])
      {
        temp = buffer_arr[i];
        buffer_arr[i] = buffer_arr[j];
        buffer_arr[j] = temp;
      }
    }
  }
  avgval = 0;
  for (int i = 2; i < 8; i++)
    avgval += buffer_arr[i];
  float volt = (float)avgval * 5.0 / 1024 / 6;
  float ph_act = -5.70 * volt + calibration_value;
  lcd.setCursor(0, 0);
  lcd.print("pH Val:");
  lcd.setCursor(8, 0);
  lcd.print(ph_act);
  Blynk.virtualWrite(V9,ph_act);
  delay(1000);
  lcd.clear();

   
}

   /////////////
   
void Growlights(){
  if( digitalRead( ldr_pin2 ) == 1){
      digitalWrite( Relay3, LOW);
      Blynk.virtualWrite(V11,"Growlights ON");
   }
   else{
      digitalWrite( Relay3 , HIGH);
      Blynk.virtualWrite(V11,"Growlights OFF");
   }
}


///////////////////
void DHT(){
  
  int DHT1CHECK = DHT1.read11(DHT1_PIN);
  int DHT2CHECK = DHT2.read11(DHT2_PIN);
  int DHT3CHECK = DHT3.read11(DHT3_PIN);
  int DHT4CHECK = DHT4.read11(DHT4_PIN);
  
    if ((DHT1CHECK ==0)&&(DHT2CHECK ==0)&&(DHT3CHECK ==0)&&(DHT4CHECK ==0)) {
    }
      do {  
        ////////Vertical
          lcd.setCursor(0,0);
          lcd.print("1.Humidity:");
          lcd.print(DHT1.humidity,1);
          lcd.print("%");  
          Blynk.virtualWrite(V1,DHT1.humidity);       
          lcd.setCursor(0,1); 
          lcd.print("2.Humidity:");
          lcd.print(DHT2.humidity,1);
          lcd.print("%");        
          Blynk.virtualWrite(V2,DHT2.humidity); 
          delay(1000);
          lcd.clear();
          
        ////////Horizontal
          lcd.setCursor(0,0);
          lcd.print("3.Humidity:");
          lcd.print(DHT3.humidity,1);
          lcd.print("%"); 
          Blynk.virtualWrite(V3,DHT3.humidity); 
          lcd.setCursor(0,1); 
          lcd.print("4.Humidity:");
          lcd.print(DHT4.humidity,1);
          lcd.print("%");
          Blynk.virtualWrite(V4,DHT4.humidity); 
      } while(lcd.print("")); 
          delay(1000); 
          lcd.clear(); 
}

////////////////////////////////////////////////

void WATERLVLSENSOR(){
  
  int value = analogRead(A0); 
  lcd.setCursor(1, 0);
  lcd.print("Water   status");
  if (value <= 29) {
    lcd.setCursor(3, 1);
    lcd.print("Critical Level");
    Blynk.notify("Critical Water Level!");  
    Blynk.virtualWrite(V6,"Critical Water Level");   
    }
  else if (value > 30 && value < 250) {
    lcd.setCursor(3, 1);
    lcd.print("Normal Level");
    Blynk.virtualWrite(V6,"Normal Water Level");
    } 
  else if (value > 250){
    lcd.setCursor(5, 1);
    lcd.print("High!!");
    Blynk.virtualWrite(V6,"High Water Level");
    
    }
   delay(1000); 
 }```

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

1 Like

You have blocking code in your sketch :
Delay, for next loop.
And you must clean your void loop :scream:
I’m surprised that this code works with blynk.

1 Like

@Luke6478 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard, do not use Blockquotes as you are at the moment.

Pete.

1 Like

noted ill make sure to read this thankyou.

im not familiar with blocking Code
:<

to be honest me and my partner is new to Blynk T_T and we are struggling on learning it since there is only a little tutorial for it in Youtube :< We thought it was easy.

anyways ill take note on the blocking code thankyou for the comment <3

1 Like

thanks for the Info, im new in here thats why i dont know how things work here hehe.