Pantech.AI

How to Interface SIM900A GPRS with Arduino UNO using TCP Client

Overview of Sim900A

Sim900A

The SIM900 module enables GPRS connectivity for embedded applications. It supports the implementation of the TCP Client protocol using SIM900’s AT commands.

The Transmission Control Protocol (TCP) is a standard internet transport layer protocol used for establishing and maintaining communication between a server and client. It is widely applied in IoT (Internet of Things) applications, where sensors are connected to a server, allowing remote control and monitoring over the internet.

The GSM/GPRS module communicates with a microcontroller or PC terminal using USART, with AT commands used to configure the module for various functions, such as making calls, posting data to websites, and more.

For more information about the SIM900A GSM/GPRS module and how to use it, refer to the “SIM900A GSM/GPRS Module” topic in the Sensors and Modules section.

For detailed information on the SIM900A and how to use it, refer to the “SIM900A GSM/GPRS Module” topic in the Sensors and Modules section.

Connection Diagram of Sim900A GPRS Module with Arduino

Interfacing Sim900A GPRS Module With Arduino UNO

Sending data and reading data from a remote server using SIM900A as a TCP Client.

Here, we’ll use the ThingSpeak server for demonstration purposes.

ThingSpeak is an open IoT platform that allows users to visualize and analyze live data from sensor devices. It also supports data analysis on data posted by remote devices using MATLAB code. For more information on ThingSpeak, refer to this link.

To get started, sign up on ThingSpeak and create a channel. We’ve created a channel with the following credentials for sending and receiving data:

  • Channel ID: 119922
  • Write Key: C7JFHZY54GLCJY38

Note: Remember to check the “Make Public” option in your channel settings on ThingSpeak. This setting makes the channel publicly accessible.

For the TCP Receive method, use the AT commands shown in the RealTerm software screenshot below, where the AT commands are displayed in green and the responses in yellow.

The screenshot consists of AT commands (Green) and Responses (Yellow).

For the TCP SEND method, use the AT commands displayed in the RealTerm software screenshot below.

In the screenshot, AT commands are shown in green, and responses are shown in yellow.

TCP SEND Requests (Green) and Responses (Yellow)

Note: The receive buffer in the Software Serial implementation has a default size of 64 bytes, which may not be enough to display all received data. To increase this to a larger size, such as 160 bytes, modify the line #define _SS_MAX_RX_BUFF 64 to #define _SS_MAX_RX_BUFF 160 in the SoftwareSerial.h file within the Arduino IDE folder. The file can be found at:

D:\Arduino\arduino-1.8.2\hardware\arduino\avr\libraries\SoftwareSerial\src

In this example, the Arduino IDE was unzipped in the “D” drive in a folder named “Arduino.” If the buffer size is not increased, responses from the GPRS module may appear incomplete on the serial monitor.

Important: You must use the Access Point Name (APN) for the network provider associated with your SIM card.

In the screenshots, the APN for Idea (internet) has been used, whereas in the code, the APN for Tata Docomo (TATA.DOCOMO.INTERNET) is used.

Caution: The provided sketches are intended for a basic understanding of concepts and may not follow the optimal approach for transmitting and receiving commands and responses.

To ensure reliable communication, always check the response for each AT command before sending the next one. For detailed guidance on command transmission, response checking, and further actions, refer to “TCP Client for ATmega and PIC Microcontrollers.”

TCP Receive Code for Arduino Uno

#include <SoftwareSerial.h>
/* Create object named SIM900 of the class SoftwareSerial */
SoftwareSerial SIM900(8, 7);
void setup() {
  SIM900.begin(9600);	/* Define baud rate for software serial communication */
  Serial.begin(9600);	/* Define baud rate for serial communication */
}

void loop() {
  Serial.println("TCP Receive :");
  Serial.print("AT\\r\\n");
  SIM900.println("AT"); /* Check Communication */
  delay(5000);
  ShowSerialData();	/* Print response on the serial monitor */
  delay(5000);
  Serial.print("AT+CIPMODE=0\\r\\n");	
  SIM900.println("AT+CIPMODE=0");	/* Non-Transparent (normal) mode for TCP/IP application */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPMUX=0\\r\\n");
  SIM900.println("AT+CIPMUX=0");	/* Single TCP/IP connection mode */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT=1\\r\\n");
  SIM900.println("AT+CGATT=1");	/* Attach to GPRS Service */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CREG?\\r\\n");
  SIM900.println("AT+CREG?");	/* Network registration status */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT?\\r\\n");
  SIM900.println("AT+CGATT?");	/* Attached to or detached from GPRS service */ 
  delay(5000); 
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CSTT=\"TATA.DOCOMO.INTERNET\",\"\",\"\"\\r\\n");
  SIM900.println("AT+CSTT=\"TATA.DOCOMO.INTERNET\",\"\",\"\"");	/* Start task and set APN */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIICR\\r\\n");
  SIM900.println("AT+CIICR");	/* Bring up wireless connection with GPRS */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIFSR\\r\\n");
  SIM900.println("AT+CIFSR");	/* Get local IP address */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\\r\\n");
  SIM900.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");	/* Start up TCP connection */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSEND\\r\\n");
  SIM900.println("AT+CIPSEND");	/* Send data through TCP connection */
  delay(2000);
  ShowSerialData();
  delay(2000);
  Serial.print("GET /channels/119922/feeds/last.txt\\r\\n");
  SIM900.print("GET /channels/119922/feeds/last.txt\r\n\x1A");	/* URL for data to be read from */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSHUT\\r\\n");
  SIM900.println("AT+CIPSHUT");	/* Deactivate GPRS PDP content */
  delay(5000);
  ShowSerialData();
  delay(5000);
}

