Preparation Time: 45 minutes
NOTE: If you are looking for a complete solution to use 3G, GPRS and A-GPS, you can use our 3G/GPRS shield for Arduino and Raspberry Pi (3G + GPRS) or our Kit with Audio/Video
NOTE: The Arduino codes of the tutorial have developed to work on Arduino IDE v1.0.1
The board (shield) we are going to use in this tutorial is the GPRS/GSM Quadband Module for Arduino and Raspberry Pi (SIM900) from Cooking hacks.
The GPRS shield is fully compatible with old Arduino USB versions, Duemilanove and Mega.
GPRS Shield diagram version 1:
The LED of the shield shows the status of the GPRS/GSM module. The table below shows the meaning of the blink of the LED.
Status | SIM900 behavior |
---|---|
Off | SIM900 is not running |
64ms On/ 800ms Off | SIM900 not registered the network |
64ms On/ 3000ms Off | SIM900 registered to the network |
SIM900 registered to the network | PPP GPRS communication is established |
Important issues:
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
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:
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.h in /arduino-1.0.X/hardware/arduino/avr/cores/arduino/ (or in file HardwareSerial.cpp in /arduino-1.0.X/hardware/arduino/cores/arduino/ depending on the Arduino IDE version)
#if (RAMEND < 1000) #define SERIAL_TX_BUFFER_SIZE 16 #define SERIAL_RX_BUFFER_SIZE 16 #else #define SERIAL_TX_BUFFER_SIZE 64 #define SERIAL_RX_BUFFER_SIZE 64 #endif #endif
to
#if (RAMEND < 1000) #define SERIAL_TX_BUFFER_SIZE 400 #define SERIAL_RX_BUFFER_SIZE 400 #else #define SERIAL_TX_BUFFER_SIZE 400 #define SERIAL_RX_BUFFER_SIZE 400 #endif #endif
The first thing we are going to do with the module is to check the basic AT commands in gateway mode. Using gateway mode, commands can be sent from our Raspberry Pi directly to any serial module. To stablish UART connection with the module you can use several programs as cutecom in Raspbian graphical environment or minicom in a terminal window in order to use it via SSH . For using minicom follow these steps:
Place the Arduino/RPI jumper in the SIM900 shield to the right, in RPI position.
Place the two Serial com. jumpers in the SIM900 shield to the left, in Arduino position. These jumpers always have to be in this position, in gateway mode and when you run codes.
We recommend to power externally the shield, but it's not necessary in all cases.
If you decide not to use an external power supply you must set the “Vin ext. Jumper” to the right, in “Arduino 5V position” and do not forget to place the switch in the Raspberry connection bridge to the right.
If you choose to use an external power supply you must place it through the external power input, the “Vin ext. jumper” must be set to the left, in “Vext position” and the BAT/REG jumper must be set to the right, in REG position. The power supply must be able to provide sufficient current up to 2A. You can also use a battery to external power the shield, in that case put the BAT/REG jumper to the left, in BAT position.
Besides this, do not forget to place the switch in the Raspberry connection bridge to the left.
You can check our FAQ about 3G/Sim900/Sim908 here.
Connect the antennas to the shield and the shield to the Raspberry Pi connection bridge. Now press the ON button in the shield, you will see a green LED.
Open a Terminal window on Raspberry Pi, or connect to Raspberry Pi through SSH.
In order to enter via SSH change here the IP from your Raspberry Pi. The default user is pi.
ssh -X pi@192.168.1.X
Now you will have to type your password. The default password is “raspberry”.
Install minicom (only if you haven't done it before):
sudo apt-get install minicom
Open comunication with the module UART, the baudrate is 115200:
minicom -b 115200 -o -D /dev/ttyAMA0
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.
To exit minicom press CTRL+A, then press X and Yes.
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 or Raspberry Pi => 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.
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.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin = 2; int button = 12; char aux_str[30]; char phone_number[]="******"; // ********* is the number to call char pin[] = "****"; void setup(){ pinMode(onModulePin, OUTPUT); pinMode(button, INPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code sprintf(aux_str, "AT+CPIN=%s", pin); sendATcommand(aux_str, "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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin = 2; int button = 12; char aux_str[30]; char phone_number[]="*******"; // ********* is the number to call char pin[] = "*******"; void setup(){ pinMode(onModulePin, OUTPUT); pinMode(button, INPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code sprintf(aux_str, "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); delay(3000); printf("Connecting to the network...\n"); 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 printf("Call disconnected\n"); } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
To make a lost call next code is used.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin = 2; int seconds = 20; char aux_str[30]; char phone_number[]="*******"; // ********* is the number to call char pin[] = "*******"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code sprintf(aux_str, "AT+CPIN=%s", pin); sendATcommand(aux_str, "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 Serial.println("Call disconnected"); } 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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin = 2; int seconds = 20; char aux_str[30]; char phone_number[]="*******"; // ********* is the number to call char pin[] = "*******"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); // sets the PIN code sprintf(aux_str, "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); delay(3000); printf("Connecting to the network...\n"); 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 printf("Call disconnected\n"); } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
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 | Response | Description |
---|---|---|
ATD*********; | ********* is the number to call. | |
ATA | OK | Answer an incoming call. |
ATH | OK | Cancel voice calls. |
The first code is used to send a SMS, the second one reads the first SMS into the memory.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin= 2; char aux_string[30]; char phone_number[]="*********"; // ********* is the number to call char pin[] = "****"; char sms_text[]="Test-Arduino-Hello World"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code sprintf(aux_string, "AT+CPIN=%s", pin); sendATcommand(aux_string, "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); // sets the SMS mode to text Serial.println("Sending SMS"); sprintf(aux_string,"AT+CMGS=\"%s\"", phone_number); answer = sendATcommand(aux_string, ">", 2000); // send the SMS number if (answer == 1) { Serial.println(sms_text); 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; // 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); // Initialice 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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin= 2; char aux_string[30]; char phone_number[]="*********"; // ********* is the number to call char pin[] = "*******"; char sms_text[]="Test-Arduino-Hello World"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); // sets the PIN code sprintf(aux_string, "AT+CPIN=%s", pin); sendATcommand(aux_string, "OK", 2000); delay(3000); printf("Connecting to the network...\n"); while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); printf("Setting SMS mode...\n"); sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text printf("Sending SMS\n"); sprintf(aux_string,"AT+CMGS=\"%s\"", phone_number); answer = sendATcommand(aux_string, ">", 2000); // send the SMS number if (answer == 1) { Serial.println(sms_text); Serial.write(0x1A); answer = sendATcommand("", "OK", 20000); if (answer == 1) { printf("Sent \n"); } else { printf("error \n"); } } else { printf("error %o\n",answer); } } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int x; int onModulePin= 2; char aux_string[30]; char SMS[200]; char pin[] = "****"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); // sets the PIN code sprintf(aux_string, "AT+CPIN=%s", pin); sendATcommand(aux_string, "OK", 2000); delay(3000); Serial.println("Setting SMS mode..."); sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); // selects the memory answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000); // reads the first SMS if (answer == 1) { answer = 0; while(Serial.available() == 0); // this loop reads the data of the SMS do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() > 0){ SMS[x] = Serial.read(); x++; // check if the desired answer (OK) is in the response of the module if (strstr(SMS, "OK") != NULL) { answer = 1; } } }while(answer == 0); // Waits for the asnwer with time out SMS[x] = '\0'; Serial.print(SMS); } else { Serial.print("error "); Serial.println(answer, DEC); } } 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); // Initialice 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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int x; int onModulePin= 2; char aux_string[30]; char SMS[200]; char pin[] = "*******"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); // sets the PIN code sprintf(aux_string, "AT+CPIN=%s", pin); sendATcommand(aux_string, "OK", 2000); delay(3000); printf("Setting SMS mode...\n"); sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); // selects the memory answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000); // reads the first SMS if (answer == 1) { answer = 0; while(Serial.available() == 0); // this loop reads the data of the SMS do{ // if there are data in the UART input buffer, reads it and checks for the asnwer if(Serial.available() > 0){ SMS[x] = Serial.read(); x++; // check if the desired answer (OK) is in the response of the module if (strstr(SMS, "OK") != NULL) { answer = 1; } } }while(answer == 0); // Waits for the asnwer with time out SMS[x] = '\0'; printf("%s\n",SMS); } else { printf("error %o\n",answer); } } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
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. |
Creating a file into the FTP server, writing it and reading it.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin = 2; char aux_str[50]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char ftp_server[]="*******"; char ftp_user_name[]="*******"; char ftp_password[]="*******"; char path[]="/*******/"; char incoming_data[120]; char file_name[ ]="test.txt"; char test_str[ ]= "0000000011111111222222223333333344444444555555556666666677777777000000001111111122222222333333334444"; int data_size, aux; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(5000); //sets the PIN code snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); 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(){ // sets APN, user name and password sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password); sendATcommand(aux_str, "OK", 2000); // sets the paremeters for the FTP server while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) != 1); sendATcommand("AT+FTPCID=1", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPSERV=\"%s\"", ftp_server); sendATcommand(aux_str, "OK", 2000); sendATcommand("AT+FTPPORT=21", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPUN=\"%s\"", ftp_user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPPW=\"%s\"", ftp_password); sendATcommand(aux_str, "OK", 2000); } void uploadFTP(){ snprintf(aux_str, sizeof(aux_str), "AT+FTPPUTNAME=\"%s\"", file_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPPUTPATH=\"%s\"", path); sendATcommand(aux_str, "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) { sendATcommand(test_str, "+FTPPUT:1,1", 30000); sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", 30000); Serial.println("Upload done!!"); } else { sendATcommand("AT+FTPPUT=2,0", "OK", 30000); } } else { sendATcommand("AT+FTPPUT=2,0", "OK", 30000); } } else { Serial.println("Error opening the FTP session"); } } void downloadFTP(){ int x = 0; snprintf(aux_str, sizeof(aux_str), "AT+FTPGETNAME=\"%s\"", file_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPGETPATH=\"%s\"", path); sendATcommand(aux_str, "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 opening 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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, unsigned int timeout); int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); void downloadFTP(); void uploadFTP(); void configure_FTP(); int8_t answer; int onModulePin = 2; char aux_str[50]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char ftp_server[]="*******"; char ftp_user_name[]="*******"; char ftp_password[]="*******"; char path[]="/*******/"; char incoming_data[120]; char file_name[ ]="test.txt"; char test_str[ ]= "0000000011111111222222223333333344444444555555556666666677777777000000001111111122222222333333334444"; int data_size, aux; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(5000); //sets the PIN code snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); configure_FTP(); uploadFTP(); downloadFTP(); printf("Incoming data: %s\n",incoming_data); } void loop(){ } void configure_FTP(){ // sets APN, user name and password sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password); sendATcommand(aux_str, "OK", 2000); // sets the paremeters for the FTP server while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) != 1); sendATcommand("AT+FTPCID=1", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPSERV=\"%s\"", ftp_server); sendATcommand(aux_str, "OK", 2000); sendATcommand("AT+FTPPORT=21", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPUN=\"%s\"", ftp_user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPPW=\"%s\"", ftp_password); sendATcommand(aux_str, "OK", 2000); } void uploadFTP(){ snprintf(aux_str, sizeof(aux_str), "AT+FTPPUTNAME=\"%s\"", file_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPPUTPATH=\"%s\"", path); sendATcommand(aux_str, "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) { sendATcommand(test_str, "+FTPPUT:1,1", 30000); sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", 30000); printf("Upload done!!\n"); } else { sendATcommand("AT+FTPPUT=2,0", "OK", 30000); } } else { sendATcommand("AT+FTPPUT=2,0", "OK", 30000); } } else { printf("Error opening the FTP session\n"); } } void downloadFTP(){ int x = 0; snprintf(aux_str, sizeof(aux_str), "AT+FTPGETNAME=\"%s\"", file_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+FTPGETPATH=\"%s\"", path); sendATcommand(aux_str, "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); printf("Data received: %d\n",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 { printf("Download finished\n"); } } else if (answer == 2) { printf("Error from FTP\n"); } else { printf("Error getting the file\n"); data_size = 0; } }while (data_size > 0); } else { printf("Error opening the FTP session\n"); } } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const 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(); printf("%c",response[x]); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { printf("\n"); answer = 1; } // check if the desired answer 2 is in the response of the module else if (strstr(response, expected_answer2) != NULL) { printf("\n"); answer = 2; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
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. |
Sending data to a TCP server first, and then it sends to a UDP server.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin= 2; char aux_str[100]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char IP_address[]="*******"; char port[]="*******"; 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 snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 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(sendATcommand("AT+CIPSTATUS", "INITIAL", 500) == 0 ); delay(5000); snprintf(aux_str, sizeof(aux_str), "AT+CSTT=\"%s\",\"%s\",\"%s\"", apn, user_name, password); // Sets the APN, user name and password if (sendATcommand2(aux_str, "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand("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(sendATcommand("AT+CIPSTATUS", "GPRSACT", 500) == 0 ); delay(5000); // Gets Local IP Address if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1) { // Waits for status IP STATUS while(sendATcommand("AT+CIPSTATUS", "IP STATUS", 500) == 0 ); delay(5000); Serial.println("Opening TCP"); snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"", IP_address, port); // Opens a TCP socket if (sendATcommand2(aux_str, "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 opening 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 = 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(); //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 else if (strstr(response, expected_answer2) != NULL) { answer = 2; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, unsigned int timeout); int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin= 2; char aux_str[100]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char IP_address[]="*******"; char port[]="*******"; char ip_data[40]="Test string from GPRS shield\r\n"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand2(aux_str, "OK", "ERROR", 2000); delay(3000); printf("Connecting to the network...\n"); 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(sendATcommand("AT+CIPSTATUS", "INITIAL", 500) == 0 ); delay(5000); snprintf(aux_str, sizeof(aux_str), "AT+CSTT=\"%s\",\"%s\",\"%s\"", apn, user_name, password); // Sets the APN, user name and password if (sendATcommand2(aux_str, "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand("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(sendATcommand("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); printf("Opening TCP\n"); snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"", IP_address, port); // Opens a TCP socket if (sendATcommand2(aux_str, "CONNECT OK", "CONNECT FAIL", 30000) == 1) { printf("Connected\n"); // 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 { printf("Error opening the connection\n"); } } else { printf("Error getting the IP address\n"); } } else { printf("Error bring up wireless connection\n"); } } else { printf("Error setting the APN\n"); } } else { printf("Error setting the single connection\n"); } sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000); delay(10000); } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const 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(); printf("%c",response[x]); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { printf("\n"); answer = 1; } // check if the desired answer 2 is in the response of the module else if (strstr(response, expected_answer2) != NULL) { printf("\n"); answer = 2; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
SIM900 allows to use 8 connections simultaneously. Here is the example code with a UDP and TCP connections.
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin= 2; char aux_str[100]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char IP_address[]="*******"; char port[]="*******"; 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 snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 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); snprintf(aux_str, sizeof(aux_str), "AT+CSTT=\"%s\",\"%s\",\"%s\"", apn, user_name, password); // Sets the APN, user name and password if (sendATcommand2(aux_str, "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand("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(sendATcommand("AT+CIPSTATUS", "GPRSACT", 500) == 0 ); delay(5000); // Gets Local IP Address if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1) { // Waits for status IP STATUS while(sendATcommand("AT+CIPSTATUS", "IP STATUS", 500) == 0 ); delay(5000); Serial.println("Opening TCP"); snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=1,\"TCP\",\"%s\",\"%s\"", IP_address, port); // Opens a TCP socket with connection 1 if (sendATcommand2(aux_str, "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 opening 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 = 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(); //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 else if (strstr(response, expected_answer2) != NULL) { answer = 2; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, unsigned int timeout); int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin= 2; char aux_str[100]; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char IP_address[]="*******"; char port[]="*******"; char ip_data[40]="Test string from GPRS shield\r\n"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); // sets the PIN code snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand2(aux_str, "OK", "ERROR", 2000); delay(3000); printf("Connecting to the network...\n"); 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(sendATcommand("AT+CIPSTATUS", "INITIAL", 500) == 0 ); delay(5000); snprintf(aux_str, sizeof(aux_str), "AT+CSTT=\"%s\",\"%s\",\"%s\"", apn, user_name, password); // Sets the APN, user name and password if (sendATcommand2(aux_str, "OK", "ERROR", 30000) == 1) { // Waits for status IP START while(sendATcommand("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(sendATcommand("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); printf("Opening TCP\n"); snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=1,\"TCP\",\"%s\",\"%s\"", IP_address, port); // Opens a TCP socket with connection 1 if (sendATcommand2(aux_str, "CONNECT OK", "CONNECT FAIL", 30000) == 1) { printf("Connected\n"); // 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 { printf("Error opening the connection 1\n"); } } else { printf("Error getting the IP address\n"); } } else { printf("Error bring up wireless connection\n"); } } else { printf("Error setting the APN\n"); } } else { printf("Error setting the multi-connection\n"); } sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000); delay(10000); } 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(const char* ATcommand, const 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer) != NULL) { printf("\n"); answer = 1; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const 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(); printf("%c",response[x]); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { printf("\n"); answer = 1; } // check if the desired answer 2 is in the response of the module else if (strstr(response, expected_answer2) != NULL) { printf("\n"); answer = 2; } } } // Waits for the asnwer with time out while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
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 |
SIM900 can launch a HTTP operation like GET or POST. Here is an example with GET operation:
Arduino:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ int8_t answer; int onModulePin= 2; char data[512]; int data_size; char aux_str[100]; char aux; int x = 0; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char url[ ]="test.libelium.com/test-get-post.php?a=1&b=2"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); power_on(); delay(3000); //sets the PIN code snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); delay(3000); while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0); // sets APN , user name and password sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password); sendATcommand(aux_str, "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 snprintf(aux_str, sizeof(aux_str), "AT+HTTPPARA=\"URL\",\"%s\"", url); answer = sendATcommand(aux_str, "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("No more data available"); 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; }
Raspberry Pi:
/* * GPRS/GSM Quadband Module (SIM900) * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Alejandro Gallego & Marcos Martinez */ //Include arduPi library #include "arduPi.h" int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, unsigned int timeout); int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout); void power_on(); int8_t answer; int onModulePin= 2; char data[512]; int data_size; char aux_str[100]; char aux; int x = 0; char pin[]="*******"; char apn[]="*******"; char user_name[]="*******"; char password[]="*******"; char url[ ]="test.libelium.com/test-get-post.php?a=1&b=2"; void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); //sets the PIN code snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); delay(3000); while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0); // sets APN , user name and password sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name); sendATcommand(aux_str, "OK", 2000); snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password); sendATcommand(aux_str, "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 snprintf(aux_str, sizeof(aux_str), "AT+HTTPPARA=\"URL\",\"%s\"", url); answer = sendATcommand(aux_str, "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); printf("Data received: %d\n",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 { printf("Download finished\n"); } } else if (answer == 2) { printf("Error from HTTP\n"); } else { printf("No more data available\n"); data_size = 0; } sendATcommand("", "+HTTPACTION:0,200", 20000); }while (data_size > 0); printf("Data received: %s\n",data); } else { printf("Error getting the url\n"); } } else { printf("Error setting the url\n"); } } else { printf("Error setting the CID\n"); } } else { printf("Error initializating\n"); } 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(const char* ATcommand, const 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); 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(); printf("%c",response[x]); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer1) != NULL) { printf("\n"); answer = 1; } } // Waits for the asnwer with time out } while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const 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); 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(); printf("%c",response[x]); x++; // check if the desired answer 1 is in the response of the module if (strstr(response, expected_answer1) != NULL) { printf("\n"); answer = 1; } // check if the desired answer 2 is in the response of the module if (strstr(response, expected_answer2) != NULL) { printf("\n"); answer = 2; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < timeout)); return answer; } int main (){ setup(); while(1){ loop(); } return (0); }
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. |
GPRS/GSM Quadband Module for Arduino and Raspberry Pi(SIM900) enables the connectivity to high speed WCDMA and HSPA cellular networks in order to make possible the creation of the next level of worldwide interactivity projects inside the new "Internet of Things" era.
You can download our Fritzing libraries from this area.
NOTE: If you are looking for a complete solution to use 3G, GPRS and A-GPS, you can use our 3G/GPRS shield for Arduino and Raspberry Pi(3G + GPRS) or our Kit with Audio/Video
If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.
Know more at:
Get the Starter Kits at: