ESP-01 Not responding and dropping

Help needed,
I have adjusted my code to what I think is better for the blynk application but I am having trouble staying online and esp not responding. I have changed the baud rate to 9600 and yesterday the code and baud didnt match and it randomly started working. I’m really confused and have to hand up this project in 2 days.

Today Baud was changed to 9600 and was working but stopped at the next upload with no changes to ESP not responding. Any help really appreciated as my time is running out :frowning:

#include <util/delay.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <Wire.h>

#define pumptime 2500 //Pump on time 

//BLYNK- APP and WEB requirements 
#define BLYNK_TEMPLATE_ID "TMPL6Pv17xQ0"//
#define BLYNK_TEMPLATE_NAME "Greenhouse"//project name
#define BLYNK_AUTH_TOKEN "5j3T5Wy-3sapGdJpzz3F-T3AfyN-IKEN"

#define BLYNK_PRINT Serial
//Blynk Libraries
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//BlynkTimer timer;
BlynkTimer timer;
#define INTERVAL 5000L //5 sec timer 

// Your WiFi details.
// Set password to "" for open networks.
char ssid[] = "Fridgefi";
char pass[] = "xxxxxxx";

// For arduino Uno
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

// DHT parameters
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Arduino Pins
#define FAN 10 //enable Fan on pin 10
#define BULB  9 //enable Bulb on pin 9
#define PUMP  8  //enable pump on pump 8
#define RedLED1  7 //Red LED for water level
#define GreenLED1  4 //Green LED for water level
#define SoilMoisture  A1 //Soil Moisture Sensor
#define WaterLevel  A2// WaterLevel Sensor

//Virtual pins FOR BLYNK APP
#define V1_SOIL_MOISTURE V1
#define V2_WATER_LEVEL V2
#define V3_GREEN_LED V3
#define V5_Humidity V5
#define V6_Temperature V6
#define V7_RED_LED V7
#define V8_PUMP V8
#define V9_BULB V9
#define V10_FAN V10

const int SoilDry = 520; //Dry value of soil
const int SoilWet = 260; //Wet value of soil

 void getTemp(){
  // Read DHT11- Inside Greenhouse
int temperature = dht.readTemperature();// read the temp and assign the value to temperature 
int humidity = dht.readHumidity();// read the humidity and assign the value to temperature 
  Blynk.virtualWrite(V6_Temperature, temperature);
  Blynk.virtualWrite(V5_Humidity, humidity);
  Serial.print("Temp: ");
  Serial.println(temperature);
  Serial.print("Humidity: ");
  Serial.println(humidity);

  // Check if reads fails
  if (isnan(temperature) || isnan(humidity)) {// if not a number on temp or humidity return the error below
    //Serial.println(F("Failed to read from DHT sensor"));
    digitalWrite (RedLED1,HIGH);
    digitalWrite (GreenLED1,LOW);// turn on red LED for alarm
    return;
  }
    if (temperature < 22) {
    digitalWrite(FAN, LOW);//Turn off the fan
    digitalWrite(BULB,HIGH);
    Blynk.virtualWrite(V9_BULB, HIGH);
    Blynk.virtualWrite(V10_FAN, LOW);
  } 
    else if(temperature > 23 ) {
    digitalWrite(FAN, HIGH);//Turn on the fan 
    digitalWrite(BULB, LOW);//Turn off the bulb
    Blynk.virtualWrite(V9_BULB, LOW);
    Blynk.virtualWrite(V10_FAN, HIGH);
  }
 }
  void checkMoisture(){
 int SoilMoisture = analogRead (SoilMoisture);//read value of soil moisture
 int WaterLevel = analogRead (WaterLevel); //read value of waterlevel
 int SoilMoistpercent; // moisture value in percentage
 SoilMoistpercent = map(SoilMoisture,SoilWet,SoilDry,100,0); // To display the soil moisture value in percentage
 Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoistpercent);
 Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);

  Serial.print("WaterLevel: ");
  Serial.println(WaterLevel);
  Serial.print("SoilMoisture % is : ");
  Serial.println(SoilMoistpercent);

   if (SoilMoistpercent < 50 && WaterLevel > 350){ 
    digitalWrite (RedLED1,LOW);
    digitalWrite (GreenLED1,HIGH);
    digitalWrite (PUMP,HIGH);
    Blynk.virtualWrite(V7_RED_LED, LOW);
    Blynk.virtualWrite(V3_GREEN_LED, HIGH);
    Blynk.virtualWrite(V8_PUMP, HIGH);
    delay (pumptime); 
    digitalWrite (PUMP,LOW);    
    digitalWrite (RedLED1,LOW);
    digitalWrite (GreenLED1,HIGH);
    Blynk.virtualWrite(V8_PUMP, LOW);
    Blynk.virtualWrite(V3_GREEN_LED, HIGH);
    Blynk.virtualWrite(V7_RED_LED, LOW);
    delay(1000);
}
 else if (WaterLevel < 340){
    digitalWrite (RedLED1,HIGH); //turn  on the red LED
    Blynk.virtualWrite(V7_RED_LED, HIGH);//Write to app
    digitalWrite (GreenLED1,LOW);//Turn off green led 
    Blynk.virtualWrite(V3_GREEN_LED, LOW);//write to blynk
    digitalWrite (PUMP,LOW); //turn off pump
    delay(5000);//5 second delay
 }
}
void mytimer(){// write on the timer ticks 
Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoisture);  
Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);  
Blynk.virtualWrite(V3_GREEN_LED, GreenLED1);
Blynk.virtualWrite(V7_RED_LED, RedLED1);
Blynk.virtualWrite(V8_PUMP, PUMP);
Blynk.virtualWrite(V9_BULB, BULB);
Blynk.virtualWrite(V10_FAN, FAN);
Blynk.syncVirtual(V1, V2, V3, V5, V6, V7, V8, V9, V10);// sync with app
}

