Buffer over flow...please help

buffer overflow comes every time…
please help
using arduino mega…
program…

#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <LiquidCrystal.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "447d46869de544b093f47e322940fb39";
char ssid[] = "My ASUS";
char pass[] = "rajpunith";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#include<dht.h>
#define trigger 7
#define echo 8
#define RELAY1  9
#define RELAY2 13 
#define buzzer 14
#define RELAY3 10
 #define DHT11_PIN 6

dht DHT;
#define DHT11_PIN 22
int thresholdUp = 400;
int thresholdDown =400;
int sensorPin = A0;
int ledPin=40;
int ldrPin = A2;
float time=0,distance=0;
#define DHT11_PIN 13
const int rs = 12, en =11, d4 = 5, d5 =4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
  
  lcd.begin(20,4);
  lcd.clear();
  lcd.print("   IOT Based Smart  ");
  lcd.setCursor(0, 1);
  lcd.print("     AGRICULTURE ");
  lcd.setCursor(1, 1);  
  delay(1000);
  lcd.clear();
  pinMode(sensorPin, INPUT);

//---------------------------------------------------------------------------soil--------------------------------------------------------------------
  pinMode(RELAY1,OUTPUT);
  pinMode(sensorPin,INPUT);
//  ---------------------------------------------------------------------------------L D R ---------------------------------------------------------------------------
  pinMode(ledPin, OUTPUT);
  pinMode(ldrPin, INPUT); 
//-------------------------------------------------------------ULTRA SONIC-----------------------------------------------------------------------------
  pinMode(RELAY2,OUTPUT);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  pinMode(buzzer, OUTPUT);
//  ---------------------------------------------------DHT-----------------------------------------------------------------------------------------------------
  pinMode(RELAY3,OUTPUT); 

Serial.begin(9600);


  delay(10);  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
    
}

