Agriculture Automation System...... Problem in PushButton switches while runing the project

hello,
i made my project on arduino mega 2560 and connect to blink server through Ethernet Shield.
when i am trying to run my project then project works properly but giving problem in digital pins i don’t understand why this happening. But when i run individual code like only for push button switches then it works ok but not working when i run the my main sketches and it happening only with buttons. can any one tell me how to over come this problem.

please reply…

lets see what i done in my project i used some sensor such as
1.dht11 - for temp and humidity measurement
2.PIR - for security purpose
3. Ultrasonic HC-SR04- For well water level detection and calculating how much water present in well in litters and percentage as well.
4. soil moisture sensor- for measurement of soil moisture in farm
5. 4 channel relay driver- out of this 2 used for driving ac motor and 2 for switching lights


my code you can see here…

 #define BLYNK_DEBUG           // Optional, this enables lots of prints
 #define BLYNK_PRINT Serial
 #define BLYNK_PRINT Serial  
 #include "DHT.h"
 #include <SPI.h>
 #include <Ethernet.h>
 #include <BlynkSimpleEthernet.h>
 #include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

 #define DHTPIN 8              // what digital pin we're connected to
 #define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "1.AUTOMATION");
    delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        timer.setInterval(1000, repeatMe);; 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        timer.setInterval(1000, repeatMe);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        timer.setInterval(1000, repeatMe); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        timer.setInterval(1000, repeatMe);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void moisture_cal()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

void loop()
{
    Blynk.run();
    timer.run();
    moisture_cal();
    cal_water();
    cal_temp();
    security();
    timer.setInterval(1000, repeatMe);
    
}

I’d remove everything from the loop, except timer.run and blynk.run. Everything else can go in the void setup().

You need to initialize timers outside loop(), unless you want to change them, but even so … something along the lines of this:

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);

void setup()
{
  // BLynk setup stuff here
}

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

This is the way to go, it works flawless at my home domotic stuff and I also control relays manual.

1 Like

i modify my code and try it but when i upload the code then it is not connecting to blynk server…

[0] Blynk v0.3.2-beta
[0] Getting IP…
[5740] My IP: 192.168.1.4
[5740] Connecting to cloud.blynk.cc:8442

and it stop here…
how to overcome this problem now…


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "1.AUTOMATION");
    delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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

Can you try removing all the delays from the Setup()? I’m not sure, but I’d remove them, just to see what happens.

no it is not working properly again same error…it is not connecting to server…

[0] Blynk v0.3.2-beta
[0] Getting IP…
[5740] My IP: 192.168.1.4
[5740] Connecting to cloud.blynk.cc:8442

code:


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    //delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    //delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    //delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    //delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    //delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "1.AUTOMATION");
   // delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      //delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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

if i upload the code 2 then it works properly except push button…


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    m1_led.off();
    m2_led.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "2.AUTOMATION");
    delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
      m1_led.off();
      m2_led.off();
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void moisture_cal()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void loop()
{
    Blynk.run();
    timer.run();
    moisture_cal();
    cal_water();
    cal_temp();
    security();
    delay(1000);
}

I’m guessing what you try to accomplish here is:

  1. determine the waterlevel
  2. act accordingly to pump in water or stop the pumps

Does that sound about right? Cause I have my doubts about all the delays you are using in the cal_water routine. I’m either missing something or all the delay(10)'s you use there add up to too much, I’m not sure.

I think some routine is maybe taking too long, but not sure, your code isn’t very readable :wink:

actually what i am doing i am detecting water surface through ultrasonic sensor and it giving me distance from it ok…
now we know the dept of well and radius that is well nothing but a cylinder
so i calculate height of water by subtracting ultrasonic distance from well height…
now calculating volume of cylinder i.e
volume= pi*(radius*radius)*height
now to calculating how much water in that volume there is one formula
water in litter = volume/61.0237

Too many delays in setup. Blynk will disconnect you after 15 seconds of non-activity that happens because of your delays. Either use local server with increased delay interval either change your code.

