I'm starting some physical computing with an Arduino Mega and an ESP-01. Would anyone be able to outline why I cannot send multiple commands to the ESP when I use the following code? I'm sure I'm missing something obvious, or I have a lack of understanding about the hardware.
(I've tried to piece this together from various tutorials around the web - as each tutorial often fails at a particular stage. Any help would be greatly appreciated.)
Arduino Sketch:
// params for the script
int TIMEOUT = 5000;
/**
* Send an AT command to the ESP and print the output to Serial
*/
void sendCommand(String command, String keyword) {
// print command for debug
Serial.print("> sending ");
Serial.println(command);
// send the command to the ESP
Serial1.println(command);
// Print the response until `keyword` or a TIMEOUT
long int time = millis();
int current_char = 0;
while((time + TIMEOUT) > millis()) {
while(Serial1.available()) {
char ch = Serial1.read();
Serial.write(ch);
if (ch == keyword[current_char]) {
if (++current_char == keyword.length()) {
Serial.println();
Serial.println("> End of command response.");
return;
}
}
}
}
// Timeout
Serial.print("> Timeout or error for '");
Serial.print(command);
Serial.println("'");
}
/**
* Setup
*/
void setup () {
Serial.begin(115200);
Serial.println("> Arduino is go");
Serial1.begin(115200);
// check AT
sendCommand("AT", "OK");
// list the available connections
sendCommand("AT+CWLAP", "OK");
}
/**
* Loop
*/
void loop () {
}
Serial Monitor output:
> Arduino is go
> sending AT
AT
OK
> End of command response.
> sending AT+CWLAP
> Timeout or error for 'AT+CWLAP'