user13557: I'm looking for a way to wait for a response from an AT command to return OK or CONNECTED or whatever the command returns when it's true.
I can check if the serial is available, but that means I have to manually guess what time between commands to give them time to connect or start.
Function:
void ModemCommand(String command) {
  //Send command to modem
  mySerial.println(command);
  Serial.println(command);
  delay(100);
  while (!mySerial.available()) {
    //Wait for Response from modem
    Serial.println(F("No response yet"));
    delay(1000);
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read());
    //Print response from modem
  }
  Serial.println("");
  //Linefeed after reponse
  delay(10);
}

Code example:
ModemCommand(F("ATE0"));
//Turn on echo
ModemCommand(F("AT+CFUN=1"));
//Turn modem functions on
waitSec(8,F("turning on Modem functions"));
ModemCommand(F("AT+CGREG?"));
//GSM attached?
ModemCommand(F("AT+CSQ"));
//Signal Quality
ModemCommand(F("AT+CSTT=\"internet\""));
//Set APN
ModemCommand(F("AT+CIICR"));
//Connect to APN GPRS
waitSec(8,F("connecting to APN GPRS"));
ModemCommand(F("AT+CIFSR"));
//Get IP

Does anybody know how to do this?

user13940: You would need to create a buffer which will populate over each loop so you dont block the main loop while waiting the all string.
When you get the string or your buffer is full, just check whatever is the response, and based on that you can make your decision.

user11763: The most you can do is define a certain amount of time between received characters, after which you can decide that there's nothing more to be read from the modem. Of course, the time taken for processing is longer for certain commands (and some provide no response at all, until you've completed entering their payload, like TCPSEND, in which case you should simply use mySerial.write() to send said payload, and not your function), but once a response is ready and its first character is available, a timeout of 1 second between characters should be sufficient, for any reasonable baud rate. Something like this:
#define TIMEOUT 1000

void modem_command(String command){
  mySerial.println(command);
  Serial.println(command);
  while (mySerial.available() == 0);  // wait for first char

  unsigned long lastRead = millis();   // last time a char was available
  while (millis() - lastRead < TIMEOUT){   
    while (mySerial.available()){
      Serial.write(mySerial.read());
      lastRead = millis();   // update the lastRead timestamp
    }
  }
  // No need for extra line feed since most responses contain them anyways
}

Of course, you can always alter TIMEOUT based on your findings.

13557: <p>I'm looking for a way to wait for a response from an AT command to return OK or CONNECTED or whatever the command returns when it's true.</p>

<p>I can check if the serial is available, but that means I have to manually guess what time between commands to give them time to connect or start.</p>

<p>Function:</p>

<pre><code>void ModemCommand(String command) {
  //Send command to modem
  mySerial.println(command);
  Serial.println(command);
  delay(100);
  while (!mySerial.available()) {
    //Wait for Response from modem
    Serial.println(F("No response yet"));
    delay(1000);
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read());
    //Print response from modem
  }
  Serial.println("");
  //Linefeed after reponse
  delay(10);
}
</code></pre>

<p>Code example:</p>

<pre><code>ModemCommand(F("ATE0"));
//Turn on echo
ModemCommand(F("AT+CFUN=1"));
//Turn modem functions on
waitSec(8,F("turning on Modem functions"));
ModemCommand(F("AT+CGREG?"));
//GSM attached?
ModemCommand(F("AT+CSQ"));
//Signal Quality
ModemCommand(F("AT+CSTT=\"internet\""));
//Set APN
ModemCommand(F("AT+CIICR"));
//Connect to APN GPRS
waitSec(8,F("connecting to APN GPRS"));
ModemCommand(F("AT+CIFSR"));
//Get IP
</code></pre>

<p>Does anybody know how to do this?</p>
 13940: <p>You would need to create a buffer which will populate over each loop so you dont block the main loop while waiting the all string.</p>

<p>When you get the string or your buffer is full, just check whatever is the response, and based on that you can make your decision.</p>
 11763: <p>The most you can do is define a certain amount of time between received characters, after which you can decide that there's nothing more to be read from the modem. Of course, the time taken for processing is longer for certain commands (and some provide no response at all, until you've completed entering their payload, like TCPSEND, in which case you should simply use <code>mySerial.write()</code> to send said payload, and not your function), but once a response is ready and its first character is available, a timeout of 1 second between characters should be sufficient, for any reasonable baud rate. Something like this:</p>

<pre><code>#define TIMEOUT 1000

void modem_command(String command){
  mySerial.println(command);
  Serial.println(command);
  while (mySerial.available() == 0);  // wait for first char

  unsigned long lastRead = millis();   // last time a char was available
  while (millis() - lastRead &lt; TIMEOUT){   
    while (mySerial.available()){
      Serial.write(mySerial.read());
      lastRead = millis();   // update the lastRead timestamp
    }
  }
  // No need for extra line feed since most responses contain them anyways
}
</code></pre>

<p>Of course, you can always alter <code>TIMEOUT</code> based on your findings.</p>