void loop()
{

  Blynk.run();
  delay(100);
  String DisplayWords;
  String WaterLevel;
  String moist_prcnt;
  
  lcd.print("   Soil Moisture  ");
  lcd.setCursor(2,1);
  lcd.print("   Indicator  ");
  delay(1000);
  lcd.clear();
  int sensorValue;
  sensorValue = analogRead(sensorPin);
  if (sensorValue >400 )
  {
  lcd.print("Dry, Water it!");
  lcd.setCursor(2,1);

    int mrl=digitalRead(RELAY1);
    while(mrl!=HIGH)
   {
    Blynk.notify("ESP8266 Alert - DRY Field...Turn ON thePump");
    delay(100);
    mrl=digitalRead(RELAY1);
   }
   Blynk.notify("ESP8266 Alert - Pump Turned ON");
   lcd.print("Pump Turned ON");
   delay(2000);
   lcd.clear();
   }
  
 
  else if (sensorValue <400)
  {
   lcd.print("Wet, Leave it!");
   lcd.setCursor(2,1);

   int mrl=digitalRead(RELAY1);
   while(mrl!=HIGH)
   {
    Blynk.notify("ESP8266 Alert - WET Field... Turn OFF the Pump");
    delay(100);
    mrl=digitalRead(RELAY1);
   }
   Blynk.notify("ESP8266 Alert - Motor turned OFF");
   lcd.print("Motor Turned OFF");
   delay(2000);
   lcd.clear();
   }   
  
  moist_prcnt = String( 100 - ( (sensorValue/1023.00) * 100 ) );
  lcd.print("moist_prcnt= "); 
  lcd.setCursor(12,0);
  delay(100);
  lcd.print(moist_prcnt);
  lcd.print("%\n\n");
  WaterLevel = String(analogRead(sensorPin), DEC);
  lcd.print("Water Level:");
  lcd.setCursor(13,2);
  lcd.print( WaterLevel);
  delay(1000);
  lcd.clear();
//============================================LDR===================
  lcd.print("   Light  ");
  lcd.setCursor(2,1);
  lcd.print("   Indicator  ");
  delay(1000);
  lcd.clear();
  int ldrStatus=analogRead(ldrPin);
  if (ldrStatus  < 100) 
  {

    lcd.print("BRIGHT,Turn OFF Lights");
    lcd.setCursor(2,1);
    Blynk.notify("ESP8266 Alert - BRIGHT Light!...Turn OFF Lights");
    int isLed=digitalRead(ledPin);
    while(isLed!= LOW)
    {
     Blynk.notify("ESP8266 Alert - BRIGHT Light!...Turn OFF Lights");
     
     delay(100);
     isLed=digitalRead(ledPin);
    }
    Blynk.notify("ESP8266 Alert - Lights Turned OFF");
    lcd.print("Lights Turned OFF");
    delay(2000);
    lcd.clear();
   }
  
  
  else if (ldrStatus > 100) 
  {

    lcd.print("DARK, LIGHTS ON ");
    lcd.setCursor(2,1);
    Blynk.notify("ESP8266 Alert - LOW Light!...Turn ON Lights");
    int isLed=digitalRead(ledPin);
    while(isLed!=HIGH)
    {
     Blynk.notify("ESP8266 Alert - BRIGHT Light!...Turn ON Lights");
     delay(100);
     isLed=digitalRead(ledPin);
    }
    Blynk.notify("ESP8266 Alert - Lights Turned ON");
    lcd.print("Lights Turned ON");
    delay(2000); 
    lcd.clear();
   }
  
//======================Ultra Sonic==============

  lcd.clear(); 
  lcd.print("   Water Level  ");
  lcd.setCursor(2,1);
  lcd.print("   Indicator  ");
  delay(1000);
  lcd.clear();
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  time=pulseIn(echo,HIGH);
  delay(100);
  distance=time*340/20000;
  lcd.print("   Water Space In  ");
  lcd.setCursor(0,1);
  lcd.print("  Tank is: ");
  lcd.print(distance);
  lcd.print("Cm");
  delay(1000);       
  lcd.clear();
  if(distance<12 )
  {
   int pump=digitalRead(RELAY2);
   lcd.print("Water Tank Full ");
   lcd.setCursor(2,1);

    digitalWrite(buzzer, LOW);
    Blynk.notify("ESP8266 Alert - Tank FULL!...Turn OFF The Motor");
     pump=digitalRead(RELAY2);
    while(pump!=HIGH)
    {
     Blynk.notify("ESP8266 Alert - Tank FULL!...Turn OFF The Motor");
     delay(100);
     pump=digitalRead(RELAY2);
    }
    Blynk.notify("ESP8266 Alert - Motor Turned OFF");
    lcd.print("Motor Turned OFF");
    delay(2000);  
    lcd.clear();
   } 
  
 
  else if(distance>12)
  {
   lcd.print("LOW Water Level");
   lcd.setCursor(2,1);

    digitalWrite(buzzer, HIGH);
    Blynk.notify("ESP8266 Alert - Tank EMPTY!...Turn ON The Motor");
    int pump=digitalRead(RELAY2);
    while(pump!=LOW)
    {
     Blynk.notify("ESP8266 Alert - Tank EMPTY!...Turn ON The Motor");
     delay(100);
     pump=digitalRead(RELAY2);
    }
    Blynk.notify("ESP8266 Alert - Motor Turned ON");
    lcd.print("Motor Turned ON");
    delay(2000);
    lcd.clear();
   
  }

//=============================DHT=================================================
  
  lcd.print("   TEMPERATURE &"); 
  lcd.setCursor(0, 1);
  lcd.print("   HUMIDITY!");
  delay(1000);
  lcd.clear();
  int chk = DHT.read11(DHT11_PIN);
  lcd.print(" Humidity= " );
  lcd.print(DHT.humidity);
  lcd.print("% ");
  lcd.setCursor(0, 1);
  lcd.print(" Temparature= ");
  lcd.print(DHT.temperature);
  lcd.print("*C");
  lcd.setCursor(3, 1);
  delay(1000);
  lcd.clear();
  if(DHT.temperature<35)
  {
   lcd.print("Normal Temperature ");
   lcd.setCursor(2,1);
    Blynk.notify("ESP8266 Alert - Normal Temperature!...Turn OFF Fans");
    int isfan=digitalRead(RELAY3);
    Serial.print(RELAY3);
    while(isfan!=HIGH)
    {
     Blynk.notify("ESP8266 Alert - Normal Temperature!...Turn OFF Fans");
     delay(100);
     isfan=digitalRead(RELAY3);
     Serial.print(RELAY3); 
    }
    Blynk.notify("ESP8266 Alert - Fans Turned OFF");
    lcd.print("FAN Turned OFF");
    delay(5000);
    lcd.clear();
   }
  
  else if(DHT.temperature>35)
  {
   lcd.print("High Teperature");
   lcd.setCursor(2,1);
    Blynk.notify("ESP8266 Alert - High Temperature!...Turn ON Fans");
    int isfan=digitalRead(RELAY3);
    Serial.print(RELAY3);
    while(isfan!=LOW)
     {
      Blynk.notify("ESP8266 Alert - High Temperature!...Turn ON Fans");
      delay(100);
      isfan=digitalRead(RELAY3);
      Serial.print(RELAY3);
     }
    Blynk.notify("ESP8266 Alert - Fans Turned ON");
    lcd.print("FAN Turned ON");
    delay(2000);
    lcd.clear();
   
  }
}

