Click here to Skip to main content
15,881,559 members
Articles / Internet of Things

How to Send, Receive and Delete SMS with IOT Devices (Arduino and GSM Shield)

Rate me:
Please Sign up or sign in to vote.
4.84/5 (30 votes)
2 Mar 2016CPOL9 min read 153.1K   7.2K   71   35
Let us see how to send, receive and delete SMS using Arduino and GSM Sheild

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

Introduction

In this article, I am going to walk you through on how to use Arduino and GSM SIM 900 modem for Sending and Receiving SMS.

Please note, I am using a 2G Modem; which means it works only with 2G compatible SIM card. That being said, it will not work with 3G or 4G LTE SIM cards. If you have questions regarding the SIM card with which I have tested is,T-Mobile Standard Prepaid SIM card. 

http://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a-GSM-Modem-using-AT-Com

I will be covering the below topics.

Prerequisites 
Background 
Using Code

Below is the snapshot of the GSM SIM 900 Shield with Arduino Uno

GSM_Modem

Prerequisites

1) Arduino, the most commonly used ones are Arduino Uno.

2) GSM SIM 900 – It’s a cheap and easy to use shield. You can buy one from Ebay. Please note, the GSM Shield for Arduino comes with a 2G, 3G etc. The one which I'm using is a Quad band; which means it only works with 2G compatible GSM Sim cards.

3) SIM 900 Library for Arduino – You can download from http://www.gsmlib.org/download.html.
Download BETA_GSM_GPRS_GPS_IDE100_v307_1.zip or the latest one.

Background

If you are a beginner to Arduino, take a look into the official Arduino website

http://arduino.cc/en/Guide/HomePage

Introduction to AT Commands

http://www.developershome.com/sms/atCommandsIntro.asp

http://www.codeproject.com/Articles/85636/Introduction-to-AT-commands-and-its-uses

SIM900 Documentation

http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0

Simple Arduino Serial Communication basics

http://arduinobasics.blogspot.com/2012/07/arduino-basics-simple-arduino-serial.html

Using the code

We will be programming the Arduino in a very generic way for receiving the AT Commands, which is being sent by the program running on PC.

You can make use of the below tool, which basically connects the Arduino using Serial communication. Once connected, you should be able to send “AT Commands” and receive the response from Arduino.

Sscom32E Serial tool

http://www.seeedstudio.com/wiki/images/b/b2/Sscom32E.zip

Below is the code snippet that we are making use of for receiving the AT commands and sending back the response through serial communication with the baud rate of 9600.  

The below code snippet for Arduino GSM/GPRS Shield Code is being reused from http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0

Here’s what we do.

  1. The first thing, we should be doing is to include the SoftwareSerial library.
     
  2. Let us open the serial port and set the serial baud rate to 9600. We will begin with 9600 bits per second over the serial communication. More information about the same can be found at http://arduino.cc/en/Serial/Begin
     
  3. Within the loop method, we have to code to receive the AT commands sent from the application running on our PC. Also we do code for sending the GSM Shield response back to PC. Notice below, when we are sending the response back to PC, we read one character at a time and hold the same in buffer with the size as 64 and then write the same over serial port. Finall we will clearing the buffer and reset the count back to zero.
//Serial Relay - Arduino will patch a 
//serial link between the computer and the GPRS Shields
//at 9600 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART 
 
#include <SoftwareSerial.h>
 
SoftwareSerial GPRS(7, 8);
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0;              // counter for buffer array 

void setup()
{
  GPRS.begin(9600);     // the GPRS baud rate   
  Serial.begin(9600);   // the Serial port of Arduino baud rate. 
}
 
void loop()
{
  if (GPRS.available())              // if date is comming from softwareserial port ==> data is comming from gprs shield
  {
    while(GPRS.available())          // reading data into char array 
    {
      buffer[count++]=GPRS.read();   // writing data into array
      if(count == 64)break;
  }
    Serial.write(buffer,count);   // if no data transmission ends, write buffer to hardware serial port
    clearBufferArray();           // call clearBufferArray function to clear the storaged data from the array
    count = 0;                    // set counter of while loop to zero
  }
  if (Serial.available())         // if data is available on hardwareserial port ==> data is comming from PC or notebook
    GPRS.write(Serial.read());    // write it to the GPRS shield
}
void clearBufferArray()           // function to clear buffer array
{
  for (int i=0; i<count;i++)
    { buffer[i]=NULL;}            // clear all index of array with command NULL
}

