I'm sending AT commands to an ESP8266 from an Arduino Uno/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:
Get AT command response
But I have a specific issue not covered there that might also be of interest to other people on here...
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, but it only parses the first line that comes back. For example, the expected output in the serial monitor would be:
c
AT+CIPSTART="TCP","api.thingspeak.com",80
CONNECT
OK
Connected to ThingSpeak!
Where c
is just the command character I type to initiate the connection.
The actual response, however, is as follows:
c
AT+CIPSTART="TCP","api.thingspeak.com",80
Cannot connect to ThingSpeak!
CONNECT
OK
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.
Just to test the parsing function, I tried searching for "80"
and it returned true as this is found at the end of the first line of the response. Whether it searches for "OK"
or "OK
"
the result is the same, it returns false and THEN the rest of the response is received.
Here's the 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) < timeout)
{
if (ESP.available())
{
responseBuffer += ESP.read();
}
}
Serial.println(responseBuffer);
if (responseBuffer.endsWith(target))
{
return true;
} else {
return false;
}
}
void openCxn()
{
ESP.print("AT+CIPSTART="TCP","api.thingspeak.com",80");
delay(500);
if (waitForResponse("80", 10000L))
{
Serial.println("Connected to ThingSpeak!");
} else {
Serial.println("Cannot connect to ThingSpeak!");
}
}
Why does it return before the full response is received (well within the timeout period)? Is it something to do with the endsWith()
function?
Consequently, do you have any ideas of how to make it parse the entire response instead of just the first line?
To reiterate, I am only interested in the end of the response (e.g. "OK"
or "OK
"
).
See Question&Answers more detail:
os