That’s one of the worst void loops I’ve seen in a long time :rofl::rofl::rofl::rofl:

Pete.

1 Like

yes indeed !!!
:rofl::rofl::rofl::rofl::rofl::rofl:

@Ashwith_Saldanha

in the void loop you must have only 2 lines !

you don’t need Blynk for that project
the only Blynk function you are using, is blynk.notify !
better use SMS API

@PeteKnight :smile:

1 Like

You must avoid the use of delay…

Tears of blood are falling from my eyes after check your code…

Try to use correctly the ```
It’s hard to understand anything.

@PeteKnight couldn’t agree more with you. You are being very polished, diplomatic, humorous and mild in your statement. @trystan4861 said tears of blood. Yeah…

Proper way to show program…

Blynk - FTFC

1 Like

giphy

How many DHT you have? And i what pin is attached? (the reply must be one at pin13) Why define the same const 3 times?

Well try this code…

#include <DHT.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <LiquidCrystal.h>
#include <Tasker.h>

char auth[] = "DO NOT SHARE THIS INFORMATION!!";
char ssid[] = "EITHER THIS OTHER INFORMATION!!";
char pass[] = "OR NEITHER OTHER INFORMATION!!";
#define EspSerial Serial
#define ESP8266_BAUD 115200
#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 13
#define trigger 7
#define echo 8
#define RELAY1  9
#define RELAY2 13 
#define RELAY3 10
#define buzzer 14
#define CycleSensorInterval 2000
#define LCDLines 4
#define LCDColumns 20
#define sensorPin A0
#define ledPin 40
#define ldrPin A2

Tasker Task(true);
DHT DHTSensor(DHTPIN, DHTTYPE);
byte Sensor2Check=0;
ESP8266 wifi(&EspSerial);

LiquidCrystal LCD(12, 11, 5, 4, 3, 2);

void CenterLCD(String SMS,byte Line=0,long ClearAfter=0);

void ClearLCD(int Line)
{
  CenterLCD("",Line);
}
void LeftLCD(String SMS,byte Line=0,long ClearAfter=0)
{
  if (Task.scheduledIn(ClearLCD,Line)>0) Task.cancel(ClearLCD,Line);
  String FullText=SMS;
  for (byte i=SMS.length();i<LCDColumns;i++) FullText.concat(" ");
  LCD.setCursor(0,Line);
  //FullText.toUpperCase();
  LCD.print(FullText);
  if (ClearAfter>0) Task.setTimeout(ClearLCD,ClearAfter,Line);
}

void CenterLCD(String SMS,byte Line=0,long ClearAfter=0)
{
  if (Task.scheduledIn(ClearLCD,Line)>0) Task.cancel(ClearLCD,Line);
  String FullText="";
  int inicio=floor((LCDColumns-(SMS.length())) / 2);
  if (inicio<0) inicio=0;
  for (byte i=0;i<LCDColumns;i++)
  {
    if (i<inicio || i>=inicio+SMS.length()) FullText.concat(" ");
    else FullText.concat(SMS.charAt(i-inicio));
  }
  LCD.setCursor(0,Line);
  //FullText.toUpperCase();
  LCD.print(FullText);
  if (ClearAfter>0) Task.setTimeout(ClearLCD,ClearAfter,Line);
}
float GetDistance()
{
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  return (float) pulseIn(echo,HIGH)*340/20000;
}
void DoBeep()
{
  digitalWrite(buzzer,!digitalRead(buzzer));
}
void CheckSensors()
{
  for (int i=0;i<=3;i++) ClearLCD(i);
  Sensor2Check++;
  if (Sensor2Check>4) Sensor2Check=1;
  switch (Sensor2Check)
  {
    case 1:
      CenterLCD("Moisture Indicator",0);
      if (analogRead(sensorPin)>=400)
      {
        if (!digitalRead(RELAY1))
        {
          digitalWrite(RELAY1,HIGH);
          Blynk.notify("ESP8266 Alert - Pump Turned ON");
          CenterLCD("Dry, Pump Turned ON",1);
        }
        else CenterLCD("Dry, Pump ON",1);
      }
      else
      {
        if (digitalRead(RELAY1))
        {
          digitalWrite(RELAY1,LOW);
          Blynk.notify("ESP8266 Alert - Pump Turned OFF");
          CenterLCD("Wet, Pump Turned OFF",1);
        }
        else CenterLCD("Wet, Pump OFF",1);
      }   
      LeftLCD("Moist: "+String(100 -((analogRead(sensorPin)/1023.00) * 100 ))+"%",2);
      LeftLCD("H2O Level: "+String(analogRead(sensorPin), DEC),3);
    break;
    case 2:
      CenterLCD("Light Indicator",0);
      if (analogRead(ldrPin)<=100) 
      {
        if (digitalRead(ledPin))
        {
          digitalWrite(ledPin,LOW);
          Blynk.notify("ESP8266 Alert - Lights Turned OFF");
          CenterLCD("Lights Turned OFF!",1);
        }
        else CenterLCD("BRIGHT, Lights OFF",1);
      }
      else
      {
        if (!digitalRead(ledPin))
        {
          digitalWrite(ledPin,HIGH);
          Blynk.notify("ESP8266 Alert - Lights Turned ON");
          CenterLCD("Lights Turned ON");
        }
        else CenterLCD("DARK, Lights ON");
      }
    break;
    case 3:
      CenterLCD("Water Level in Tank",0);
      CenterLCD(String(GetDistance())+"Cm",1);
      if(GetDistance()<=12)
      {
        CenterLCD("Tank Full Motor OFF",2);
        if (digitalRead(RELAY2))
        {
          if (Task.scheduledIn(DoBeep)>0) Task.cancel(DoBeep);
          digitalWrite(buzzer, LOW);
          digitalWrite(RELAY2, LOW);
          Blynk.notify("ESP8266 Alert - Motor Turned OFF");
          CenterLCD("Motor Turned OFF",2);
        }
      } 
      else
      {
        CenterLCD("LOW Water Level",2);
        if(!digitalRead(RELAY2))
        {
          digitalWrite(RELAY2,HIGH);
          digitalWrite(buzzer, HIGH);
          Blynk.notify("ESP8266 Alert - Motor Turned ON");
          CenterLCD("Motor Turned ON",2);
          Task.setTimeout(DoBeep,500);
        }
      }
    break;
    case 4:
      CenterLCD("HEAT & HUMIDITY",0);
      LeftLCD("Temparature: "+String(DHTSensor.readTemperature())+"*C",1);
      LeftLCD("Humidity: "+String(DHTSensor.readHumidity())+"%",2);
      if(DHTSensor.readTemperature()<=35)
      {
        CenterLCD("Normal Temperature",3);
        if(digitalRead(RELAY3))
        {
          digitalWrite(RELAY3,LOW);
          Blynk.notify("ESP8266 Alert - Fans Turned OFF");
          CenterLCD("FAN Turned OFF",3);
        }
      }
      else
      {
        CenterLCD("High Teperature",3);
        if(!digitalRead(RELAY3))
        {
          digitalWrite(RELAY3,HIGH);
          Blynk.notify("ESP8266 Alert - Fans Turned ON");
          CenterLCD("FAN Turned ON",3);
        }
      }
    break;
  }
}
void setup()
{
  LCD.begin(LCDColumns,LCDLines);
  LCD.clear();
  CenterLCD("IOT Based Smart",0);
  CenterLCD("AGRICULTURE",1);
  pinMode(sensorPin, INPUT);
  pinMode(ldrPin, INPUT); 
  pinMode(echo, INPUT);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT); 
  pinMode(ledPin, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(buzzer, OUTPUT);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  Task.setTimeout(CheckSensors,CycleSensorInterval);
}

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

I make the system totally automated. The system cycles the sensor check every 2 seconds (configurable) and check one sensor each time.

I didn’t try it… but it should work fine!

There is a clear problem and that is that when the engine of the water tank is turned on, the level is checked every 8 seconds and depending on the speed of the engine the water could overflow.

With minor changes you can make it works with a simple ESP-01 with a MCP23017 and a I2C LCD

If the overflow still happening maybe it’s due to some var type mistake… Post a log or say where it fails

… It is a little disappointing to see a user create an account one day, ask a question that same day and when he receives an answer that same day he does not go back to the forum from that same day and he is also so polite that he does not even say thank you. (I do not mind not receiving the thanks, but at least he could say he tried what I programmed for him …)

I hope that at least this post will help other people to know what not to do, both at the programming level and at the community level …

1 Like

:wink:
+1
I took great interest in your post

Maybe I scared him off with my response about his void loop :roll_eyes:

Pete.

i think that yes… you scared him… check his summary… 2m reading!! https://community.blynk.cc/u/ashwith_saldanha/summary :sweat_smile:

Oh well, hope he didn’t head on over to Stack Overflow instead or he’ll probably have had a nervous breakdown by now!

Pete.