Using the above code in Arduino IDE, let us compile and upload the same to Arduino connected with the GSM Shield, should be all fine for receiving the AT Commands and responding back with the response to from Shield.

ArduinoGSMCode

Let us make use of “Sscom32E” tool and get our hands wet in using AT commands. First you need to select the appropriate serial com port, leave the default data, stop bit etc. and hit “OpenCom” button so that should open the serial communication with Arduino.

Below is the code snippet, where we are trying to read all SMS by using an AT command, AT+CMGL=”ALL”

ReadAll-Messages

Some more AT Commands

Check whether SIM Ready

AT+CPIN?
+CPIN: READY
OK

Get network Info

AT+COPS?
+COPS: 0,0,”T-Mobile”
OK

Voice Call 

ATD1224XXX31XX;

Hang Up

ATH

Test Signal Strength

AT+CSQ
+CSQ: 11,0
OK

Read Unread messages

AT+CMGL="REC UNREAD"

Read All messages

AT+CMGL="ALL"

SMS Application (.NET WinForm)

Let us dig into the .NET WinForm application and try to understand how the AT Commands are sent over to Arduino using serial communication.

Below is the code snippet which gets all the “COM” ports and adds them to combo box so that you can select the specific port for communicating with your Arduino.

Note – When you are connecting the Arduino to your PC, you should be able to see the COM port it’s using. That is the port you have to select for sending AT Commands.

string[] ports = SerialPort.GetPortNames();
// Add all port names to the combo box:
foreach (string port in ports)
{
    this.cboPortName.Items.Add(port);
}

Next we see how to open a serial connection. In the UI, you see a “Connect” button, on click on that triggers the below code. You can see below, we are making use of SMSHelper for open the COM port with the specified COM port name, baud rate, data bits etc.

SMS-App-Port-Settings

private void btnOK_Click(object sender, EventArgs e)
{
            try
            {
                //Open communication port 
                this.port = smsHelper.OpenPort(this.cboPortName.Text, 
                           Convert.ToInt32(this.cboBaudRate.Text),
                           Convert.ToInt32(this.cboDataBits.Text),
                           Convert.ToInt32(this.txtReadTimeOut.Text),
                           Convert.ToInt32(this.txtWriteTimeOut.Text));

                if (this.port != null)
                {
                    this.gboPortSettings.Enabled = false;
                    this.statusBar1.Text = "Modem is connected at PORT " + this.cboPortName.Text;

                    // Add tab pages
                    // Code for adding tabs goes here
                }
                else
                {
                    //MessageBox.Show("Invalid port settings");
                    this.statusBar1.Text = "Invalid port settings";
                }
            }
            catch (Exception ex)
            {
                ErrorLog(ex.Message);
            }
}

Now we take a look into in understanding on how to open and close serial com port by using System.IO.Ports. SerialPort class. Below is the code snippet for the same.  

We will be creating an instance of SerailPort and set all the required properties like portname, baudrate, data and stop bits etc. Then we open a serial port connection so that we can send AT commands and receive the response. Also notice the DataReceived event is being handled which gets triggered when the Arduino sends data over the specified serial port for communication.

There is one interesting thing that you will see, that is we are creating an instance of AutoResetEvent.  It represents a wait handle event. In the below code snippet, on data received event you will see that when we have received some data, we can set the wait handle event signaling the availability of data that can be read.

Coming next, you will see how the AT Commands and the response are read from the designated serial port for communication between Arduino and PC. 

