Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

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

4.84/5 (30 votes)
2 Mar 2016CPOL9 min read 157.8K   7.3K  
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)