void setup() {
  // Debug console
  Serial.begin(9600);// for Blynk app 
  // Set ESP8266 baud rate
 EspSerial.begin(ESP8266_BAUD);
 delay(10);
  // Initialize Temp & Humidity Sensor
dht.begin();

//Blynk WIFI 
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80); //BLYNK app

//timer.setInterval(INTERVAL, getTemp);

 //Enable pinMode 
//OUTPUTS//
pinMode (FAN, OUTPUT); //enable pin 10 as output 
digitalWrite(FAN, LOW);//initial low
pinMode (BULB, OUTPUT); //enable pin 9 as output 
digitalWrite(BULB, LOW);//initial low
pinMode (PUMP, OUTPUT); //enable pin 8 as output 
digitalWrite(PUMP, LOW);//initial low
pinMode (RedLED1,OUTPUT);
digitalWrite(RedLED1, LOW);//initial low
pinMode (GreenLED1,OUTPUT);
digitalWrite(GreenLED1, LOW);//initial low
//INPUTS//
pinMode(SoilMoisture, INPUT); //enable pin A1 as input 
pinMode(WaterLevel, INPUT);//enable pin A2 as input
//pinMode()

}
void loop() {
  Blynk.run();//Runs blynk
  getTemp (); //function for temp
  checkMoisture();//function for soil moisture 
  timer.run();//Run the Blynk timer
}

These two lines need to come out of your void loop, and you need to call them with BlynkTimer interval timers.

You also need to remove the delays from these functions, as they will be redundant when you use BlynkTimer to set the frequency at which they are called.

In addition, this line of code should not be executed in your sketch. Fortunately the mytimer function that this is inside doesn’t seem to be called, and you should keep it that way…

I’m not sure what this is meant to do…

but it doesn’t sound like it will work well with Blynk.

As far as your baud rate issues are concerned, it’s difficult to make any sense of what you’ve written. More information would be needed to explore that issue.

Pete.

Ok Thanks Pete. I will look at the suggestions. So if you can tell me do I call the timer in the setup like I have commented out ?

I have tried to upload a screen grab of what I am seeing. So it was working then with the next upload I get the ESP not connecting. This is happening a lot and I plug into the computer and check AT commands and fine.

Done what you suggested and working a lot smoother. Thank you .

Please copy and paste the text from your serial monitor, and use triple backticks like you do when posting code, rather than posting screenshots.

Pete.

