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.
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.
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.
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.