can you highlight the which part is wrong in this code


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    //delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    //delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    //delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    //delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    //delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "1.AUTOMATION");
   // delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val<650)
      {
        digitalWrite(motor1_pin, HIGH);
        digitalWrite(motor2_pin, HIGH);
        delay(10); 
      }
      if(val>649)
      {
        digitalWrite(motor1_pin, LOW);
        digitalWrite(motor2_pin, LOW);
        delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      //delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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

That should do the trick I think.

Currently I’m at work, so I don’t have a chance to take a deeper look into your code, but I may be able to do that tonight.

1 Like

Please try to comment all code after :

while (Blynk.connect() == false) 
  {
    // Wait until connected
  }

in setup function. Does that help?

i modify my code and try but still not working plz help me…

#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();      // at the start up off all the led indications low water level 
    med_lev.off();      // medium water level indication
    high_lev.off();     // high water level indications
    no_lev.off();       // no water level indication
    sec_led.off();      // security alart indicator
    auto_led.off();     // automation mode led indication
    man_led.off();      // manual mode led indication
    err_led.off();      // error led indication if any error in dht sensor
    digitalWrite(buzzer_pin, LOW); // bzzer connected to pin 3 make it off initially 
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");     // nitify when project is connected to server
    //delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO");       // display wel come mst
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    //delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(2, 1, "INITIAZING..");
    //delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(5, 1, "ONLINE");
    //delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE");       // secect mode of operation automation or mannual
    lcd.print(0, 1, "1.AUTO");            // if pin 52 is high then go to automation mode operations
    lcd.print(11, 1, "2.MAN");            // if pin52 is low then go to manual mode operations
    //delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE");    // here we directly select mode 1 so display that mode
    lcd.print(0, 1, "1.AUTOMATION");
   // delay(2500);
    lcd.clear();
}

void cal_water()
{
  /************* this function for sending high low pulses t0 ultrasonic sensor*************/
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  /****************************************************************************************/
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");          // print msg on lcd ex:  "WATER LTR: 6725L"
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");          // print water level in per % ex "WATER LEV:  67.52%"
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)                  // if water level is less than 15% send notification water level is low
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)  // if water level is between 15% to 25 % then glow no_lev led
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )  // if water level is in between 15% to 33.33% them glow low_lev led and make motor off 
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);      // water level is low so off the motor if it is activated
      digitalWrite(motor2_pin, LOW);     
    }
  if (water_per > 33.33 && water_per < 66.66 )  // if water level is between 33.33 to 66.66% then check soil moisture level
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
  if (water_per > 66.66 )   // if waer level gretter than 66.66% that is water is present so glow all level leds and off no_level led
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security() // PIR sensor connecte dot pin 3 if any human detect then give notification and glow sec_led
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      //delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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

But you didn’t comment block I asked you to.

ok sorry…
error again… not connecting…

[0] Blynk v0.3.2-beta
[0] Getting IP…
[5740] My IP: 192.168.1.4
[5740] Connecting to cloud.blynk.cc:8442
[0] Blynk v0.3.2-beta
[0] Getting IP…
[5740] My IP: 192.168.1.4
[5740] Connecting to cloud.blynk.cc:8442


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }

   /*   
    low_lev.off();      // at the start up off all the led indications low water level 
    med_lev.off();      // medium water level indication
    high_lev.off();     // high water level indications
    no_lev.off();       // no water level indication
    sec_led.off();      // security alart indicator
    auto_led.off();     // automation mode led indication
    man_led.off();      // manual mode led indication
    err_led.off();      // error led indication if any error in dht sensor
    digitalWrite(buzzer_pin, LOW); // bzzer connected to pin 3 make it off initially 
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");     // nitify when project is connected to server
    //delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO");       // display wel come mst
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    //delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(2, 1, "INITIAZING..");
    //delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(5, 1, "ONLINE");
    //delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE");       // secect mode of operation automation or mannual
    lcd.print(0, 1, "1.AUTO");            // if pin 52 is high then go to automation mode operations
    lcd.print(11, 1, "2.MAN");            // if pin52 is low then go to manual mode operations
    //delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE");    // here we directly select mode 1 so display that mode
    lcd.print(0, 1, "1.AUTOMATION");
   // delay(2500);
    lcd.clear();
    */
}

