Arduino GSM shield

By on March 21, 2011
Pin It

 

Download_GSM_library

 

 

 

This is a very low cost and simple Arduino GSM and GPRS shield. We use the module SIMCom SIM900. It’s the cheaper module now avalaible in the market. The module is not simple to mount by an hobbyst, so we use the Breakboard TDGGSM_900 that we presented in this post.

You can buy the GSM module premounted from our cart.

To connect this module to Arduino we make a PCB that include a LM317 some capacitor filter and no more.

The LM317 give to module about 3.9V.
There is a switch to select the way to comunicate with arduino:
– throw serial hardware (pin 0 and 1)
– throw serial software (pin 4 and 5)

R1: 470 ohm
R2: 1 kohm
C1-C3: 100 nF
C2-C4: 470 µF 25 VL
U1: LM317
GSM: TDGGSM_900 module







SW1: switch
P1: Microswitch

Selecting the serial hardware there are no problem with the baudrate (the default module baudrate is 115200) but there are problem programming the module (the serial is also used to upload the sketch).
Using instead the pin 4 and 5 there aren’t problems to upload the sketch but the maximum baudrate for NewSoftSerial (the serial library) is 57600.
We performed a GSM library to controll easly the module. The GSM library is a modified version of the library of HWKitchen.
With our version we controll the module throw the pin 4 and 5 (so normal digital pin) and our GSM libray include also the NewSoftSerial, so you can easy control the module, send and read SMS, make call, control the GSM state ecc.

Download GSM_Shield_Library

New GSM/GPRS library

 

 

 

This is the GSM_shield library documentation:

Download GSM_Shield_Library

New GSM/GPRS library

 

 

 

 

In this page you can find some example.

Example to test library

/* GSM Shield example

 created 2011
 by Boris Landoni

 This example code is in the public domain.

 http://www.open-electronics.org
 http://www.futurashop.it
 */

#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;

void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}

void loop()
{

    int ver;
    ver=gsm.LibVer();
    Serial.print("Response Ver ");
    Serial.println(ver);
    delay(50000);
}

 

Example to network registration

 

/* GSM Shield example

 created 2011
 by Boris Landoni

 This example code is in the public domain.

 http://www.open-electronics.org
 http://www.futurashop.it
 */

#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;

void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}

void loop()
{

    int reg;
    reg=gsm.CheckRegistration();
    switch (reg){
      case REG_NOT_REGISTERED:
        Serial.println("not registered");
        break;
      case REG_REGISTERED:
        Serial.println("GSM module is registered");
        break;
      case REG_NO_RESPONSE:
        Serial.println("GSM doesn't response");
        break;
      case REG_COMM_LINE_BUSY:
        Serial.println("comm line is not free");
        break;
    }
    delay(2000);

    reg=gsm.IsRegistered();
      Serial.print("Registration ");
      Serial.println(reg);

    delay(50000);
}

 

Example to test call

 

/* GSM Shield example

 created 2011
 by Boris Landoni

 This example code is in the public domain.

 http://www.open-electronics.org
 http://www.futurashop.it
 */

#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;

void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}

void loop()
{

    int call;
    call=gsm.CallStatus();
    switch (call){
      case CALL_NONE:
        Serial.println("no call");
        break;
      case CALL_INCOM_VOICE:
        Serial.println("incoming voice call");
        delay(5000);
        gsm.PickUp();
        break;
      case CALL_ACTIVE_VOICE:
        Serial.println("active voice call");
        delay(5000);
        gsm.HangUp();
        break;
      case CALL_NO_RESPONSE:
        Serial.println("no response");
        break;
    }
    delay(1000);

}

 

Download GSM_Shield_Library

New GSM/GPRS library

 

 

 

Bigger example

 

/* GSM Shield example

 created 2011
 by Boris Landoni

 This example code is in the public domain.

 http://www.open-electronics.org
 http://www.futurashop.it
 */

#include <GSM_Shield.h>

//**************************************************************************
char number[]="+39123456789";  //Destination number
char text[]="hello world";  //SMS to send
byte type_sms=SMS_UNREAD;      //Type of SMS
byte del_sms=0;                //0: No deleting sms - 1: Deleting SMS
//**************************************************************************

GSM gsm;
char sms_rx[122]; //Received text SMS
//int inByte=0;    //Number of byte received on serial port
char number_incoming[20];
int call;
int error;

