sending AT commands via code

Here is a function that takes a character string and a timeout. It displays the command being sent and any characters received during the timeout. You can use it to see if the response you are receiving is what you expect. It also returns a String containing the returned text so you can use the returned text in your code.

SoftwareSerial ATDevice(RXPin, TXPin);
String command(const char *toSend, unsigned long milliseconds) {
  String result;
  Serial.print("Sending: ");
  Serial.println(toSend);
  ATDevice.println(toSend);
  unsigned long startTime = millis();
  Serial.print("Received: ");
  while (millis() - startTime < milliseconds) {
    if (ATDevice.available()) {
      char c = ATDevice.read();
      Serial.write(c);
      result += c;  // append to the result string
    }
  }
Serial.println();  // new line after timeout.
return result;
}