public SerialPort OpenPort(string portName, int baudRate,
                            int dataBits, int readTimeout, int writeTimeout)
{
            receiveNow = new AutoResetEvent(false);
            SerialPort port = new SerialPort();

            try
            {           
                port.PortName = portName;                 //COM1
                port.BaudRate = baudRate;                 //9600
                port.DataBits = dataBits;                 //8
                port.StopBits = StopBits.One;             //1
                port.Parity = Parity.None;                //None
                port.ReadTimeout = readTimeout;           //300
                port.WriteTimeout = writeTimeout;         //300
                port.Encoding = Encoding.GetEncoding("iso-8859-1");
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                port.Open();
                port.DtrEnable = true;
                port.RtsEnable = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return port;
}

public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
            try
            {
                if (e.EventType == SerialData.Chars)
                {
                    receiveNow.Set();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
}

//Close Port
public void ClosePort(SerialPort port)
{
            try
            {
                port.Close();
                port.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
                port = null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
}

Send/Execute AT Command 

Below is the code snippet for sending the AT Commands over the opened COM port.

We are making a call to “Write” method of SerialPort instance, with the command to be executed. Read the response to make sure the command executed successfully, if not we can throw a generic error message.

public string SendATCommand(SerialPort port,string command, 
                                                   int responseTimeout, string errorMessage)
{
            try
            {               
                port.DiscardOutBuffer();
                port.DiscardInBuffer();
                receiveNow.Reset();
                port.Write(command + "\r");
           
                string input = ReadResponse(port, responseTimeout);
                if ((input.Length == 0) || ((!input.EndsWith("\r\n> "))
                            && (!input.EndsWith("\r\nOK\r\n"))))
                    throw new ApplicationException("No success message was received.");
                return input;
            }
            catch (Exception ex)
            {
                throw ex;
            }
}   

Reading AT Command Response

Below is the code snippet for reading the AT Command response.  

We are going to read the serial data by making a call to ReadExisting method of SerialPort instance; which returns a partial response, so we have to loop through and append the data until the serial data that we received contain a substring “OK” or “\r\n>” means we have completely read the AT command response.

public string ReadResponse(SerialPort port, int timeout)
{
            string serialPortData = string.Empty;
            try
            {    
                do
                {
                    if (receiveNow.WaitOne(timeout, false))
                    {
                        string data = port.ReadExisting();
                        serialPortData += data;
                    }
                    else
                    {
                        if (serialPortData.Length > 0)
                            throw new ApplicationException("Response received is incomplete.");
                        else
                            throw new ApplicationException("No data received from phone.");
                    }
                }
                while (!serialPortData.EndsWith("\r\nOK\r\n") &&
                 !serialPortData.EndsWith("\r\n> ") && !serialPortData.EndsWith("\r\nERROR\r\n"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return serialPortData;
}

Sending SMS

Let us dig into in understanding how to trigger an SMS. Below is the snapshot of the application UI looks like.

SMS-App-SendMessage

Here’s the code snippet for sending SMS using AT Command. The syntax for sending SMS message is as follows.

AT+CMGF=1 <ENTER> - Indicates we are interesting in sending text messages. Please note, using this one you cannot send a Unicode message.

AT+CMGS="+1224XXXXXX" <ENTER>

Test message from CodeProject Send and Receive SMS with IOT Device (Arduino and GSM Shield) <CTRL-Z>

Here’s what we do for sending SMS.

  1. Send an “AT” Command to check whether the phone is connected.
  2. Send a command with AT+CMGF=1, indicating that we will be sending a text message.
  3. Send a command with AT+CMGS="+1224XXXXXX" <ENTER>

Now send a command with the text message that you wish to send with a <CTRL-Z> in the end.

public bool SendMessage(SerialPort port, string phoneNo, string message)
{
            bool isSend = false;
            try
            {                
                string recievedData = SendATCommand(port,"AT", 300, "No phone connected");
                string command = "AT+CMGF=1" + char.ConvertFromUtf32(13);
                recievedData = SendATCommand(port,command, 300, "Failed to set message format.");

                // AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/
                command = "AT+CMGS=\"" + phoneNo + "\"" + char.ConvertFromUtf32(13);
                recievedData = SendATCommand(port, command, 300,
                    "Failed to accept phoneNo");

                command = message + char.ConvertFromUtf32(26);
                recievedData = SendATCommand(port, command, 3000,
                    "Failed to send message"); //3 seconds

                if (recievedData.EndsWith("\r\nOK\r\n"))
                    isSend = true;
                else if (recievedData.Contains("ERROR"))
                    isSend = false;
        
                return isSend;
            }
            catch (Exception ex)
            {
                throw ex; 
            }          
 }    

Below is the debug code snapshot, where you can see when the message is being sent to trigger an SMS, the GSM Modem replays back with the response. If everything goes well, the message will be sent and you should be able to receive the message within a second. 

SendMessage-Debug

Reading SMS Messages

Now it’s the time to look into how we can read SMS messages from SIM memory.  

SMS-App-ReadAll-Messages_1

Let us try to understand the AT commands for reading messages.

1)    Read all messages  - "AT+CMGL=\"ALL\""
2)    Read unread messages  -  "AT+CMGL=\"REC UNREAD\""
3)    Read store sent messages - "AT+CMGL=\"STO SENT\""
4)    Read store unsent messages - AT+CMGL=\"STO UNSENT\""

We will be sending the above mentioned AT commands to GSM Modem using serial communication. Once we receive the response, we will be parsing the same and retuning back to the caller.

public ShortMessageCollection ReadSMS(SerialPort port, string atCommand)
{
            // Set up the phone and read the messages
            ShortMessageCollection messages = null;
            try
            {

                #region Execute Command
                // Check connection
                SendATCommand(port,"AT", 300, "No phone connected");
                // Use message format "Text mode"
                SendATCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
                // Read the messages
                string input = SendATCommand(port, atCommand, 5000, "Failed to read the messages.");
                #endregion

                #region Parse messages
                messages = ParseMessages(input);
                #endregion

            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (messages != null)
                return messages;
            else
                return null;        
}

Below is the code snippet for parsing the SMS messages. The response contains string with CMGL, we will be making use of a Regular expression to match and get the formatted SMS messages. 
Notice below, we are building a collection of short SMS messages and returning the same to the caller. 

public ShortMessageCollection ParseMessages(string input)
{
            ShortMessageCollection messages = new ShortMessageCollection();
            try
            {     
                Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
                Match m = r.Match(input);
                while (m.Success)
                {
                    ShortMessage msg = new ShortMessage();
                    msg.Index = m.Groups[1].Value;
                    msg.Status = m.Groups[2].Value;
                    msg.Sender = m.Groups[3].Value;
                    msg.Alphabet = m.Groups[4].Value;
                    msg.Sent = m.Groups[5].Value;
                    msg.Message = m.Groups[6].Value;
                    messages.Add(msg);
                    m = m.NextMatch();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return messages;
}

Delete SMS Message

Let us see how we can remove or delete message by index or delete all message (All or Read) ones.  Below is the code snippet where you can see how the SMS messages are deleted based on the specified AT command that will be sent over the opened serial com port.

Before we dig into the code for deleting the SMS, let us first understand the AT command usage. 

AT+CMGD=<index><CR> - Deletes the message based on the specified index.

Here’s the high level syntax for deleting SMS. More understanding on the same can be found at http://www.developershome.com/sms/cmgdCommand.asp

+CMGD=index[,flag]

In order to delete all SMS, the below mentioned AT command is sent. Where the value “4” is used to simply ignore the index and delete all the SMS from the storage area.

AT+CMGD=1,4 

public bool DeleteMessage(SerialPort port, string atCommand)
{
            bool isDeleted = false;
            try
            {
                #region Execute Command
                string recievedData = SendATCommand(port,"AT", 300, "No phone connected");
                recievedData = SendATCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
                String command = atCommand;
                recievedData = SendATCommand(port,command, 300, "Failed to delete message");
                #endregion

                if (recievedData.EndsWith("\r\nOK\r\n"))
                {
                    isDeleted = true;
                }
                if (recievedData.Contains("ERROR"))
                {
                    isDeleted = false;
                }
                return isDeleted;
            }
            catch (Exception ex)
            {
                throw ex; 
            }            
} 

Below is the snapshot of the Delete SMS tab

SMS-App-DeleteMessage

Points of Interest

When I was researching and working with IOT devices, I realized the SMS integration is something which is required and helpful for almost all applications. There are numerous application that you can think off in using the SMS functionality. Ofcourse, this is not the only solution but it is a cost effective one as you really don't have to worry about third party sevices for sending SMS. There are times when the IOT devices can function based on the received on some specific message, in such scenarios you can definitely make use of these solution. 

Reference

Note – The front end, .NET WinForm Application was originally coded by Syeda Anila Nusrat. This article reuses the most and does some small enhancements and code refactoring.

History

Version 1.0 - Initial publishing on how to send, receive and delete SMS using Arduino and GSM Sheild - 03/15/2015.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Profile

Around 10 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for healthcare domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.

In Brief

Analyse and create High Level , Detailed Design documents.
Use UML Modelling and create Use Cases , Class Diagram , Component Model , Deployment Diagram, Sequence Diagram in HLD.

Area of Working : Dedicated to Microsoft .NET Technologies
Experience with : C# , J2EE , J2ME, Windows Phone 8, Windows Store App
Proficient in: C# , XML , XHTML, XML, HTML5, Javascript, Jquery, CSS, SQL, LINQ, EF

Software Development

Database: Microsoft SQL Server, FoxPro
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.5
UI: Windows Forms, Windows Presentation Foundation, ASP.NET Web Forms and ASP.NET MVC3, MVC4
Coding: WinForm , Web Development, Windows Phone, WinRT Programming, WCF, WebAPI

Healthcare Domain Experience

CCD, CCR, QRDA, HIE, HL7 V3, Healthcare Interoperability

Education

B.E (Computer Science)

CodeProject Contest So Far:

1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link - http://www.codeproject.com/Articles/582535/HealthReunion-A-Windows-Azure-based-healthcare-pro

2. DnB Developer Contest - DNB Business Lookup and Analytics , link - http://www.codeproject.com/Articles/618344/DNB-Business-Lookup-and-Analytics

3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link - http://www.codeproject.com/Articles/517482/Journey-from-development-code-signing-to-publishin

4. Intel App Innovation Contest 2013 - eHealthCare

5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Contest 2014

6. Grand Prize Winner of CodeProject Android Article Contest 2014

7. Grand Prize Winner of IOT on Azure Contest 2015

Comments and Discussions

 
QuestionSend more than 160 characters SMS using AT Commands Pin
Mohamed Farhan20-Jun-15 11:48
Mohamed Farhan20-Jun-15 11:48 
AnswerRe: Send more than 160 characters SMS using AT Commands Pin
Ranjan.D27-Jun-15 11:19
professionalRanjan.D27-Jun-15 11:19 
QuestionReceived Messages Pin
Tiago Bernardo19-May-15 3:21
Tiago Bernardo19-May-15 3:21 
AnswerRe: Received Messages Pin
Ranjan.D19-May-15 4:38
professionalRanjan.D19-May-15 4:38 
GeneralRe: Received Messages Pin
Tiago Bernardo19-May-15 5:05
Tiago Bernardo19-May-15 5:05 
AnswerRe: Received Messages Pin
jaradcz20-Jul-15 21:35
jaradcz20-Jul-15 21:35 
GeneralRe: Received Messages Pin
Ranjan.D21-Jul-15 3:46
professionalRanjan.D21-Jul-15 3:46 
Questionswitching lights on/off with arduino and gprs shield by sending an sms. Pin
Member 1168133411-May-15 3:20
Member 1168133411-May-15 3:20 
I have arduino uno with gprs shield v2.1 I need to switch light on/off throgh sending an sms, anyone who can help me doing that.
here is the code I try.

C#
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1);

// EN: String buffer for the GPRS shield message
// FR: Mémoire tampon de type string pour les messages du shield GPRS
String SmsStorePos = String("");
String msg = String("");
String snTmp = String("");
String snFull = String("");

// EN: Set to 1 when the next GPRS shield message will contains the SMS message
// FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS
int SmsContentFlag = 0;

// EN: Pin of the LED to turn ON and OFF depending on the received message
// FR: Pin de la LED a allumer/éteindre en fonction du message reçu
int ledPin = 5;

void setup()
{
  mySerial.begin(19200);               // the GPRS baud rate
  mySerial.print("\r");
  delay(1000);
  Serial.begin(19200);                 // the GPRS baud rate
  Serial.println("Started!");

  pinMode( ledPin, OUTPUT );
  digitalWrite( ledPin, LOW );
}

void loop()
{
    char SerialInByte;

    if(Serial.available())
    {
       mySerial.print((unsigned char)Serial.read());
     }
    else  if(mySerial.available())
    {
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();

        // EN: Relay to Arduino IDE Monitor
        // FR: Relayer l'information vers le moniteur Serie Arduino
        Serial.print( SerialInByte );

        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // FR: Le programme écoute également les messages issus du GPRS Shield.
        // -------------------------------------------------------------------

        // EN: If the message ends with <CR> then process the message
        // FR: Si le message se termine par un <CR> alors traiter le message
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          // FR: Stocké le caractère dans le buffer de message
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
            // FR: Ignorer les Line Feed
         }
         else {
           // EN: store the current character in the message string buffer
           // FR: stocker le caractère dans la mémoire tampon réservé au message
           msg += String(SerialInByte);
         }
     }
}

// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
// FR: Execute une action sur base du contenu d'un SMS.
//     Notez que le contenu du SMS est le résultat du traitement de plusieurs messages du shield GPRS.
void ProcessSms( String sms ){
  sms.toLowerCase();
  Serial.print( "ProcessSms for [" );
  Serial.print( sms );
  Serial.println( "]" );

  if( sms.indexOf("on") >= 0 ){
    digitalWrite( ledPin, HIGH );
    Serial.println( "LED IS ON" );
    return;
  }
  if( sms.indexOf("off") >= 0 ){
    digitalWrite( ledPin, LOW );
    Serial.println( "LED IS OFF" );
    return;
  } else {
    mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
    delay(1000);
    mySerial.print("AT+CMGS=\"");
    mySerial.print(snFull);
    mySerial.print("\"\r");
    delay(1000);
    mySerial.print("Unknown Command: ");
    mySerial.print(sms);
    mySerial.print("\r");
    delay(1000);
    mySerial.write(0x1A);  //Equivalent to sending Ctrl+Z
    return;
  }
}
// EN: Request Text Mode for SMS messaging
// FR: Demande d'utiliser le mode Text pour la gestion des messages
void GprsTextModeSMS(){
  mySerial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  // Serial.print( "GprsReadSmsStore for storePos " );
  // Serial.println( SmsStorePos );
  mySerial.print( "AT+CMGR" );
  mySerial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
// FR: efface le contenu de la mémoire tampon des messages du GPRS shield.
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
// FR: interprete le message du GPRS shield et agit en conséquence
void ProcessGprsMsg() {
  Serial.println("");
  Serial.print( "GPRS Message: [" );
  Serial.print( msg );
  Serial.println( "]" );

  if( msg.indexOf( "Call Ready" ) >= 0 ){
     Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }

  // EN: unsolicited message received when getting a SMS message
  // FR: Message non sollicité quand un SMS arrive
  if( msg.indexOf( "AT+CMTI" ) >= 0 ){
     Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     // FR: Rechercher la position de la virgule dans le message complet (+CMTI: "SM",6)
     //     Dans l'exemple, le SMS est stocké à la position 6
     int iPos = msg.indexOf( "," );
     SmsStorePos = msg.substring( iPos+1 );
     Serial.print( "SMS stored at " );
     Serial.println( SmsStorePos );

     // EN: Ask to read the SMS store
     // FR: Demande de lecture du stockage SMS
     GprsReadSmsStore( SmsStorePos );
  }

  // EN: SMS store readed through UART (result of GprsReadSmsStore request)
  // FR: Lecture du stockage SMS via l'UART (résultat de la requete GprsReadSmsStore)
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // get number of sender
    int snPos = msg.indexOf("+1");
    Serial.print("SMS From: ");
    snTmp = msg.substring(snPos+1);
    snFull = "";
    for (int i = 0; i < 11; i++){
      snFull += snTmp[i];
    }
    Serial.println(snFull);

    // EN: Next message will contains the BODY of SMS
    // FR: Le prochain message contiendra le contenu du SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    // FR: Les ligne suivantes sont essentielle pour ne pas effacer le flag!
    ClearGprsMsg();
    return;
  }

  // EN: +CMGR message just before indicate that the following GRPS Shield message
  //     (this message) will contains the SMS body
  // FR: le message +CMGR précédent indiquait que le message suivant du Shield GPRS
  //     (ce message) contient le corps du SMS
  if( SmsContentFlag == 1 ){
    Serial.println( "*** SMS MESSAGE CONTENT ***" );
    Serial.println( msg );
    Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
    delSMS();
  }

  ClearGprsMsg();
  // EN: Always clear the flag
  // FR: Toujours mettre le flag à 0
  SmsContentFlag = 0;
}
void delSMS() {
  mySerial.print("AT+CMGD=1,4");
  mySerial.println(SmsStorePos);
}


thanks!
AdminThanks for entering! Pin
Kevin Priddle19-Mar-15 9:41
professionalKevin Priddle19-Mar-15 9:41 
GeneralRe: Thanks for entering! Pin
Ranjan.D19-Mar-15 14:14
professionalRanjan.D19-Mar-15 14:14 
GeneralExactly what I suggested in your previous article Pin
extremeg17-Mar-15 11:54
extremeg17-Mar-15 11:54 
GeneralMy vote of 5 Pin
Phebous17-Mar-15 9:35
Phebous17-Mar-15 9:35 
GeneralMy vote of 5 Pin
M Rayhan15-Mar-15 23:20
M Rayhan15-Mar-15 23:20 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun15-Mar-15 20:55
Humayun Kabir Mamun15-Mar-15 20:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.