Reading ESP serial AT responses with Arduino
Posted: Sat Oct 17, 2015 5:20 am
Hi All
When using an ESP as a WiFi module issuing AT commands over serial, what is the best way to read the ESP response back with an Arduino?
I’ve seen various ways of reading from serial such as this which simply echo's the response out to the serial monitor.
I also need to be able to interpret the response and create conditions. They way I've been doing this so far is with Serial.find i.e:
The issues I’m coming across are:
1) Sometimes delays are needed between issuing the command and waiting for a response.
.a) These delays sometimes need to be different lengths per command i.e AT+CIPMODE is instant but AT+CWJAP_DEF might take a few seconds.
.b) These delays sometimes vary for the same command i.e during very busy network traffic AT+CIPSTART or AT+CIPSEND might take longer than usual.
.c) Can’t make them too short or you miss the response, but they can't be too long either.
2) Longer responses that have multiple lines are much more difficult to read for a simple print let alone use with Serial.find. The following is an example I’ve been using with the delay needed within the loop, this delay is problematic and causes random characters to be missed if it's not exact. i.e
Can anyone offer any suggestions on better ways I can achieve this? I need to be able to read the response for error checking and other conditions.
Thanks
When using an ESP as a WiFi module issuing AT commands over serial, what is the best way to read the ESP response back with an Arduino?
I’ve seen various ways of reading from serial such as this which simply echo's the response out to the serial monitor.
Code: Select all
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
I also need to be able to interpret the response and create conditions. They way I've been doing this so far is with Serial.find i.e:
Code: Select all
Serial.print(F("\nPing Router......."));
Serial1.println(F("AT+PING=\"192.168.0.1\""));
delay(500);
if (Serial1.find("OK")) {
Serial.print(F("OK"));
} else {
Serial.print(F("Error"));
}
The issues I’m coming across are:
1) Sometimes delays are needed between issuing the command and waiting for a response.
.a) These delays sometimes need to be different lengths per command i.e AT+CIPMODE is instant but AT+CWJAP_DEF might take a few seconds.
.b) These delays sometimes vary for the same command i.e during very busy network traffic AT+CIPSTART or AT+CIPSEND might take longer than usual.
.c) Can’t make them too short or you miss the response, but they can't be too long either.
2) Longer responses that have multiple lines are much more difficult to read for a simple print let alone use with Serial.find. The following is an example I’ve been using with the delay needed within the loop, this delay is problematic and causes random characters to be missed if it's not exact. i.e
Code: Select all
Serial1.println(F("AT+CIFSR"));
while (Serial1.available()) {
delayMicroseconds(460);
char inByte = Serial1.read();
Serial.write(inByte);
}
Can anyone offer any suggestions on better ways I can achieve this? I need to be able to read the response for error checking and other conditions.
Thanks