No problem, Can I use a delay at all when using blynk ? In this function I use a delay to define the pump on time, but now that you have said it to me I have an issue where the pump is staying on. Now has me thinking that it does not see the delay

 int SoilMoisture = analogRead (SoilMoisture);//read value of soil moisture
 int WaterLevel = analogRead (WaterLevel); //read value of waterlevel
 int SoilMoistpercent; // moisture value in percentage
 SoilMoistpercent = map(SoilMoisture,SoilWet,SoilDry,100,0); // To display the soil moisture value in percentage
 Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoistpercent);
 Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);

  Serial.print("WaterLevel: ");
  Serial.println(WaterLevel);
  Serial.print("SoilMoisture % is : ");
  Serial.println(SoilMoistpercent);

   if (SoilMoistpercent < 50 && WaterLevel > 350){ 
    digitalWrite (RedLED1,LOW);
    digitalWrite (GreenLED1,HIGH);
    digitalWrite (PUMP,HIGH);
    Blynk.virtualWrite(V7_RED_LED, LOW);
    Blynk.virtualWrite(V3_GREEN_LED, HIGH);
    Blynk.virtualWrite(V8_PUMP, HIGH);
    delay (pumptime); 
    digitalWrite (PUMP,LOW);    
    digitalWrite (RedLED1,LOW);
    digitalWrite (GreenLED1,HIGH);
    Blynk.virtualWrite(V8_PUMP, LOW);
    Blynk.virtualWrite(V3_GREEN_LED, HIGH);
    Blynk.virtualWrite(V7_RED_LED, LOW);
}
 else if (WaterLevel < 340){
    digitalWrite (RedLED1,HIGH); //turn  on the red LED
    Blynk.virtualWrite(V7_RED_LED, HIGH);//Write to app
    digitalWrite (GreenLED1,LOW);//Turn off green led 
    Blynk.virtualWrite(V3_GREEN_LED, LOW);//write to blynk
    digitalWrite (PUMP,LOW); //turn off pump
 }
}```

If you want me to look at your code then you need to post the full sketch, not snippets.

If you want me to look at serial monitor output then you need to post it as text, with triple backticks, rather than screenshots.

Pete.

Sorry, Thanks Pete. I have taken on board your suggestions and I have made changes to the timers and have added in the timer.enable(PUMPTIME); instead of delay having read over the documents but cannot test it yet due to the ESP not responding. Seem when I make a change and upload then I get ESP not responding. Sometimes I can disconnect and wait and it will come back but takes a few tries.
Again thank you for the support. Have included the new updated code

#include <avr/io.h>

#include <DHT.h>

#include <SoftwareSerial.h>

#include <Wire.h>

#define pumptime 2500 //Pump on time

//BLYNK- APP and WEB requierments

#define BLYNK_TEMPLATE_ID "TMPL6Pv17xQ0"//

#define BLYNK_TEMPLATE_NAME "Greenhouse"//project name

#define BLYNK_AUTH_TOKEN "5j3T5Wy-3sapGdJpzz3F-T3AfyN-IKEN"

#define BLYNK_PRINT Serial

//Blynk Libraries

#include <ESP8266_Lib.h>

#include <BlynkSimpleShieldEsp8266.h>

//BlynkTimer timer;

BlynkTimer timer;// enables blynk timer

#define INTERVAL 5000L//5 sec timer for functions(temp and soil moisture)

#define PUMPTIME 2500L // timer for pump on time

// Your WiFi details.

// Set password to "" for open networks.

char ssid[] = "Fridgefi";

char pass[] = "arsenaL2005*";

// For arduino Uno

SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:

#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

// DHT parameters

#define DHTPIN 5

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

// Arduino Pins

#define FAN 10 //enable Fan on pin 10

#define BULB  9 //enable Bulb on pin 9

#define PUMP  8  //enable pump on pump 8

#define RedLED1  7 //Red LED for water level

#define GreenLED1  4 //Green LED for water level

#define SoilMoisture  A1 //Soil Moisture Sensor

#define WaterLevel  A2// WaterLevel Sensor

//Virtual pins FOR BLYNK APP

#define V1_SOIL_MOISTURE V1

#define V2_WATER_LEVEL V2

#define V3_GREEN_LED V3

#define V5_Humidity V5

#define V6_Temperature V6

#define V7_RED_LED V7

#define V8_PUMP V8

#define V9_BULB V9

#define V10_FAN V10

const int SoilDry = 524; //Dry value of soil

const int SoilWet = 254; //Wet value of soil

 void getTemp(){

  // Read DHT11- Inside Greenhouse

int temperature = dht.readTemperature();// read the temp and assign the value to temperature

int humidity = dht.readHumidity();// read the humidity and assign the value to temperature

  Blynk.virtualWrite(V6_Temperature, temperature);

  Blynk.virtualWrite(V5_Humidity, humidity);

  Serial.print("Temp: ");

  Serial.println(temperature);

  Serial.print("Humidity: ");

  Serial.println(humidity);

  // Check if reads fails

  if (isnan(temperature) || isnan(humidity)) {// if not a number on temp or humidity return the error below

    Serial.println(F("Failed to read from DHT sensor"));

    return;

  }

    if (temperature < 22) {

    digitalWrite(FAN, LOW);//Turn off the fan

    digitalWrite(BULB,HIGH);// turn on the bulb

    Blynk.virtualWrite(V9_BULB, HIGH);

    Blynk.virtualWrite(V10_FAN, LOW);

  }

    else if(temperature > 23 ) {

    digitalWrite(FAN, HIGH);//Turn on the fan

    digitalWrite(BULB, LOW);//Turn off the bulb

    Blynk.virtualWrite(V9_BULB, LOW);

    Blynk.virtualWrite(V10_FAN, HIGH);

  }

 }

  void checkMoisture(){

 int SoilMoisture = analogRead (SoilMoisture);//read value of soil moisture

 int WaterLevel = analogRead (WaterLevel); //read value of waterlevel

 int SoilMoistpercent; // moisture value in percentage

 SoilMoistpercent = map(SoilMoisture,SoilWet,SoilDry,100,0); // To display the soil moisture value in percentage

 Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoistpercent);

 Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);

  Serial.print("WaterLevel: ");

  Serial.println(WaterLevel);

  Serial.print("SoilMoisture % is : ");

  Serial.println(SoilMoistpercent);

   if (SoilMoistpercent < 90 && WaterLevel > 350){

    digitalWrite (RedLED1,LOW);

    digitalWrite (GreenLED1,HIGH);

    digitalWrite (PUMP,HIGH);

    Blynk.virtualWrite(V7_RED_LED, LOW);

    Blynk.virtualWrite(V3_GREEN_LED, HIGH);

    Blynk.virtualWrite(V8_PUMP, HIGH);

    timer.enable(PUMPTIME); //timer for pump time, can be changed above

    digitalWrite (PUMP,LOW);    

    digitalWrite (RedLED1,LOW);

    digitalWrite (GreenLED1,HIGH);

    Blynk.virtualWrite(V8_PUMP, LOW);

    Blynk.virtualWrite(V3_GREEN_LED, HIGH);

    Blynk.virtualWrite(V7_RED_LED, LOW);

}

 else if (WaterLevel < 340){

    digitalWrite (RedLED1,HIGH); //turn  on the red LED

    Blynk.virtualWrite(V7_RED_LED, HIGH);//Write to app

    digitalWrite (GreenLED1,LOW);//Turn off green led

    Blynk.virtualWrite(V3_GREEN_LED, LOW);//write to blynk

    digitalWrite (PUMP,LOW); //turn off pump

 }

}

void setup() {

  // Debug console

  Serial.begin(9600);// for Blynk app

  // Set ESP8266 baud rate

 EspSerial.begin(ESP8266_BAUD);

 delay(10);

  // Initialize Temp & Humidity Sensor

dht.begin();

//Blynk WIFI

Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80); //BLYNK app

timer.setInterval(INTERVAL, getTemp);

timer.setInterval(INTERVAL, checkMoisture);

 //Enable pinMode

//OUTPUTS//

pinMode (FAN, OUTPUT); //enable pin 10 as output

digitalWrite(FAN, LOW);//initial low

pinMode (BULB, OUTPUT); //enable pin 9 as output

digitalWrite(BULB, LOW);//initial low

pinMode (PUMP, OUTPUT); //enable pin 8 as output

digitalWrite(PUMP, LOW);//initial low

pinMode (RedLED1,OUTPUT);

digitalWrite(RedLED1, LOW);//initial low

pinMode (GreenLED1,OUTPUT);

digitalWrite(GreenLED1, LOW);//initial low

//INPUTS//

pinMode(SoilMoisture, INPUT); //enable pin A1 as input

pinMode(WaterLevel, INPUT);//enable pin A2 as input

//pinMode()

}

void loop() {

  Blynk.run();//Runs blynk

  timer.run();//Run the Blynk timer

}