Sending AT commands and receiving response

Hey all,

I just figured out how to send AT commands to the Arduino GSM Shield and get back the response. I’ve been struggling with this for a while so I thought to share it with you here on the forum.

This method works but is probably not the best way.

#include <GSM.h>

// initialize the library instance
GPRS gprsAccess;  // GPRS access
GSM gsmAccess(true);    // GSM access: include a 'true' parameter for debug enabled
GSMClient client;  // Client service for TCP connection

// you need modemAccess to access the function writeModemCommand
// that function allows you to specify a delay for getting the answer from the gsm module
GSM3ShieldV1DirectModemProvider modemAccess;

// PIN Number
#define PINNUMBER "xxxx"

char answer[100];

void setup()
{
  Serial.begin(9600);
  
  boolean notConnected = true;
  
  Serial.println("Connecting to the GSM network");
  
  while(notConnected){
    if(gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else {
      Serial.println("Not connected, trying again");
      delay(1000);
    }
  }
  
  Serial.println("Connected.");

}

void loop()
{
  
  Serial.println("Sending AT Command.");

  Serial.println(modemAccess.writeModemCommand("AT+QNITZ=1",1000));
  delay(1000);
  Serial.println(modemAccess.writeModemCommand("AT+QNITZ?",1000));
  delay(1000);
  Serial.println(modemAccess.writeModemCommand("AT+CCLK?",1000));
  
  while(true);
}

Here is a sketch where you can send AT commands from the terminal. Include a new line after each command:

#include <GSM.h>

// initialize the library instance
GSM gsmAccess;
GPRS gprsAccess;

GSM3ShieldV1DirectModemProvider modemAccess;

// PIN Number
#define PINNUMBER "9731"

// APN info
char apn[] = {"web.pro.be"};
char login[] = {""};
char password[] = {""};

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete


void setup(){
  Serial.begin(9600);
  connectGSM();
}
void loop(){
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.print("Sending command: "+inputString);
    Serial.println(send_AT_Command(inputString,1000)); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
String send_AT_Command(String AT_COMMAND, int tDelay){
  /*Serial.print("COMMAND: "+AT_COMMAND);
  Serial.println(modemAccess.writeModemCommand(AT_COMMAND,tDelay));
  Serial.println("-----------------------------------------------");*/
  
  return modemAccess.writeModemCommand(AT_COMMAND,tDelay);
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}
void connectGSM(){
  boolean notConnected=true;
  Serial.println("Connecting...");
  while(notConnected){
    if(gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else delay(1000);
  }
  Serial.println("Connected to GSM, now GPRS...");
 
  // 1000ms bug
  delay(1000);
  
  while(gprsAccess.attachGPRS(apn, login, password)!=GPRS_READY){
    delay(1000);
  }
  Serial.println("Fully connected.");
}

Sir, how about in sending MMS or SMS AT commands. I am not getting any response in serial monitor like "OK" or "Error" or "CONNECT". I dont know if there is an error or none. Please help me.

what commands and which shield are you using?

Im using GSM/GPRS shield sir.

AT+CMMSINIT OK AT+CMMSCURL="192.40.100.20/MMSC" OK AT+CMMSCID=1 OK AT+CMMSPROTO="10.124.26.94",8799 OK AT+SAPBR=3,1,"CONTYPE","GPRS" OK AT+SAPBR=3,1,"APN","TATA.DOCOMO.MMS" OK AT+SAPBR=1,1 OK AT+SAPBR=2,1 OK AT+CMMSEDIT=1 OK AT+CMMSDOWN="TEXT",3,5000000 CONNECT OK

What functions am i going to add in order to get responses like "OK" in serial monitor ?

Dear Mathias, how can I parse the modem reply? Have you tried to read its answers?

Hi

can you help me?

I use your first code to get the time from the Network.
But the time does not change.

You have any ideia why? I try diffrents networks and they support this.