void cal_water()
{
  /************* this function for sending high low pulses t0 ultrasonic sensor*************/
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  /****************************************************************************************/
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");          // print msg on lcd ex:  "WATER LTR: 6725L"
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");          // print water level in per % ex "WATER LEV:  67.52%"
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)                  // if water level is less than 15% send notification water level is low
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)  // if water level is between 15% to 25 % then glow no_lev led
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )  // if water level is in between 15% to 33.33% them glow low_lev led and make motor off 
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);      // water level is low so off the motor if it is activated
      digitalWrite(motor2_pin, LOW);     
    }
  if (water_per > 33.33 && water_per < 66.66 )  // if water level is between 33.33 to 66.66% then check soil moisture level
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
  if (water_per > 66.66 )   // if waer level gretter than 66.66% that is water is present so glow all level leds and off no_level led
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security() // PIR sensor connecte dot pin 3 if any human detect then give notification and glow sec_led
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      //delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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

Does simple Blynk sketch without any your code works for you? Maybe problem in your router?

right now i upload my sketch 1 and that working properly but not sketch 2.

sketch 1 code:

 
#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    m1_led.off();
    m2_led.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "2.AUTOMATION");
    delay(2500);
    lcd.clear();
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
      m1_led.off();
      m2_led.off();
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
       //delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
       //delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void moisture_cal()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void loop()
{
    Blynk.run();
    timer.run();
    moisture_cal();
    cal_water();
    cal_temp();
    security();
    timer.setInterval(1000, repeatMe);
}

sketch 2 code that is not working properly… same connection error in blynk server


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    pinMode(mode, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }

   /*   
    low_lev.off();      // at the start up off all the led indications low water level 
    med_lev.off();      // medium water level indication
    high_lev.off();     // high water level indications
    no_lev.off();       // no water level indication
    sec_led.off();      // security alart indicator
    auto_led.off();     // automation mode led indication
    man_led.off();      // manual mode led indication
    err_led.off();      // error led indication if any error in dht sensor
    digitalWrite(buzzer_pin, LOW); // bzzer connected to pin 3 make it off initially 
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");     // nitify when project is connected to server
    //delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO");       // display wel come mst
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    //delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(2, 1, "INITIAZING..");
    //delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(5, 1, "ONLINE");
    //delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE");       // secect mode of operation automation or mannual
    lcd.print(0, 1, "1.AUTO");            // if pin 52 is high then go to automation mode operations
    lcd.print(11, 1, "2.MAN");            // if pin52 is low then go to manual mode operations
    //delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE");    // here we directly select mode 1 so display that mode
    lcd.print(0, 1, "1.AUTOMATION");
   // delay(2500);
    lcd.clear();
    */
}

void cal_water()
{
  /************* this function for sending high low pulses t0 ultrasonic sensor*************/
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  /****************************************************************************************/
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");          // print msg on lcd ex:  "WATER LTR: 6725L"
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");          // print water level in per % ex "WATER LEV:  67.52%"
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)                  // if water level is less than 15% send notification water level is low
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)  // if water level is between 15% to 25 % then glow no_lev led
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )  // if water level is in between 15% to 33.33% them glow low_lev led and make motor off 
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);      // water level is low so off the motor if it is activated
      digitalWrite(motor2_pin, LOW);     
    }
  if (water_per > 33.33 && water_per < 66.66 )  // if water level is between 33.33 to 66.66% then check soil moisture level
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
  if (water_per > 66.66 )   // if waer level gretter than 66.66% that is water is present so glow all level leds and off no_level led
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security() // PIR sensor connecte dot pin 3 if any human detect then give notification and glow sec_led
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void cal_moisture()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

