Tuesday, July 2, 2013

SIM300 GSM/GPRS AT commands for client-server connection

Although SIM300 GSM/GPRS modems are out-dated now, with the coming of its successors, I thought it is a good idea to post the AT commands for GPRS access. AT commands are string commands used to communicate with gsm/gprs modems.If you have a modem, that can be interfaced with PC through COM port, then you will be able to test these commands using some terminal softwares (Hyperterminal in Windows Xp, Putty for Windows 7, Minicom in linux, …). SIM 300 has an inbuilt TCP/IP stack, which will be helpful when it is connected to a microcontroller for gprs data transfer.

Here is the sequence of AT commands for various purposes. All commands should be followed by Carriage return(\r) and Line feed(\n) at the end (if you are using terminal in PC, you can simply hit ENTER). If the commands are correct, the modem will return OK otherwise ERROR.

To send an SMS:
AT+CMGF=1                //This command select the text message format  
AT+CMGS=”<mob num>”      //Hit ENTER and the modem will return a prompt (>), then type the message and press Ctrl-Z (0x0A in hex)  
Here is an example,
AT+CMGS=”123456789”
>how are you?
Press Ctrl-Z.
Modem won’t send message until Ctrl-Z is pressed.

To receive an SMS:
AT+CNMI=2,2,0,0,0     //Setting this command will set the modem to receive text messages instantly on reception.You can give different parameters by referring to the datasheet.
Here,with these parameters, it is stated that the modem buffers unsolicited result codes (including text messages) in the TA when TA-TE link is reserved (e.g. in on-line data mode) and flush them to the TE after reservation. Otherwise forward them directly to the TE. (TA is the modem and TE is PC or microcontroller). The message will be received starting with +CMT.

Configuring as Client:
Note:If one of the below commands (those changing status of modem) fails, then you
have to issue AT+CIPSHUT and start from the beginning.
Before configuring the modem as client, we should check the status of modem.
AT+CIPSTATUS     //Query current connection status
This command returns one of the following responses.
IP INITIAL           //This should be the initial status
IP START             //After issuing AT+CSTT command
IP CONFIG            //After issuing AT+CIICR command
IP IND               //After issuing AT+CIICR, if the module accepts the command.
IP GPRSACT           //After issuing AT+CIICR, if activated successfully
IP STATUS            //After issuing AT+CIFSR command
TCP/UDP CONNECTING   //After AT+CIPSTART command, modem is trying to connect
IP CLOSE             //After AT+CIPCLOSE, when the current client-server 
                      connection closed.
CONNECT OK           //After AT+CIPSTART, when a connection is established.
CONNECT FAIL         //After AT+CIPSTART, when a connection fails.
Start configuring on the status of IP INITIAL.
AT+CGATT=1             //Attaching to GPRS service
AT+CIPCSGP=1,”<apn>”    //Selecting GPRS mode and providing access point name. For example, <apn> for Airtel is airtelgprs.com or airtelgprs.
AT+CSTT                 //Starts the task. After this command, status should change to IP START.
AT+CREG?                //Query network registration status (optional). If the returned value is 1 or 5, sim card is registered home/roaming network. For all other values, you cannot proceed.
AT+CIICR                //On the status of IP START. It will bring up a GPRS connection. This command will takes a seconds/minutes to return OK/ERROR.
AT+CIFSR                //Returns the IP address of the modem.
AT+CDNSORIP=1           //0 if server IP address and 1 if server domain name in AT+CIPSTART command.
AT+CIPSPRT=1            //Set prompt ‘>’ and SEND OK when data is sent successfully.
AT+CIPHEAD=1            //Add an IP header while receiving data from server (Received data starts with +IPD) (optional)
Connecting to server:
After configuring as client, you will be getting the status as IP STATUS and so proceed to connect to remote server.
AT+CIPSTART=”TCP”,”www.google.com",”80”    //On status of IP STATUS. This command connects to the server. Returns CONNECT OK on success.
AT+CIPSEND=<len><Hit ENTER><type data after prompt>    //To send known length of data
    OR
AT+CIPSEND<Hit ENTER>    //To send unknown length of data.
><type data after prompt><press Ctrl-Z to send>
Example:
AT+CIPSEND        //wait for module to return'>' to indicate it's ready to receive data 
GET / HTTP/1.1    //This an HTTP request for the default page 
Host: "www.google.com"
Connection: Keep-Alive
Accept: */*
Accept-Language: en-us
<Hit ENTER>
<press Ctrl-Z to send>
AT+CIPCLOSE    //Closes the current client server connection
AT+CIPSHUT    //Closes entire GPRS context and returns to initial state.
Configuring as Server:
Proceed with the same steps for Configuring as client. After getting the status as IP STATUS, you have to issue the following commands.
AT+CLPORT="TCP",<portnum>    //Set port
AT+CIPSERVER=1    //Returns SERVER OK if running successfully
Now, you can access this server port from anywhere. You may try it using Telnet for raw data transfer.
Some useful commands:
Here are some of the basic commands which may help you in diagnosis of modem.
AT+CSQ    //Returns signal strength and bit error rate. Maximum of signal strength is 31, which means you have a very good coverage.
ATE0     //Turns Echo OFF
AT+CUSD=1,”<ussd code>”    //Such as to check balance
AT+CLTS    //To get local time stamp
AT+CCLK?    //Query current date and time.
Keep in mind that SIM300 acts as a server or a client at a time, it cannot be configured as both client and server simultaneously. I hope this is it. Now you can try it. And thank you for reading this.