void ShowSerialData()
{
  while(SIM900.available()!=0)	/* If data is available on serial port */
  Serial.write(char (SIM900.read()));	/* Print character received on to the serial monitor */
}

Serial Monitor Output Window

In the image above, the data read from the ThingSpeak server is highlighted within the red box. It displays the entry ID of the most recent data entry on the server, as we requested only the last entry.

TCP Send Code for Arduino Uno

#include <SoftwareSerial.h>
/* Create object named SIM900 of the class SoftwareSerial */
SoftwareSerial SIM900(8, 7);
void setup() {
  SIM900.begin(9600);	/* Define baud rate for software serial communication */
  Serial.begin(9600);	/* Define baud rate for serial communication */
}

void loop() {
  Serial.println("TCP Send :");
  Serial.print("AT\\r\\n");
  SIM900.println("AT"); /* Check Communication */
  delay(5000);
  ShowSerialData();	/* Print response on the serial monitor */
  delay(5000);
  Serial.print("AT+CIPMODE=0\\r\\n");
  SIM900.println("AT+CIPMODE=0");	/* Non-Transparent (normal) mode for TCP/IP application */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPMUX=0\\r\\n");
  SIM900.println("AT+CIPMUX=0");	/* Single TCP/IP connection mode */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT=1\\r\\n");
  SIM900.println("AT+CGATT=1");	/* Attach to GPRS Service */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CREG?\\r\\n");
  SIM900.println("AT+CREG?");	/* Network registration status */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT?\\r\\n");
  SIM900.println("AT+CGATT?");	/* Attached to or detached from GPRS service */ 
  delay(5000); 
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CSTT=\"TATA.DOCOMO.INTERNET\",\"\",\"\"\\r\\n");
  SIM900.println("AT+CSTT=\"TATA.DOCOMO.INTERNET\",\"\",\"\"");	/* Start task and set APN */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIICR\\r\\n");
  SIM900.println("AT+CIICR");	/* Bring up wireless connection with GPRS */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIFSR\\r\\n");
  SIM900.println("AT+CIFSR");	/* Get local IP address */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\\r\\n");
  SIM900.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");	/* Start up TCP connection */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSEND\\r\\n");
  SIM900.println("AT+CIPSEND");	/* Send data through TCP connection */
  delay(2000);
  ShowSerialData();
  delay(2000);
  Serial.print("GET /update?api_key=C7JFHZY54GLCJY38&field1=1\\r\\n");
  SIM900.print("GET /update?api_key=C7JFHZY54GLCJY38&field1=1\r\n\x1A");	/* URL for data to be sent to */
  delay(10000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSHUT\\r\\n");
  SIM900.println("AT+CIPSHUT");	/* Deactivate GPRS PDP content */
  delay(5000);
  ShowSerialData();
  delay(5000);
}

void ShowSerialData()
{
  while(SIM900.available()!=0)	/* If data is available on serial port */
  Serial.write(char (SIM900.read()));	/* Print character received on to the serial monitor */
}

Serial Monitor Output Window

In the image above, you can see the entry ID (1127) of the data field sent to the ThingSpeak server, highlighted within the red box. The entry ID will increment with each new data post, or if another user posts data before you. In this example, we’re sending a null value to field1 on the server using GET /update?api_key=C7JFHZY54GLCJY38&field1. Alternatively, we could send a specific value, such as 30, to field1 using GET /update?api_key=C7JFHZY54GLCJY38&field1=30. While the data displayed on the server would reflect the updated value, the entry ID would remain consistent.

Updating the ThingSpeak Server with TCP SEND

For TCP SEND, the output can be observed on the server side. Here, we’re using the ThingSpeak server and transmitting a null value to field1.

Data on Thingspeak Server

The red dot highlighted in green represents a null value sent to the server, while the red dot highlighted in yellow indicates a value of around 500 sent to the server.

Leave a Comment

Your email address will not be published. Required fields are marked *