void check_mode()
{
  if(mode_state == HIGH)
    {
      lcd.clear(); 
      lcd.print(0, 0, "SELECTED MODE"); 
      lcd.print(0, 1, "2.MANUAL");
      //delay(2500);
      while(1)
      {
         Blynk.run();
         timer.run();
         security();
      }
    }
    else
    {
      //
    }
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, cal_moisture);
byte timerCal_security = timer.setInterval(1000, security);

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


So if you know difference between them - you may locate problem, aren’t you?

yaa now it working properly but again digital pins are not working when i pressing switch button on screen…:grimacing:


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }
   /*
    low_lev.off();
    med_lev.off();
    high_lev.off();
    no_lev.off();
    m1_led.off();
    m2_led.off();
    sec_led.off();
    auto_led.off();
    man_led.off();
    err_led.off();
    digitalWrite(buzzer_pin, LOW);
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
    delay(5000);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO"); 
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(5000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");
    lcd.print(2, 1, "INITIAZING..");
    delay(3000);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS"); 
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE"); 
    lcd.print(0, 1, "1.AUTO");
    lcd.print(11, 1, "2.MAN");
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE"); 
    lcd.print(0, 1, "2.AUTOMATION");
    delay(2500);
    lcd.clear();
    */
}

void cal_water()
{
    // Send a short LOW followed by HIGH pulse to Trigger input:0
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);
      digitalWrite(motor2_pin, LOW);
      m1_led.off();
      m2_led.off();
    }
  if (water_per > 33.33 && water_per < 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
       //delay(10);
      }
    }
  if (water_per > 66.66 )
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
       //delay(10);
      }
      else
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}

void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void moisture_cal()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, moisture_cal);
byte timerCal_security = timer.setInterval(1000, security);

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

you can upload the code into your board and connect it and try to whether the switch button is working or not…

code:


#define BLYNK_DEBUG           // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial  
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken

WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11),m1_led(12),m2_led(13);
WidgetLCD lcd(4);

const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 26;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;

#define DHTPIN 8              // what digital pin we're connected to
#define DHTTYPE DHT11         // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}


