AT command responses (understanding order of code execution on Arduino)
<p>I'm sending AT commands to an <a href="https://en.wikipedia.org/wiki/ESP8266" rel="nofollow noreferrer">ESP8266</a> from an <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow noreferrer">Arduino Uno</a>/Nano (ATmega328) and attempting to parse the end of the strings received in response to establish how the ESP reacted and whether it was successful (and whether it's ready to receive another command yet). I'm aware that parsing AT command responses has been discussed before here:</p>
<p><em><a href="https://stackoverflow.com/questions/36884514/get-at-command-response">Get AT command response</a></em></p>
<p>But I have a specific issue not covered there that might also be of interest to other people on here...</p>
<p>First, a function is called which sends the AT command to the ESP to connect to ThingSpeak (datalogging server). This works fine in manual mode and also connects when trying to parse the response, <em>but</em> it only parses the first line that comes back. For example, the expected output in the serial monitor would be:</p>
<pre class="lang-none prettyprint-override"><code>c
AT+CIPSTART=&quot;TCP&quot;,&quot;api.thingspeak.com&quot;,80
CONNECT

OK
Connected to ThingSpeak!
</code></pre>
<p>Where <code>c</code> is just the command character I type to initiate the connection.</p>
<p>The actual response, however, is as follows:</p>
<pre class="lang-none prettyprint-override"><code>c
AT+CIPSTART=&quot;TCP&quot;,&quot;api.thingspeak.com&quot;,80
Cannot connect to ThingSpeak!


CONNECT

OK
</code></pre>
<p>This means that the parsing function is ending before it receives the response... As shown in the code below, there is a 10 second timeout currently specified. Even with a 20 second timeout, the same thing happens, despite the fact that when executed manually, the response arrives in around one second.</p>
<p>Just to test the parsing function, I tried searching for <code>&quot;80&quot;</code> and it returned true as this is found at the end of the first line of the response. Whether it searches for <code>&quot;OK&quot;</code> or <code>&quot;OK\r\n&quot;</code> the result is the same, it returns false and THEN the rest of the response is received.</p>
<p>Here's the code:</p>
<pre><code>boolean waitForResponse(String target, unsigned long timeout)
{
  unsigned long startTime = millis();
  String responseBuffer;
  char charIn;

  // Keep checking for ESP response until timeout expires
  while ((millis() - startTime) &lt; timeout)
  {
    if (ESP.available())
    {
      responseBuffer += ESP.read();
    }
  }
  Serial.println(responseBuffer);

  if (responseBuffer.endsWith(target))
  {
    return true;
  } else {
    return false;
  }
}

void openCxn()
{
  ESP.print(&quot;AT+CIPSTART=\&quot;TCP\&quot;,\&quot;api.thingspeak.com\&quot;,80&quot;);
  delay(500);
  if (waitForResponse(&quot;80&quot;, 10000L))
  {
    Serial.println(&quot;Connected to ThingSpeak!&quot;);
  } else {
    Serial.println(&quot;Cannot connect to ThingSpeak!&quot;);
  }
}
</code></pre>
<p>Why does it return before the full response is received (well within the timeout period)? Is it something to do with the <code>endsWith()</code> function?</p>
<p>Consequently, do you have any ideas of how to make it parse the entire response instead of just the first line?</p>
<p>To reiterate, I am only interested in the end of the response (e.g. <code>&quot;OK&quot;</code> or <code>&quot;OK\r\n&quot;</code>).</p>