Connecting Arduino to GPRS/GSM Quadband Module (SIM900)
Contents
- The shield (hardware)
- Using GSM/GPRS module with AT commands
- Powering the board
- Using the shield in standalone mode - Calls
- Using the shield in standalone mode - FTP
- Using the shield in standalone mode - TCP and UDP
- Using the shield in standalone mode - HTTP
Components:
1 x Arduino
1 x GPRS/GSM Quadband Module for Arduino (SIM900)
1 x GPRS antenna
1 x SIM card
1 x PC
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 1:
GPRS Shield diagram version 2:
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 |
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 |
All AT commands: SIM900 AT Command Manual.pdf
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 ;
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
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.
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.
int8_t answer; int onModulePin = 2; int button = 12; char aux_str[30]; char phone_number[]="*********"; // ********* is the number to call void setup(){ pinMode(onModulePin, OUTPUT); pinMode(button, INPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code 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); // press the button for hang the call while(digitalRead(button)==1); Serial.println("ATH"); // disconnects the existing call } void loop(){ } void power_on(){ uint8_t answer=0; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ if(Serial.available() != 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer response[x] = Serial.read(); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < timeout)); return answer; }
To make a lost call next code is used.
int8_t answer; int onModulePin = 2; int seconds = 20; char aux_str[30]; char phone_number[]="*********"; // ********* is the number to call void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code 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"); // disconnects the existing call } void loop(){ } void power_on(){ uint8_t answer=0; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < timeout)); return answer; }
Command summary | |
Command | 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. |
Using the shield in standalone mode - FTP
Creating a file into the FTP server, writing it and reading it.
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; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ if(Serial.available() != 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer response[x] = Serial.read(); //Serial.print(response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { answer = 1; } } } // Waits for the asnwer with time out 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } // check if the desired answer 2 is in the response of the module if (strstr(response, expected_answer2) != NULL) { answer = 2; } } // Waits for the asnwer with time out }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. |
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.
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); // sets the PIN code 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(){ // Selects Single-connection mode if (sendATcommand2("AT+CIPMUX=0", "OK", "ERROR", 1000) == 1) { // Waits for status IP INITIAL while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0 ); delay(5000); // Sets the APN, user name and password if (sendATcommand2("AT+CSTT=\"APN\",\"user_name\",\"password\"", "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0 ); delay(5000); // Brings Up Wireless Connection if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1) { // Waits for status IP GPRSACT while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0 ); delay(5000); // Gets Local IP Address if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1) { // Waits for status IP STATUS while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0 ); delay(5000); Serial.println("Openning TCP"); // Opens a TCP socket if (sendATcommand2("AT+CIPSTART=\"TCP\",\"IP_address\",\"port\"", "CONNECT OK", "CONNECT FAIL", 30000) == 1) { Serial.println("Connected"); // Sends some data to the TCP socket sprintf(aux_str,"AT+CIPSEND=%d", strlen(ip_data)); if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1) { sendATcommand2(ip_data, "SEND OK", "ERROR", 10000); } // Closes the socket 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; // checks if the module is started answer = sendATcommand2("AT", "OK", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } // check if the desired answer 2 is in the response of the module else if (strstr(response, expected_answer2) != NULL) { answer = 2; } } } // Waits for the asnwer with time out 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.
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); // sets the PIN code 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(){ // Selects Multi-connection mode if (sendATcommand2("AT+CIPMUX=1", "OK", "ERROR", 1000) == 1) { // Waits for status IP INITIAL while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0 ); delay(5000); // Sets the APN, user name and password if (sendATcommand2("AT+CSTT=\"APN\",\"user_name\",\"password\"", "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0 ); delay(5000); // Brings Up Wireless Connection if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1) { // Waits for status IP GPRSACT while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0 ); delay(5000); // Gets Local IP Address if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1) { // Waits for status IP STATUS while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0 ); delay(5000); Serial.println("Openning TCP"); // Opens a TCP socket with connection 1 if (sendATcommand2("AT+CIPSTART=1,\"TCP\",\"IP_address\",\"port\"", "CONNECT OK", "CONNECT FAIL", 30000) == 1) { Serial.println("Connected"); // Sends some data to the TCP socket 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); } // Closes the socket 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; // checks if the module is started answer = sendATcommand2("AT", "OK", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } // check if the desired answer 2 is in the response of the module else if (strstr(response, expected_answer2) != NULL) { answer = 2; } } } // Waits for the asnwer with time out 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 |
Using the shield in standalone mode - HTTP
SIM900 can launch a HTTP operation like GET or POST. Here is an example with GET operation:
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); // sets the PIN code 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(){ // Initializes HTTP service answer = sendATcommand("AT+HTTPINIT", "OK", 10000); if (answer == 1) { // Sets CID parameter answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000); if (answer == 1) { // Sets url answer = sendATcommand("AT+HTTPPARA=\"URL\",\"www.url.com\"", "OK", 5000); if (answer == 1) { // Starts GET action 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; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } } // Waits for the asnwer with time out } 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); // Initialize the string delay(100); while( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do{ if(Serial.available() != 0){ response[x] = Serial.read(); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } // check if the desired answer 2 is in the response of the module if (strstr(response, expected_answer2) != NULL) { answer = 2; } } // Waits for the asnwer with time out }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. |
More information
- Connect Arduino to the Mobile Network
- Connecting Arduinos together
- Connecting Arduino to Rasberry Pi
- Connecting Arduino to the Internet - How to add a webserver, HTTP client, etc
- Connecting Arduino to Android via USB cable
- Connecting Arduino to PC serial port
- Connecting Arduino to Windows and Linux through an USB cable
- Connecting Arduino to GSM GPS shield
- Connecting Arduino to GPRS/GSM Quadband Module (SIM900)