void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth);
    dht.begin();
    pinMode(pingPin, OUTPUT);
    pinMode(inPin, INPUT);
    pinMode(motor1_pin, OUTPUT);
    pinMode(motor2_pin, OUTPUT);
    pinMode(security_pin, INPUT);
    while (Blynk.connect() == false) 
      {
        // Wait until connected
      }  
    /*low_lev.off();      // at the start up off all the led indications low water level 
    med_lev.off();      // medium water level indication
    high_lev.off();     // high water level indications
    no_lev.off();       // no water level indication
    sec_led.off();      // security alart indicator
    auto_led.off();     // automation mode led indication
    man_led.off();      // manual mode led indication
    err_led.off();      // error led indication if any error in dht sensor
    digitalWrite(buzzer_pin, LOW); // bzzer connected to pin 3 make it off initially 
    Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");     // nitify when project is connected to server
    delay(2500);
    lcd.clear(); 
    lcd.print(2, 0, "WEL-COME TO");       // display wel come mst
    lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
    delay(2500);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(2, 1, "INITIAZING..");
    delay(2500);
    lcd.clear(); 
    lcd.print(3, 0, "SYSTEM IS");         // display system is online msg
    lcd.print(5, 1, "ONLINE");
    delay(2500);
    lcd.clear();
    lcd.print(2, 0, "SELECT MODE");       // secect mode of operation automation or mannual
    lcd.print(0, 1, "1.AUTO");            // if pin 52 is high then go to automation mode operations
    lcd.print(11, 1, "2.MAN");            // if pin52 is low then go to manual mode operations
    delay(2500);
    lcd.clear(); 
    lcd.print(0, 0, "SELECTED MODE");    // here we directly select mode 1 so display that mode
    lcd.print(0, 1, "1.AUTOMATION");
    delay(2500);
    lcd.clear();*/
    
}
void cal_water()
{
  /************* this function for sending high low pulses t0 ultrasonic sensor*************/
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  /****************************************************************************************/
  duration = pulseIn(inPin, HIGH);
  inches = microsecondsToInches(duration);
  indec = ((duration - inches * inchconv) * 10 / inchconv);
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  actual= fixed - inches;
  actual_ft=actual/12;
  volume=3.14*radius*radius*actual;
  ltr1=volume/61.0237;//in foot
  hight = String(inches) + "." + String(indec);
  hight_A = String(volume) + "." + String(indec);
  hight_W = String(actual_ft) + "." + String(indec);
  water_L = String(ltr1) + "." + String(indec);
  water_per=(ltr1/6724)*100;
  lcd.print(0,0,"WATER LTR:");          // print msg on lcd ex:  "WATER LTR: 6725L"
  lcd.print(10,0, ltr1);
  lcd.print(14,0, " L");
  lcd.print(0,1,"WATER LEV:");          // print water level in per % ex "WATER LEV:  67.52%"
  lcd.print(10,1, water_per);
  lcd.print(15,1, "%");
  //Automation is on
  auto_led.on();
  if(water_per < 15)                  // if water level is less than 15% send notification water level is low
    {
       Blynk.notify("WATER LEVEL IS LOW");
       timer.setInterval(1000, repeatMe);
    }
  if(water_per > 15 && water_per < 25)  // if water level is between 15% to 25 % then glow no_lev led
  {
    no_lev.on();
  }
  if (water_per > 15 && water_per <33.33 )  // if water level is in between 15% to 33.33% them glow low_lev led and make motor off 
    {
      low_lev.off();
      med_lev.off();
      high_lev.off();
      no_lev.off();
      digitalWrite(motor1_pin, LOW);      // water level is low so off the motor if it is activated
      digitalWrite(motor2_pin, LOW);     
    }
  if (water_per > 33.33 && water_per < 66.66 )  // if water level is between 33.33 to 66.66% then check soil moisture level
    {
      low_lev.on();
      med_lev.on();
      high_lev.off();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
  if (water_per > 66.66 )   // if waer level gretter than 66.66% that is water is present so glow all level leds and off no_level led
    {
      low_lev.on();
      med_lev.on();
      high_lev.on();
      no_lev.off();
      if(val1<650)         // if value of soil moisture sensor 1 is less than 650 then activate motor 1 and on motor led
      {
        digitalWrite(motor1_pin, HIGH);
        m1_led.on();
        //delay(10);
      }
      else                // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor1_pin, LOW);
        m1_led.off();
        //delay(10);
      }
      if(val2<650)       // if value of soil moisture sensor 2 is less than 650 then activate motor 2 and on motor led
      {
        digitalWrite(motor2_pin, HIGH);
        m2_led.on();
        //delay(10); 
      }
      else               // if gretter then off motor 2 and off motor led also 
      {
        digitalWrite(motor2_pin, LOW);
        m2_led.off();
        //delay(10);
      }
    }
}


void cal_temp()
{
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    err_led.on();
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h); 
  timer.setInterval(1000, repeatMe);
}
void security()
{
  sec_state = digitalRead(security_pin);
  if (sec_state == HIGH)
  {
    sec_led.on();
    digitalWrite(buzzer_pin, HIGH);
    Blynk.notify("security alart....!");
  } 
  else 
  {
    sec_led.off();
    digitalWrite(buzzer_pin, LOW);
  }
}

void moisture_cal()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  val=(val1+val2)/2;
  timer.setInterval(1000, repeatMe);
  Serial.print("Moisture Sensor1 Value:");
  Serial.println(val1);
  Serial.print("Moisture Sensor2 Value:");
  Serial.println(val2);
  Serial.print("Moisture average Value:");
  Serial.println(val);
  Blynk.virtualWrite(V10, val);
}

byte timerCal_Water = timer.setInterval(1000, cal_water);
byte timerCal_Temp = timer.setInterval(1000, cal_temp);
byte timerCal_moisture = timer.setInterval(1000, moisture_cal);
byte timerCal_security = timer.setInterval(1000, security);

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