I am doing some work with an ESP8266 WiFi module and am trying to communicate with it via serial from a UNO board. The idea is for the ESP8266 to provide WiFi functionality to my Arduino project. For testing purposes I am using softwareserial and connecting the ESP8266 to standard GPIO pins. Ultimately, once everything is tested and working, the intention is to connect the ESP8266 to the serial hardware pins 0 and 1.
The difficulty is that the ESP8266 might send out several \r+\n in its response, so I can't just use a \r or \n character as a terminator. For example, the response to the AT command is:
\r\nAT\r\n\r\nOK\r\n
The submitted command is always echoed back followed by a \r\n. There may or may not be a blank line before the response is output. The answer I am looking for will be in there somewhere, but it cannot be predicted on which line or how many \r\n sequences precede it.
Furthermore, some commands can take 7 or 8 seconds to respond.
I have tried preceding the parsing code with something like:
while (!esp8266.available()) {
}
esp8266 is defined as a SoftwareSerial object with the GPIO pins I am using.
I had hoped that the process will idle while waiting for some output, but it still proceeds directly to the parser which then fails because there is no output to process yet.
From the code below it will be evident that for the search I am not buffering the incoming characters nor using the \r or \n as a termination character, just reading whatever output is available and parsing character by character "on the fly" as it were.
The parser works just fine once some characters arrive, but how do I implement a "wait until some characters arrive" function so that the sketch will wait before parsing, bearing in mind that the wait time might be several seconds?
bool replyContains(const char* reply) {
// Clear buffer
uint8_t rl = strlen(reply);
uint8_t idx = 0;
uint8_t c = 0;
while (!esp8266.available()) {
}
Serial.print(F("Reply: "));
while (esp8266.available()) {
c = esp8266.read();
Serial.print((char)c);
if ((c == reply[idx]) && (idx < rl)) {
idx++;
}else{
if (idx != rl) idx = 0;
}
}
Serial.println();
if (idx == rl) {Serial.print(F("Found: "));Serial.println(reply);};
if (idx == rl) return true;
return false;
}