Overview of Sim900A
SIM900 enables GPRS connectivity to embedded applications. We can implement TCP Client protocol using SIM900 TCP function AT Commands.
The Transmission Control Protocol (TCP) is standard transport layer internet protocol which used in establishing and maintaining communication in between server and client.
It is widely used in IoT (Internet of Things) embedded applications, where every sensor is connected to a server and we have access to control them over the internet.
The GSM/GPRS module uses USART communication to communicate with microcontroller or PC terminal. AT commands are used to configure the module in different modes and to perform various functions like calling, posting data to a site, etc.
For more information about Sim900A and how to use it, refer the topic Sim900A GSM/GPRS Module in the sensors and modules section.
Connection Diagram of Sim900A GPRS Module with Arduino
Sending data and reading data from a remote server using SIM900A as a TCP Client.
Here, we will be using Thingspeak server for demo purpose.
Thingspeak is an open IOT platform where anyone can visualize and analyse live data from their sensor devices. We can also perform data analysis on data posted by remote devices with Matlab code in Thingspeak.
To learn more about Thingspeak refer link https://thingspeak.com/pages/learn_more.
Just sign up and create a channel. We have created a channel with the ID and write key as given below on Thingspeak for sending and receiving data.
channel ID is = 119922
Write Key is = C7JFHZY54GLCJY38
Note: Do not forget to tick Make Public field in channel setting option on your Thingspeak channel. It makes the channel available to use as public.
For TCP Receive method, use AT commands as shown in the screenshot of RealTerm software given below.
The screenshot consists of AT commands (Green) and Responses (Yellow).
TCP Receive Requests (Green) and Responses (Yellow)
For TCP SEND method, use AT commands as shown in the screenshot of RealTerm software given below.
The screenshot consists of AT commands (Green) and Responses (Yellow).
TCP SEND Requests (Green) and Responses (Yellow)
Note : The receive buffer of Software Serial implementation has a size of 64 bytes. This is not enough to show all the data being received. We need to increase this to a bigger size, 160 bytes for example.
D:\Arduino\arduino-1.8.2\hardware\arduino\avr\libraries\SoftwareSerial\src
Here, we had unzipped the Arduino IDE in our D drive in a folder named Arduino.
If the size is not changed, the response from the GPRS module will be displayed incompletely on the serial monitor.
Note :You need to use the APN of the network provider of the SIM card that you are using.
In the screenshots, APN for Idea has been used (internet). In the codes, APN for Tata Docomo has been used (TATA.DOCOMO.INTERNET).
Word of Caution : The above given sketches are for general understanding of the concepts. They do not use the optimal method for transmitting and receiving commands and responses.
Whenever any AT command is sent, its response should be checked before sending another command.
You can refer TCP Client for ATmega and PIC microcontrollers to know how to send commands, check the responses and then further take appropriate actions.
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
You can see the data read from the Thingspeak server highlighted in the red box in the image shown above. It shows the entry id corresponding to the last entry to the server. It shows just the last entry since 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
You can see the entry id (1127) of the data field sent to the Thingspeak server highlighted in red box in the image shown above. The entry id will increment each time you post some data or if someone else posts the data before you post the data. Here, we are sending null to field1 on the server using GET /update? api_key=C7JFHZY54GLCJY38&field=1. We could also send some other value (30 for example) to the field1 using GET /update?api_key=C7JFHZY54GLCJY38&field1=30. The entry id will not change though. Only the data reflected on the server side will have the value specified.
Updates at Thingspeak server on TCP SEND
For TCP SEND we can see the output at the server end. Here we are using thingspeak server and sending null value at field1 on the server.
The red dot highlighted in Green is for null value sent to the server. The red dot highlighted in Yellow is for a value around 500 sent to the server.
Comentários