How To Execute GSM AT Commands (SOLVED)

Hi,

I am using an Arduino Uno with the official GSM shield. I am trying to execute the AT commands instead of using the GSM library.

The below codes I got it from one of the post in the forum here as a reference,
http://forum.arduino.cc/index.php?topic=219677.0

#include <SoftwareSerial.h>

SoftwareSerial gsmSerial(2, 3); //Rx Tx
char inChar = 0;

//RESET the modem
void reset() {
  Serial.println("Start Reset");
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  delay(12000);
  digitalWrite(7, LOW);
  delay(1000);
  Serial.println("End Reset");  
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setting up!");
  reset();
  gsmSerial.begin(9600);
  Serial.println("Enter your AT commands (with CR & NL)..");
}

void loop() {
  
  if(gsmSerial.available()) {
    delay(1);
    inChar = gsmSerial.read();
    if((inChar >= ' ') && (inChar<='z'))
      Serial.print(inChar);
    else {
      Serial.print(inChar);
      if(inChar == 10)
        Serial.println();
    }
  } 
  
  if(Serial.available()) {
    inChar = Serial.read();
    gsmSerial.print(inChar);
  }
}

Ok, so when I enter "AT", I get the "OK" reply.

When I enter "ATI", I also get the product revision number.

However, when I call any of the "AT+XXXX" commands, I get this kind of reply,

AT+
+CME ERROR: 100

So, I thought it maybe because of this

if(Serial.available()) {
    inChar = Serial.read();
    gsmSerial.print(inChar);
  }

So, I modified the code above so that I store it in a String first before executing it. But it did not work. Then, I modified the codes as shown below:

#include <SoftwareSerial.h>
#include <string.h>

SoftwareSerial gsmSerial(2, 3); //Rx Tx
char inChar = 0;

//RESET the modem
void reset() {
  Serial.println("Start Reset");
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  delay(12000);
  digitalWrite(7, LOW);
  delay(1000);
  Serial.println("End Reset");  
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setting up!");
  reset();
  gsmSerial.begin(9600);
  Serial.println("Enter your AT commands (with CR & NL)..");
  
  //gsmSerial.write("AT\r\n"); //work
  //delay(1);
  //gsmSerial.write("ATI\r\n"); //product revision
  //delay(1);
  gsmSerial.write("AT+GSN\r\n"); //no reply at all!!
  //delay(1);
  //gsmSerial.flush();
  Serial.println("after command");
}

void loop() {
  
  if(gsmSerial.available()) {
    delay(1);
    inChar = gsmSerial.read();
    if((inChar >= ' ') && (inChar<='z'))
      Serial.print(inChar);
    else {
      Serial.print(inChar);
      if(inChar == 10)
        Serial.println();
    }
  } 
  
  if(Serial.available()) {
    inChar = Serial.read();
    gsmSerial.print(inChar);
  }
}

So, yeah, as you can see, I sent this gsmSerial.write("AT+GSN\r\n"); but I do not get any reply at all.

I am trying to execute the commands below and see what are the replies.

/*
 * AT+GSN - get IMEI, 100 error
 * AT+CGSN - product serial number, 100 error
 * AT+CPAS - Mobile equipment activity statys
 * AT+CMGS - Send SMS
 * AT+CGATT - Attach GPRS
 * AT+CGREG - Network registration status
 * AT+CGSMS - Select server for MO SMS messages
 * AT+QPOWD - Power off
 * AT+QSIMSTAT - SIM inserted status reporting
 * AT+QSIMDET - Switch on or off detecting SIM card
 * AT+QGID - Get SIM card group identifier
 * AT+QGPCLASS - Change GPRS multi-slot class
 * AT+QMGHEX - Enable to send non-ascii characters SMS
 * AT+QSMSCODE - Configure SMS code mode
 * AT+QSCANF - Scan power of GSM frequency
 * AT+QINISTAT - Query state of initialization
 * AT+QNSTATUS - Query GSM network status
 
 *  Commands for TCIPIP application toolkit
 * AT+QIOPEN - Startup TCP or UDP
 * AT+QISEND - Send data through TCP / UDP
 * 
 */

Kindly advice me on what might I have been missing.

Thank you in advice. :slight_smile:

AT command to call is ATD (from AT Dial, I think). Following to ATD you must write the phone number that you want to call (without spaces, comas, plus sign, etc., only the number).

Some commands, only are available after the module initializes (and it takes a few seconds). I don't know if is the case, but you can wait a while before you send the commands. I think there is a command to see if the modules is ready.

@ luisilva

Yes. To know whether it is ready or not. Just call "AT". It will tell you something like "call ready" after "ok".

Do you have a SIM card in the module?
Do have unblock the PIN from your SIM card?

@ luisilva

Yes, there is SIM card in the module. There is no PIN on the SIM card, and I can send SMS / get IMEI using the GSM library.

That's interesting, because according to the documentation there isn't a CME error 100. But that's not to say I've got the most up to date version.

However, I've loaded Serial Relay onto my Uno R3, I'm using a UK O2 sim card (Telefonica):

AT+GSN

861064025276147

OK
AT+CGSN

861064025276147

OK
AT+CPAS

+CPAS: 0

OK

(Numbers changed to protect the innocent!)

@dannable

Thanks for confirming that the codes work well on the UNO. Yes It is really driving me crazy. haha.

I just called this in my code, and it worksssss!!!!!!!!!

gsmSerial.write("AT+GSN\r\n");
delay(1);

That code is not working in ARDUINO mega 2560.The problem iam facing is when i enter "AT" in the serial monitor i dont get any reply its blank,so do you want me to change something in the code,so that i would get the desired result.

You haven't replied to the question I asked you in your thread on this subject.

I am using sim808 and arduino uno by uart connection
I got error every time when i send gps or gsm or gprs related at commands , i got results when i send "AT", "AT+IPR=9600", "AT+ECHARGE=1","AT&W","AT+CPOWD=1","AT+CBC","AT+CSQ", ETC.

and when i connect first time with mircocontroller, i does not get the results like below.

RDY
+CFUN: 1
GPS Ready
+CPIN: READY
Call Ready
SMS Ready

also not get gps location when i send AT+CGPSINF=0

what should i do?

visualxl:
I just called this in my code, and it worksssss!!!!!!!!!

gsmSerial.write("AT+GSN\r\n");

delay(1);

The threat died from long time ago, anyway, I had similar problem this week. So, just to add piece of information:
The command worked for you, when you ran it from code (SoftwareSerial), not sent via Serial. I made me looking for root cause. The reason probably was in communication speed. When my Serial was slowed down, everything started working. I set 9600, didn't try with other figure. But, the quoted message led me to the solution. Thanks!