void setup()
{
  Serial.begin(9600);
  Serial.println("system startup");
  gsm.TurnOn(9600);          //module power on
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(0);               //enable AT echo 

}

void loop()
{
  char inSerial[5];
  int i=0;
  delay(2000);

    Check_Call(); //Check if there is an incoming call
    Check_SMS();  //Check if there is SMS
    //Check data serial com 

    if (Serial.available() > 0)
    {
       while (Serial.available() > 0) {
         inSerial[i]=(Serial.read()); //read data
         i++;
       }
       inSerial[i]='\0';
      Check_Protocol(inSerial);
    }

}  

void Check_Protocol(String inStr)
{
       Serial.print("Command: ");
       Serial.println(inStr);

  Serial.println("Check_Protocol");

    switch (inStr[0])
      {
       case 'a' :  //Answer
           if (gsm.CallStatus()==CALL_INCOM_VOICE){
             gsm.PickUp();
             Serial.println("Answer");
           }
           else
           {
             Serial.println("No incoming call");
           }
         break;

       case 'c': // C  //Call
         if (inStr.length()<2)  //To call variable 'number'    comand   c
         {
           Serial.print("Calling ");
           Serial.println(number);
           gsm.Call(number);
         }
         if (inStr.length()==2)  //To call number in phone book position   comand   cx where x is the SIM position
         {
             error=gsm.GetPhoneNumber(inStr[1],number);
             if (error!=0)
             {
               Serial.print("Calling ");
               Serial.println(number);
               gsm.Call(number);
             }
             else
             {
               Serial.print("No number in pos ");
               Serial.println(inStr[1]);
             }
         }
         break;

       case 'h': //H //HangUp if there is an incoming call
         if (gsm.CallStatus()!=CALL_NONE)
         {
           Serial.println("Hang");
           gsm.HangUp();
         }
         else
         {
           Serial.println("No incoming call");
         }
         break;

       case 's': //S //Send SMS
         Serial.print("Send SMS to ");
         Serial.println(number);
         error=gsm.SendSMS(number,text);
         if (error==0)  //Check status
         {
             Serial.println("SMS ERROR \n");
         }
         else
         {
             Serial.println("SMS OK \n");
         }
         break;

       case 'p':  //Read-Write Phone Book
         if (inStr.length()==3)
         {

           switch (inStr[1])
           {
             case 'd':  //Delete number in specified position  pd2
               error=gsm.DelPhoneNumber(inStr[2]);
               if (error!=0)
               {
                 Serial.print("Phone number position ");
                 Serial.print(inStr[2]);
                 Serial.println(" deleted");
               }
               break;

             case 'g':  //Read from Phone Book position      pg2
               error=gsm.GetPhoneNumber(inStr[2],number);
               if (error!=0)  //Find number in specified position
               {
                 Serial.print("Phone Book position ");
                 Serial.print(inStr[2]);
                 Serial.print(": ");
                 Serial.println(number);
               }
               else  //Not find number in specified position
               {
                 Serial.print("No Phone number in position ");
                 Serial.println(inStr[2]);
               }
               break;
             case 'w':  //Write from Phone Book Position    pw2
               error=gsm.WritePhoneNumber(inStr[2],number);
               if (error!=0)
               {
                 Serial.print("Number ");
                 Serial.print(number);
                 Serial.print(" writed in Phone Book position ");
                 Serial.println(inStr[2]);
               }
               else Serial.println("Writing error");
               break;

           }

         }
         break;

       }

    delay(1500);

    return;
 }

 void Check_Call()  //Check status call if this is available
 {
     call=gsm.CallStatus();
     switch (call)
     {
       case CALL_NONE:
         Serial.println("no call");
         break;
       case CALL_INCOM_VOICE:
         gsm.CallStatusWithAuth(number_incoming,0,0);
         Serial.print("incoming voice call from ");
         Serial.println(number_incoming);
         break;
       case CALL_ACTIVE_VOICE:
         Serial.println("active voice call");
         break;
       case CALL_NO_RESPONSE:
         Serial.println("no response");
         break;
     }
     return;
 }

 void Check_SMS()  //Check if there is an sms 'type_sms'
 {
     char pos_sms_rx;  //Received SMS position
     pos_sms_rx=gsm.IsSMSPresent(type_sms);
     if (pos_sms_rx!=0)
     {
       //Read text/number/position of sms
       gsm.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
       Serial.print("Received SMS from ");
       Serial.print(number_incoming);
       Serial.print("(sim position: ");
       Serial.print(word(pos_sms_rx));
       Serial.println(")");
       Serial.println(sms_rx);
       if (del_sms==1)  //If 'del_sms' is 1, i delete sms
       {
         error=gsm.DeleteSMS(pos_sms_rx);
         if (error==1)Serial.println("SMS deleted");
         else Serial.println("SMS not deleted");
       }
     }
     return;
 }

 

