-->
Page 1 of 1

Arudino Mega - sending multiple AT commands to ESP-01

PostPosted: Tue Jan 17, 2017 5:06 am
by CTollemache
Hello!

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:
Code: Select all// 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:
Code: Select all> Arduino is go
> sending AT
AT


OK
> End of command response.
> sending AT+CWLAP

> Timeout or error for 'AT+CWLAP'

Re: Arudino Mega - sending multiple AT commands to ESP-01

PostPosted: Wed Jan 18, 2017 6:03 am
by CTollemache
The issue is I don't understand serial communication, and I'm an lacking understanding of C (as well as being a total noob to hardware stuff like buffers, memory, etc). I've been reading through these resources:

https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/
http://forum.arduino.cc/index.php?topic=288234.0

I'll post a working program once I have it, and hopefully an explanation, as it could help someone out in the future.