ESP8266 automatic check connectivity every 1min

Hi
i am try to write the code for esp8266 to check connectivity every min and to re connect if it was not connected.i send the commend AT+CWJAP? to check connectivity.
the problem is when i look for the word "ERROR" is dose not pick up.
eg of the code

if (timer0 > interval) {
    timer0 -= interval; //reset the timer
    esp8266.println("AT+CWJAP?");
    wait_for_esp_response(1000); 
    if(esp8266.find("ERROR")){
     
    setupWiFi();
    }
   
   
   
  }

You are using blocking code, that means while the code you wrote is executed, your microcontroller does nothing else (or whatever wait_for_esp_response(1000) does). Even worse, during find(), it will again wait for a second and in the meantime discard anything that is not "ERROR", especially connection attempts (if you are in server mode).

Not too long ago, I got a very similar answer for my code. Robin2 kindly recommended this post, which was very helpful.
After that, I rewrote my code and found it worked much better. I don't claim to be an expert or that the following is especially good code, but most ESP8266 examples out there are terrible, so I dare to paste mine. You should find some helpful snippets in there. Also, I am glad about any comments, of course:

(Is is a controller for a garage door that switched a relay and finds the current status of the door with an ultrasonic distance sensor)
It does not have a connectivity check like you plan to implement yet, but examples for non blocking serial input and an implementation of a find-like function that outputs the actual data into debug (checkForOK), if blocking still is an option for you. That might give you the answer why "ERROR" is not found.

A link to the library would be necessary.

What DOES the response contain?

code example is

// comment this part out if not using LCD debug
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
#define BUFFER_SIZE 512

#define SSID "route-Factory"
#define PASS "r0b0t2014"
#define PORT  "80"           // using port 8080 by default
#include <SoftwareSerial.h>
#include <elapsedMillis.h>
elapsedMillis timer0;
#define interval 60000
char buffer[BUFFER_SIZE];

/*
// If using Software Serial for debug
// Use the definitions below
//#include <SoftwareSerial.h>
//SoftwareSerial dbg(7,8);  // use pins 7, 8 for software serial 
//#define esp Serial
*/

// If your MCU has dual USARTs (e.g. ATmega644)
// Use the definitions below
#define dbg Serial    // use Serial for debug
SoftwareSerial esp8266(10,11);

// By default we are looking for OK\r\n
char OKrn[] = "OK\r\n";
byte wait_for_esp_response(int timeout, char* term=OKrn) {
  unsigned long t=millis();
  bool found=false;
  int i=0;
  int len=strlen(term);
  // wait for at most timeout milliseconds
  // or if OK\r\n is found
  while(millis()<t+timeout) {
    if(esp8266.available()) {
      buffer[i++]=esp8266.read();
      if(i>=len) {
        if(strncmp(buffer+i-len, term, len)==0) {
          found=true;
          break;
        }
      }
    }
  }
  buffer[i]=0;
  dbg.print(buffer);
  return found;
}

void setup() {
emon1.current(5, 60);             // Current: input pin, calibration.
timer0 = 0;
  // assume esp8266 operates at 115200 baud rate
  // change if necessary to match your modules' baud rate
  esp8266.begin(9600);
  
  dbg.begin(9600);
  dbg.println("begin.");
    
  setupWiFi();

  // print device IP address
  dbg.print("device ip addr:");
  esp8266.println("AT+CIFSR");
  wait_for_esp_response(1000);

}

bool read_till_eol() {
  static int i=0;
  if(esp8266.available()) {
    buffer[i++]=esp8266.read();
    if(i==BUFFER_SIZE)  i=0;
    if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {
      buffer[i]=0;
      i=0;
      dbg.print(buffer);
      return true;
    }
  }
  return false;
}