NEW GPRS FUNCTION 

Test AT command

#include "SIM900.h"
#include <NewSoftSerial.h>
#include "inetGSM.h"

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to communicate with SIM900 through AT commands.

InetGSM inet;
char msg[150];
int numdata;
char inSerial[40];
int i=0;

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(4800))
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");
};

void loop()
{
  //Read for new byte on serial hardware,
  //and write them on NewSoftSerial.
  serialhwread();
  //Read for new byte on NewSoftSerial.
  serialswread();
};

void serialhwread(){
  i=0;
  if (Serial.available() > 0){
    while (Serial.available() > 0) {
      inSerial[i]=(Serial.read());
      delay(10);
      i++;
    }

    inSerial[i]='\0';
    if(!strcmp(inSerial,"/END")){
      Serial.println("_");
      inSerial[0]=0x1a;
      inSerial[1]='\0';
      gsm.SimpleWrite(inSerial);
    }
    //Send a saved AT command using serial port.
    if(!strcmp(inSerial,"TEST")){
      Serial.println("SIGNAL QUALITY");
      gsm.SimpleWrite("AT+CSQ");
    }
    //Read last message saved.
    if(!strcmp(inSerial,"MSG")){
      Serial.println(msg);
    }

    else{
      Serial.println(inSerial);
      gsm.SimpleWrite(inSerial);
    }    

    inSerial[0]='\0';
  }
}

void serialswread(){
  gsm.SimpleRead();
}

 

 

GPRS Client

#include "SIM900.h"
#include <NewSoftSerial.h>
#include "inetGSM.h"

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to start a connection as client.

InetGSM inet;
char msg[100];
int numdata;
char inSerial[30];
int i=0;

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(4800))
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");

  //GPRS attach, put in order APN, username and password.
  //If no needed auth let them blank.
  if (gsm.attachGPRS("internet.wind", "", ""))
    Serial.println("status=ATTACHED");
  else Serial.println("status=ERROR");
  delay(5000);

  //TCP Client GET, send a GET request to the server and
  //save the reply.
  numdata=inet.httpGET("www.google.com", 80, "/", msg, 100);
  //Print the results.
  Serial.println("\nNumber of data received:");
  Serial.println(numdata);
  Serial.println("Data received:");
  Serial.println(msg); 

  //Tweet
  //inet.tweet("*********************key************", "An Arduino at #cpes15");

};

void loop()
{
  //Read for new byte on serial hardware,
  //and write them on NewSoftSerial.
  serialhwread();
  //Read for new byte on NewSoftSerial.
  serialswread();
};

void serialhwread(){
  i=0;
  if (Serial.available() > 0){
    while (Serial.available() > 0) {
      inSerial[i]=(Serial.read());
      delay(10);
      i++;
    }

    inSerial[i]='\0';
    if(!strcmp(inSerial,"/END")){
      Serial.println("_");
      inSerial[0]=0x1a;
      inSerial[1]='\0';
      gsm.SimpleWrite(inSerial);
    }
    //Send a saved AT command using serial port.
    if(!strcmp(inSerial,"TEST")){
      Serial.println("SIGNAL QUALITY");
      gsm.SimpleWrite("AT+CSQ");
    }
    //Read last message saved.
    if(!strcmp(inSerial,"MSG")){
      Serial.println(msg);
    }

    else{
      Serial.println(inSerial);
      gsm.SimpleWrite(inSerial);
    }    

    inSerial[0]='\0';
  }
}

void serialswread(){
  gsm.SimpleRead();
}

 

GPRS Server

#include "SIM900.h"
#include <NewSoftSerial.h>
#include "inetGSM.h"

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to start a connection as server.

