Tutorial Index
3
Ingredients:
Difficulty: Medium -
Preparation Time: 45 minutes
NOTE: The codes of the tutorial have developed to work on Arduino IDE v1.0.1
Go to IndexStep 1: The shield (hardware)
The board (shield) we are going to use in this tutorial is the GPRS/GSM Quadband Module for Arduino (SIM900) from Cooking hacks.
The GPRS shield is fully compatible with old Arduino USB versions, Duemilanove and Mega.
GPRS Shield diagram version 2:
NOTE: The Arduino/Raspberry Pi jumper MUST be in Arduino position. The Raspberry Pi position should be used only if the shield is connected to a Raspberry Pi.
A wrong position of this jumper can damage the GPRS shield.
GPRS Shield diagram version 1:
The LED of the shield shows the status of the GPRS+GPS module. The table below shows the meaning of the blink of the LED.
Status | SIM908 behavior |
Off |
SIM908 is not running |
64ms On/ 800ms Off |
SIM908 not registered the network |
64ms On/ 3000ms Off |
SIM908 registered to the network |
SIM908 registered to the network |
PPP GPRS communication is established |
Go to IndexStep 2: Using GSM/GPRS module with AT commands
Important issues:
- Use capital letters for AT commands.
- Send CR (Carriage return) and LF (Line feed) after the AT command.
- Place the serial communication jumpers in the right position.
- Use an external power supply and place the power jumpers in the right position. If the shield is powered from the Arduino, the power jumper must be in Arduino 5V position. It the shield is powered from the Vin input (in the shield), the power jumper must be in Vext position.
The first thing we are going to do with the module is to connect the module to a PC directly (using an Arduino as gateway) and check the basic AT commands. In this case, serial communication jumpers have to be set on USB gateway position.
Remember take out the ATmega microcontroller from the Arduino gateway.
Basic configuration:
Connect the shield to the Arduino gateway.
Then connect the USB cable and the SIM card.
Finally plug the USB cable to the computer and open a serial port terminal to communicate via the usb port (e.g: hyperterminal (win), cutecom / gtkterm (linux)).
If you use the Arduino IDE serial monitor for sending AT commands – Be sure that you are sending CR (Carriage return) and LF (Line Feed).
Set the baudrate to 115200 bps and open the serial port, then press the ON button for two seconds. Then if you type AT you'll get OK , this means that the communication with the module is working fine. Now, with the module working you can check some AT commands to control the module, the basic commands are:
Important type commands in capital letters and with CR (carriage return) and LF (line feed)!!!
Command |
Response |
Description |
AT |
OK |
If you get OK, the communication with the module is working |
AT+CPIN="****" |
OK |
If the SIM card is locked with PIN (**** is the pin number) |
AT+COPS? |
|
Operator information |
NOTE: Factory baudrate setting is auto-bauding by default. Baudrate can be fixed using the command AT+IPR=baudrate . Allowed baudrates: 0 (Auto-bauding) , 1200 , 2400 , 4800 , 9600 , 19200 , 38400 , 57600 and 115200 ;
All the AT commands here
NOTE: With some sketches, the buffer of the UART may be small, to increase the length of the buffer you need to change these lines in the file HardwareSerial.cpp (/arduino-1.0.X/hardware/arduino/cores/arduino):
#if (RAMEND < 1000)
#define SERIAL_BUFFER_SIZE 32
#else
#define SERIAL_BUFFER_SIZE 256
#endif
by
#if (RAMEND < 1000)
#define SERIAL_BUFFER_SIZE 192
#else
#define SERIAL_BUFFER_SIZE 256
#endif
Go to IndexStep 3: Powering the board:
Some of the USB ports on computers are not able to give all the current the module needs to work, if your module goes down when it tries to connect to the network, you can use an external power supply (12V - 2A) on the Arduino.
Remember set the Arduino power jumper to EXT!!! (if you use Diecimila or older).
How to set the power jumper in the shield?.
If you want the shield takes power from Arduino => Set the jumper to Arduino 5V possition
If you want the shield takes power from an external supply => Set the jumper to V ext. possition
For powering the shield from external supply , you have to use V in ext. connector (Vin + GND).
If you use a power supply with output smaller than 2 A, you should add an extra capacitor for the power.
For example, a 220 uF electrolytic capacitor between 3.3V and GND.
Go to IndexStep 4: Using the shield in standalone mode - Calls
Originating and receiving voice calls
The code example and the connection diagram shown below are used to originate a voice call and, pushing a button, end that voice call. The button is connected between digital pin 12 an ground. A 10kΩ pull-up resistor is needed at this pin.
Hide Code
int8_t answer;
int onModulePin = 2;
int button = 12;
char aux_str[30];
char phone_number[]="*********";
void setup(){
pinMode(onModulePin, OUTPUT);
pinMode(button, INPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
sprintf(aux_str, "ATD%s;", phone_number);
sendATcommand(aux_str, "OK", 10000);
while(digitalRead(button)==1);
Serial.println("ATH");
}
void loop(){
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
To make a lost call next code is used.
Show Code
int8_t answer;
int onModulePin = 2;
int seconds = 20;
char aux_str[30];
char phone_number[]="*********";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
sprintf(aux_str, "ATD%s;", phone_number);
sendATcommand(aux_str, "OK", 10000);
delay(seconds * 1000);
Serial.println("ATH");
}
void loop(){
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
To receive calls the used code are this and the connection diagram is the same that the used to originate calls. Don't forget the pull-up resistor on pin 12.
Command summary
Command | Response | Description |
ATD*********; |
|
********* is the number to call. |
ATA |
OK |
Answer an incoming call. |
ATH |
OK |
Cancel voice calls. |
Go to IndexStep 5: Using the shield in standalone mode - Sending and receiving SMS
The first code is used to send a SMS, the second one reads the first SMS into the memory.
Show Code
int8_t answer;
int onModulePin= 2;
char aux_string[30];
char phone_number[]="*********";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
Serial.print("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000);
Serial.println("Sending SMS");
sprintf(aux_string,"AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand(aux_string, ">", 2000);
if (answer == 1)
{
Serial.println("Test-Arduino-Hello World");
Serial.write(0x1A);
answer = sendATcommand("", "OK", 20000);
if (answer == 1)
{
Serial.print("Sent ");
}
else
{
Serial.print("error ");
}
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
void loop(){
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Show Code
int8_t answer;
int x;
int onModulePin= 2;
char SMS[200];
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000);
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000);
answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);
if (answer == 1)
{
answer = 0;
while(Serial.available() == 0);
do{
if(Serial.available() > 0){
SMS[x] = Serial.read();
x++;
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}
}
}while(answer == 0);
SMS[x] = '\0';
Serial.print(SMS);
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
void loop(){
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Command summary
Command | Response | Description |
AT+CMGF= |
OK |
Specifies the input and output format of the short messages. 0 for PDU mode and 1 for text mode. |
AT+CMGS |
|
Sends a message. |
AT+CMGR=* |
|
Reads a message. * is the number of the message. |
Go to IndexStep 6: Using the shield in standalone mode - FTP
Creating a file into the FTP server, writing it and reading it.
Show Code
int8_t answer;
int onModulePin = 2;
char aux_str[30];
char incoming_data[120];
char test_str[ ]= "0000000011111111222222223333333344444444555555556666666677777777000000001111111122222222333333334444";
int data_size, aux;
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(5000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500)
|| sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
configure_FTP();
uploadFTP();
downloadFTP();
Serial.print("Incoming data: ");
Serial.println(incoming_data);
}
void loop(){
}
void configure_FTP(){
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"APN\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"USER\",\"user_name\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"PWD\",\"password\"", "OK", 2000);
while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) != 1);
sendATcommand("AT+FTPCID=1", "OK", 2000);
sendATcommand("AT+FTPSERV=\"ftp.yourserver.com\"", "OK", 2000);
sendATcommand("AT+FTPPORT=21", "OK", 2000);
sendATcommand("AT+FTPUN=\"user_name\"", "OK", 2000);
sendATcommand("AT+FTPPW=\"password\"", "OK", 2000);
}
void uploadFTP(){
sendATcommand("AT+FTPPUTNAME=\"file_name\"", "OK", 2000);
sendATcommand("AT+FTPPUTPATH=\"/path\"", "OK", 2000);
if (sendATcommand("AT+FTPPUT=1", "+FTPPUT:1,1,", 30000) == 1)
{
data_size = 0;
while(Serial.available()==0);
aux = Serial.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Serial.available()==0);
aux = Serial.read();
}
while(aux != 0x0D);
if (data_size >= 100)
{
if (sendATcommand("AT+FTPPUT=2,100", "+FTPPUT:2,100", 30000) == 1)
{
Serial.println(sendATcommand(test_str, "+FTPPUT:1,1", 30000), DEC);
Serial.println(sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", 30000), DEC);
Serial.println("Upload done!!");
}
else
{
sendATcommand("AT+FTPPUT=2,0", "OK", 30000);
}
}
else
{
sendATcommand("AT+FTPPUT=2,0", "OK", 30000);
}
}
else
{
Serial.println("Error openning the FTP session");
}
}
void downloadFTP(){
int x = 0;
sendATcommand("AT+FTPGETNAME=\"file_name\"", "OK", 2000);
sendATcommand("AT+FTPGETPATH=\"/path\"", "OK", 2000);
if (sendATcommand("AT+FTPGET=1 ", "+FTPGET:1,1", 30000) == 1)
{
do{
if (sendATcommand2("AT+FTPGET=2,50", "+FTPGET:2,", "+FTPGET:1,", 30000) == 1)
{
data_size = 0;
while(Serial.available()==0);
aux = Serial.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Serial.available()==0);
aux = Serial.read();
}while(aux != 0x0D);
Serial.print("Data received: ");
Serial.println(data_size);
if (data_size > 0)
{
while(Serial.available() < data_size);
Serial.read();
for (int y = 0; y < data_size; y++)
{
incoming_data[x] = Serial.read();
x++;
}
incoming_data[x] = '\0';
}
else
{
Serial.println("Download finished");
}
}
else if (answer == 2)
{
Serial.println("Error from FTP");
}
else
{
Serial.println("Error getting the file");
data_size = 0;
}
}while (data_size > 0);
}
else
{
Serial.println("Error openning the FTP session");
}
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Command summary
AT command | Response | Description |
AT+SAPBR |
OK |
Configures GPRS profile. |
AT+FTPCID=1 |
OK |
Selects profile 1 for FTP. |
AT+FTPSERV=”****” |
OK |
Sets FTP server domain name or IP address. **** is the domain name or the IP. |
AT+FTPPORT=*** |
OK |
Sets FTP server port. *** is the port. |
AT+FTPUN=”***” |
OK |
Sets user name for FTP server access. *** is the user name. |
AT+FTPPW=”***” |
OK |
Sets password for FTP server access. *** is the password. |
AT+FTPPUTNAME="****" |
OK |
Sets destiny name for the file.*** is the name of the file. |
AT+FTPPUTPATH="****" |
OK |
Sets destiny file path. *** is the path of the file. |
AT+FTPPUT |
OK |
Use to put a file into the FTP server. |
AT+FTPGETNAME="****" |
OK |
Sets origin name for the file.*** is the name of the file. |
AT+FTPGETPATH="****" |
OK |
Sets origin file path. *** is the path of the file. |
AT+FTPGET |
|
Use to get a file into the FTP server. |
Go to IndexStep 7: Using the shield in standalone mode - TCP and UDP
Single client
Sending data to a TCP server first, and then it sends to a UDP server.
Show Code
int8_t answer;
int onModulePin= 2;
char aux_str[50];
char ip_data[40]="Test string from GPRS shield\r\n";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand2("AT+CPIN=****", "OK", "ERROR", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)== 0 );
}
void loop(){
if (sendATcommand2("AT+CIPMUX=0", "OK", "ERROR", 1000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CSTT=\"APN\",\"user_name\",\"password\"", "OK", "ERROR", 30000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0 );
delay(5000);
Serial.println("Openning TCP");
if (sendATcommand2("AT+CIPSTART=\"TCP\",\"IP_address\",\"port\"",
"CONNECT OK", "CONNECT FAIL", 30000) == 1)
{
Serial.println("Connected");
sprintf(aux_str,"AT+CIPSEND=%d", strlen(ip_data));
if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
{
sendATcommand2(ip_data, "SEND OK", "ERROR", 10000);
}
sendATcommand2("AT+CIPCLOSE", "CLOSE OK", "ERROR", 10000);
}
else
{
Serial.println("Error openning the connection");
}
}
else
{
Serial.println("Error getting the IP address");
}
}
else
{
Serial.println("Error bring up wireless connection");
}
}
else
{
Serial.println("Error setting the APN");
}
}
else
{
Serial.println("Error setting the single connection");
}
sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
delay(10000);
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand2("AT", "OK", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand2("AT", "OK", "OK", 2000);
}
}
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Multiple client
SIM900 allows to use 8 connections simultaneously. Here is the example code with a UDP and TCP connections.
Show Code
int8_t answer;
int onModulePin= 2;
char aux_str[50];
char ip_data[40]="Test string from GPRS shield\r\n";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand2("AT+CPIN=****", "OK", "ERROR", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000) == 0 );
}
void loop(){
if (sendATcommand2("AT+CIPMUX=1", "OK", "ERROR", 1000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CSTT=\"APN\",\"user_name\",\"password\"", "OK", "ERROR", 30000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0 );
delay(5000);
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
{
while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0 );
delay(5000);
Serial.println("Openning TCP");
if (sendATcommand2("AT+CIPSTART=1,\"TCP\",\"IP_address\",\"port\"",
"CONNECT OK", "CONNECT FAIL", 30000) == 1)
{
Serial.println("Connected");
sprintf(aux_str,"AT+CIPSEND=1,%d", strlen(ip_data));
if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
{
delay(500);
sendATcommand2(ip_data, "SEND OK", "ERROR", 10000);
}
sendATcommand2("AT+CIPCLOSE=1", "CLOSE OK", "ERROR", 10000);
}
else
{
Serial.println("Error openning the connection 1");
}
}
else
{
Serial.println("Error getting the IP address");
}
}
else
{
Serial.println("Error bring up wireless connection");
}
}
else
{
Serial.println("Error setting the APN");
}
}
else
{
Serial.println("Error setting the multi-connection");
}
sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
delay(10000);
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand2("AT", "OK", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand2("AT", "OK", "OK", 2000);
}
}
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Command summary
AT command | Response | Description |
AT+CIPMUX= |
OK |
Selects single connection (0) or multiple connection (1) |
AT+CSTT="myAPN" |
OK |
Sets APN |
AT+CIICR |
|
Brings up wireless connection |
AT+CIFSR |
|
Get local IP address |
AT+CIPSTART |
|
Establishes a connection with a server. |
AT+CIPSEND |
|
Sends data when the a connection is established. |
AT+CIPCLOSE |
|
Closes the connection |
Go to IndexStep 8: Using the shield in standalone mode - HTTP
SIM900 can launch a HTTP operation like GET or POST. Here is an example with GET operation:
Show Code
int8_t answer;
int onModulePin= 2;
char data[512];
int data_size;
char aux_str[100];
char aux;
int x = 0;
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"APN\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"USER\",\"user_name\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"PWD\",\"password\"", "OK", 2000);
while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
{
delay(5000);
}
}
void loop(){
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
answer = sendATcommand("AT+HTTPPARA=\"URL\",\"www.url.com\"", "OK", 5000);
if (answer == 1)
{
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
if (answer == 1)
{
x=0;
do{
sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
{
data_size = 0;
while(Serial.available()==0);
aux = Serial.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Serial.available()==0);
aux = Serial.read();
}while(aux != 0x0D);
Serial.print("Data received: ");
Serial.println(data_size);
if (data_size > 0)
{
while(Serial.available() < data_size);
Serial.read();
for (int y = 0; y < data_size; y++)
{
data[x] = Serial.read();
x++;
}
data[x] = '\0';
}
else
{
Serial.println("Download finished");
}
}
else if (answer == 2)
{
Serial.println("Error from HTTP");
}
else
{
Serial.println("Error getting the url");
data_size = 0;
}
sendATcommand("", "+HTTPACTION:0,200", 20000);
}while (data_size > 0);
Serial.print("Data received: ");
Serial.println(data);
}
else
{
Serial.println("Error getting the url");
}
}
else
{
Serial.println("Error setting the url");
}
}
else
{
Serial.println("Error setting the CID");
}
}
else
{
Serial.println("Error initializating");
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
delay(5000);
}
void power_on(){
uint8_t answer=0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
while(answer == 0){
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while( Serial.available() > 0) Serial.read();
Serial.println(ATcommand);
x = 0;
previous = millis();
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Command summary
AT command | Response | Description |
AT+SAPBR |
OK |
Configures GPRS profile |
AT+HTTPINIT |
OK |
Initializes HTTP service |
AT+HTTPPARA |
OK |
Configures HTTP parameters |
AT+HTTPACTION=0 |
OK |
Sets HTTP Method Action , GET in this chase. |
AT+HTTPREAD |
|
Reads HTTP data |
AT+HTTPTERM |
OK |
Closes the opened HTTP session. |
Go to IndexLinks and Documentation