void loop() {
  int ch_id, packet_len;
  char *pb;  
  if(read_till_eol()) {
    if(strncmp(buffer, "+IPD,", 5)==0) {
      // request: +IPD,ch,len:data
      sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);
      if (packet_len > 0) {
        // read serial until packet_len character received
        // start from :
        pb = buffer+5;
        while(*pb!=':') pb++;
        pb++;
        if (strncmp(pb, "GET /", 5) == 0) {
          wait_for_esp_response(1000);
          dbg.println("-> serve homepage");
          serve_homepage(ch_id);
        }
      }
    }
  }
  if (timer0 > interval) {
    timer0 -= interval; //reset the timer
    esp8266.println("AT+CWJAP?");
    wait_for_esp_response(1000);  
    if(esp8266.find("ERROR\r\n")){
     
    setupWiFi();
    }
   
    
    
  }
}

void serve_homepage(int ch_id) {
  String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\nRefresh: 5\r\n";

  String content="";
 
double Irms = emon1.calcIrms(1480);  // Calculate Irms only
   if ( Irms < 4.30) {
   content += Irms;
    content += "trigger on"; 
  
  }
  
  else if ( Irms>4.30 && Irms<4.85 ) {
   content += Irms;
    content += "Welder 43 on"; 
    
  }
  else if (Irms > 4.85) {
    content += Irms;
    content += "Welder 43 off"; 
   
  }
  // output the value of each analog input pin
  

  header += "Content-Length:";
  header += (int)(content.length());
  header += "\r\n\r\n";
  esp8266.print("AT+CIPSEND=");
  esp8266.print(ch_id);
  esp8266.print(",");
  esp8266.println(header.length()+content.length());
  if(wait_for_esp_response(2000, "> ")) {
    esp8266.print(header);
    esp8266.print(content);
  } else {
    esp8266.print("AT+CIPCLOSE=");
    esp8266.println(ch_id);
  }
}


void setupWiFi() {
  // try empty AT command
  esp8266.println("AT");
  wait_for_esp_response(1000);

  // set mode 1 (client)
  esp8266.println("AT+CWMODE=1");
  wait_for_esp_response(1000);  

  // reset WiFi module
  esp8266.print("AT+RST\r\n");
  wait_for_esp_response(1500);
  delay(3000);
 
  // join AP
  esp8266.print("AT+CWJAP=\"");
  esp8266.print(SSID);
  esp8266.print("\",\"");
  esp8266.print(PASS);
  esp8266.println("\"");
  // this may take a while, so wait for 5 seconds
  wait_for_esp_response(5000);
  
  esp8266.println("AT+CIPSTO=30");  
  wait_for_esp_response(1000);

  // start server
  esp8266.println("AT+CIPMUX=1");
  wait_for_esp_response(1000);
  
  esp8266.print("AT+CIPSERVER=1,"); // turn on TCP service
  esp8266.println(PORT);
  wait_for_esp_response(1000);
  
    
}

Then the answer seems pretty easy: While you are looking for OK in wait_for_esp_response, you "eat" the input that you are later expecting in the find() function. After that, find just waits a second, discards anything that is coming in and then returns false.

But again, you are in server mode, you probably won't want to block the serial input for a second.
A solution could be to parse the input similar to the example I posted. Then set a timestamp into a global variable and interpret "ERROR" or "OK" during the next second in the appropriate way.

hi thanks for reply
i did the following and it start working.do think its fine??

if (timer0 > interval) {
    timer0 -= interval; //reset the timer
    esp8266.println("AT+CWJAP?");
    delay(100); 
    if(esp8266.find("ERROR\r\n")){
     
    setupWiFi();
    }
     
  }

I am not sure what your timer is supposed to do.

Regarding the rest

  1. You can drop the delay(100). find() essentially means "Monitor serial port until ERROR is found, then return true. Timeout after 1 second with return false" So you don't need to wait until ERROR is actually fully received.
  2. You are still blocking, so your microcontroller does not do anything until ERROR is received or 1 second is over. This is probably fine in the case of an error, because your microcontroller might not have anything senseful to do in that time. In case everything is ok, however, you will block your system for 1 second and discard any new connection attempt.

So what you should at least do is scanning for ERROR and OK, preceeding if either is received. Look at your wait_for_esp_response or my checkForOK for an idea how to do that.
BTW, I found the debug output that is in my checkForOK really really helpful when working with the ESP8266.