InetGSM inet;
char msg[100];
int numdata;
char inSerial[30];
int i=0;
long lasttime;

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(4800))
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");

  //GPRS attach, put in order APN, username and password.
  //If no needed auth let them blank.
  if (gsm.attachGPRS("internet.wind", "", ""))
    Serial.println("status=ATTACHED");
  else Serial.println("status=ERROR");
  delay(5000);

  //Read IP address.
  int i=0;
  while(i<20){
    gsm.SimpleRead();
    i++;
  }
  delay(5000);
  gsm.write((const uint8_t*)"AT+CIFSR\r", 10);
  gsm.read(msg, 200);
  Serial.println(msg);
  delay(5000); 

  //Tweet
  //inet.tweet("*********************key************", "An Arduino at #cpes15");

  //TCP Server. Start the socket connection
  //as server on the assigned port.
  Serial.println(msg);
  delay(5000);
  if (gsm.connectTCPServer(80))
    Serial.println("status=TCPSERVERWAIT");
  else Serial.println("ERROR in Server");
  lasttime=millis();
};

void loop(){
  //serialhwread();
  //serialswread();
  //Check if there is an active connection.
  if (gsm.connectedClient()){
    //Read and print the last message received.
    gsm.read(msg, 200);
    Serial.println(msg);
  }
};

/*
void loop()
{
  serialhwread();
  serialswread();
};
*/

void serialhwread(){
  i=0;
  if (Serial.available() > 0){
    while (Serial.available() > 0) {
      inSerial[i]=(Serial.read());
      delay(10);
      i++;
    }

    inSerial[i]='\0';
    if(!strcmp(inSerial,"/END")){
      Serial.println("_");
      inSerial[0]=0x1a;
      inSerial[1]='\0';
      gsm.SimpleWrite(inSerial);
    }
    //Send a saved AT command using serial port.
    if(!strcmp(inSerial,"TEST")){
      Serial.println("SIGNAL QUALITY");
      gsm.SimpleWrite("AT+CSQ");
    }
    //Read last message saved.
    if(!strcmp(inSerial,"MSG")){
      Serial.println(msg);
    }

    else{
      Serial.println(inSerial);
      gsm.SimpleWrite(inSerial);
    }    

    inSerial[0]='\0';
  }
}

void serialswread(){
  gsm.SimpleRead();
}

 

Test SMS without GPRS function

#include "SIM900.h"
#include <NewSoftSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

char msg[200];
int numdata;

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(4800))
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");

  //Enable this two lines if you want to send an SMS.
  //if (gsm.sendSMS("3471234567", "Arduino SMS"))
    //Serial.println("\nSMS sent OK");

};

void loop()
{
  char smsbuffer[160];
  char n[20];
  //Read if there are messages on SIM card and print them.
  if(gsm.readSMS(smsbuffer, 160, n, 20))
  {
    Serial.println(n);
    Serial.println(smsbuffer);
  }
  delay(1000);
};

 

 



jlcpcb.com



About Boris Landoni

Boris Landoni is the technical manager of Open-Electronics.org. Skilled in the GSM field, embraces the Open Source philosophy and its projects are available to the community.

218 Comments

  1. Pingback: Electronics-Lab.com Blog » Blog Archive

  2. Pingback: Lower Cost Arduino Cell Shield - Hack a Day

  3. Pingback: Lower Cost Arduino Cell Shield « Black Hat Security

  4. Pingback: Electronics-Lab.com Blog » Blog Archive » Yet another Arduino GSM shield

  5. Pingback: Arduino GSM GPRS and GPS shield | Open Electronics

  6. Pingback: Arduino GSM shield | Hobbies & Interests! |...

  7. Pingback: SainSmart 1.8″ TFT Color LCD Display Module with SPI Interface & MicroSD for Arduino UNO MEGA R3 · WWW.INFOWEBHUB.NET

  8. Pingback: Arduino | Pearltrees

  9. Pingback: <!–:en–>Telefónica I + D and Arduino alliance to promote the "Internet of things" through the open hardware<!–:–><!–:es–>Telefónica I+D y Arduino se alían para promover el “Internet de l

  10. Pingback: styczen

  11. Pingback: testy2014

  12. Pingback: Open Electronics: a Year in Review | Open Electronics

  13. Pingback: gtownrestorationscdc.org

  14. Pingback: countydrugcard.org

  15. Pingback: New Years Countdown Clock With SMS Display | ITead Intelligent Systems Blog

  16. Pingback: Fix Arduino Error Checking Windows XP, Vista, 7, 8 [Solved]

  17. Pingback: Telefónica I+D y Arduino se alían para promover el “Internet de las cosas” a través del hardware libre

  18. Pingback: Happy new year from Open-Electronics.org: best posts in 2016, thank you! | Open Electronics

Leave a Reply