This is one of those projects I always wanted to do. How do you use modem AT commands and make a TCP connection using a GPRS board? After struggling for a while and reading lot of documents from various places, I was able to do this. There are many blogs on the net covering this topic, but most of them either do not provide full details, or in many places specific answers have been provided to queries but not full details. I’m not claiming that this is the only blog that has the full details, just that I was not able to find a good one.
What do you need
To start this project, you will need the following:
- A GSM-GPRS board. I bought one from sunrom.com. This board can be interfaced with RS-232 DB9 connector. The rest of the required items assume that the board you have, also interface using a RS-232 connector
- A laptop with spare USB port (I’m assuming your laptop does not have RS-232 port. I have not seen a laptop with RS-232 port in over last 10 years)
- A USB to RS-232 board. Please note, this should not be a USB-to-RS232 (TTL), but a full USB-to-RS232. The output should be RS232 voltage ranges (around +/-12V). This is due to the fact that the board I had, allowed interfacing with RS-232. If your board has TTL interfaces, please get a US-to-RS232 (TTL) interface. The one I have was bought from Nex-Robotics
- A RS-232 cable (please ensure you get the right combination of male/female). My USB-to-RS232 adapter had male output and GPRS board had female input. Thus, I got one RS232 cable with male/female combination.
- A SIM card that has data plan
- A power supply for your GPRS board. Generally, the GPRS boards require 2 Amp supply. So please read the board specifications and get the power supply accordingly. I made the mistake of not reading the specs right and got a 1 Amp supply. I had trouble exchanging data.
- A terminal program like Terminal.exe. You can download this from the Sunrom website. Look at the bottom right of this page.
How to setup
- First of all you need to install the driver and setup your USB-to-RS232 adapter. This is dependent upon the adapter you have. Please contact your seller for the installation driver and details.
- Once you install, there should be a virtual COM port available on your machine. I have a windows laptop and it shows under “Devices and Printers”.
- Next, insert the SIM card in your GPRS board and power on. These boards generally come with some sort of indicator LEDs that will tell you when the registration is successful. The one I have, has a green LED that blinks at 3 sec period to indicate the board is ready. So again, read the board specification.
- Connect the RS232 cable between the adapter and the board.
- Bring up the terminal.exe. It should show available COM ports. Select the COM port that represents your USB-to-RS232 adapter. Now comes the tricky part. You need to know what is the Baud rate. After many attempts and multiple blog site visits, I found that perhaps (I’m still not sure) 19200 is the default setting for Sim 900. I have been using that and it works like a charm.
In Terminal.exe tool, you have a text box at the bottom. You enter AT commands in that text box. Please ensure, you have the checkbox labeled “+CR” (to the right of the textbox) checked. Every AT command needs to be terminated by \r\n (CR + LF) combination. You enter the command and click the “Send” button. The response is visible in the area above the textbox.
The command sequence
Now comes the interesting part. Given below is a list of AT commands that you must fire in the exact given sequence to connect to a webserver using TCP over GPRS. The symbol => indicates the command you need to enter, <= indicates the response received from the SIM900. Text within /** **/ are my comments for further explanation. The response may contains additional information than what is shown here. I have listed down only the important part of the response that you need to be aware of.
Also, please note, send the commands in uppercase only.
Here is the sequence:
/** First test if everything is okay **/
<= AT /** This should come back. SIM900 default is to echo back commands you enter **/
<= OK /** This string should tell you all is well**/
=>AT+CPIN? /**This is to check if SIM is unlocked. This sample assumes unlocked SIMs**/
<= +CPIN: READY /** If your response contains this, then it means SIM is unlocked and ready**/
=>AT+CREG? /**This checks if SIM is registered or not**/
<=+CREG: 0,1 /**This string in the response indicates SIM is registered**/
=>AT+CGATT? /**Check if GPRS is attached or not**/
<=+CGATT: 1 /**A response containing this string indicates GPRS is attached**/
=>AT+CIPSHUT /**Reset the IP session if any**/
<=SHUT OK /**This string in the response represents all IP sessions shutdown.**/
=>AT+CIPSTATUS /**Check if the IP stack is initialized**/
<=STATE: IP INITIAL /**This string in the response indicates IP stack is initialized**/
=>AT+CIPMUX=0 /**To keep things simple, I’m setting up a single connection mode**/
<=OK /**This string indicates single connection mode set successfully at SIM 900**/
=>AT+CSTT= “APN”, “UNAME”, “PWD” /**Start the task, based on the SIM card you are using, you need to know the APN, username and password for your service provider**/
<= OK /**This response indicates task started successfully**/
=> AT+CIICR /**Now bring up the wireless. Please note, the response to this might take some time**/
<=OK /**This text in response string indicates wireless is up**/
=>AT+CIFSR /**Get the local IP address. Some people say that this step is not required, but if I do not issue this, it was not working for my case. So I made this mandatory, no harm.**/
<= xxx.xxx.xxx.xxx /**If previous command is successful, you should see an IP address in the response**/
=>AT+CIPSTART= “TCP” , “www.vishnusharma.com”, “80” /**Start the connection, TCP, domain name, port**/
<= CONNECT OK /**This string in the response indicates TCP connection established**/
=>AT+CIPSEND /**Request initiation of data sending (the request)**/
<= > /**The response should be the string “>” to indicate, type your data to send**/
=> xxxxxx /**Just type anything for now**/
=>#026 /**Now type the sequence #026. This tells the terminal.exe to send the hex code 0x1a (which is Ctrl+Z) to indicate end of data sending**/
<= xxxxxxxxxx /**You should get some response back from the server…it would generally be a complain that the request string was not valid…but that is a different subject…you have established the connection**/
/**To close the connection**/
=>AT+CIPSHUT /**Request shutting down of the current connections**/
<=SHUT OK /**Indicates shutdown successful**/
You can get many errors during this exercise and I cannot possibly think of all of them. A few key ones are given below:
- You get junk characters – It can either be due to baud rate mismatch or because of lose connections. Initially, I used hookup wires to connect the two DB9 connectors but I got junk. I replaced the wires with a DB9 cable and the problem was gone.
- You get an error when you issue the command – Either your command sequence is wrong, or you have entered an invalid command. Sometimes, you need to power cycle your SIM900 board and start again.