Click here to Skip to main content
15,867,330 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 152.5K   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

 
QuestionHello Pin
Member 1552438211-Feb-22 17:51
Member 1552438211-Feb-22 17:51 
QuestionPublic bool SendSms does not return expected string Pin
vinsanity6830-Oct-20 18:11
vinsanity6830-Oct-20 18:11 
QuestionResponse Received is Incomplete. Pin
Member 1470510617-Mar-20 19:46
Member 1470510617-Mar-20 19:46 
Questionhow to request ussd Pin
Member 1420652131-Mar-19 14:23
Member 1420652131-Mar-19 14:23 
SuggestionSome Issues and recommendation Pin
Al-Rehman24-Mar-19 12:42
Al-Rehman24-Mar-19 12:42 
QuestionHow to store sms Delivery Notification report to sim storage Pin
Ganesht_200030-Aug-18 0:22
Ganesht_200030-Aug-18 0:22 
QuestionITegno 3800 Pin
lah_tacorda4-Jun-18 23:18
lah_tacorda4-Jun-18 23:18 
QuestionMessage: Response received is incomplete Pin
tony_hariono28-Mar-18 19:27
tony_hariono28-Mar-18 19:27 
Questionpls any one tell me how to read content from sms GSM module using Arduino Pin
Member 1316364130-Apr-17 22:08
Member 1316364130-Apr-17 22:08 
PraiseWow Pin
Olivia9519-Jun-16 7:38
professionalOlivia9519-Jun-16 7:38 
GeneralRe: Wow Pin
Ranjan.D21-Jun-16 1:03
professionalRanjan.D21-Jun-16 1:03 
QuestionArduino and GSM SIM 900 only? Pin
starapple130-Apr-16 6:16
starapple130-Apr-16 6:16 
AnswerRe: Arduino and GSM SIM 900 only? Pin
Ranjan.D1-May-16 4:59
professionalRanjan.D1-May-16 4:59 
GeneralRe: Arduino and GSM SIM 900 only? Pin
starapple11-May-16 7:48
starapple11-May-16 7:48 
Questionhardware uart Pin
Member 1185434321-Jul-15 22:42
Member 1185434321-Jul-15 22:42 
AnswerRe: hardware uart Pin
Ranjan.D26-Jul-15 2:38
professionalRanjan.D26-Jul-15 2:38 
Questionsde Pin
Hasan Syed (Zubeshi)27-Jun-15 0:48
Hasan Syed (Zubeshi)27-Jun-15 0:48 
Questionwavecom q24plus Pin
chuchufuentes26-Jun-15 10:12
chuchufuentes26-Jun-15 10:12 
AnswerRe: wavecom q24plus Pin
Ranjan.D26-Jun-15 14:31
professionalRanjan.D26-Jun-15 14:31 
QuestionIf I have working GSM modem + SIM Card, is normal rate for SMS will be applied to my SIM Card? Pin
Devonius24-Jun-15 21:52
Devonius24-Jun-15 21:52 
AnswerRe: If I have working GSM modem + SIM Card, is normal rate for SMS will be applied to my SIM Card? Pin
Ranjan.D25-Jun-15 3:46
professionalRanjan.D25-Jun-15 3:46 
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 

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.