Send, Receive And Delete SMS With IoT Devices (Arduino and GSM Shield)

Please note, I am using a 2G Modem that means it works only with 2G compatible SIM card. That 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.

Note: The front end .NET WinForm Application was originally coded by Syeda Anila Nusrat. We will be reusing the most and do some small enhancements and code refactoring.

Send and Read SMS through a GSM Modem using AT Commands

The following is the snapshot of the GSM SIM 900 Shield with Arduino Uno,

GSM SIM 900 Shield with Arduino Uno
                                 Figure 1: GSM SIM 900 Shield with Arduino Uno

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 that means it only works with 2G compatible GSM Sim cards.

  3. SIM 900 Library for Arduino – You can download.

    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.

Coding the sample application

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 following 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.

The following 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 snippet for Arduino GSM/GPRS Shield Code.

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 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. Finally we will clear the buffer and reset the count back to zero.
    1. //Serial Relay - Arduino will patch a   
    2. //serial link between the computer and the GPRS Shields  
    3. //at 9600 bps 8-N-1  
    4. //Computer is connected to Hardware UART  
    5. //GPRS Shield is connected to the Software UART   
    6. #  
    7. include < SoftwareSerial.h > SoftwareSerial GPRS(7, 8);  
    8. unsigned char buffer[64]; // buffer array for data recieve over serial port  
    9. int count = 0; // counter for buffer array   
    10. void setup()  
    11. {  
    12.     GPRS.begin(9600); // the GPRS baud rate   
    13.     Serial.begin(9600); // the Serial port of Arduino baud rate.   
    14. }  
    15. void loop()  
    16. {  
    17.     if (GPRS.available()) // if date is comming from softwareserial port ==> data is comming from gprs shield  
    18.     {  
    19.         while (GPRS.available()) // reading data into char array   
    20.         {  
    21.             buffer[count++] = GPRS.read(); // writing data into array  
    22.             if (count == 64) break;  
    23.         }  
    24.         Serial.write(buffer, count); // if no data transmission ends, write buffer to hardware serial port  
    25.         clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array  
    26.         count = 0; // set counter of while loop to zero  
    27.     }  
    28.     if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook  
    29.         GPRS.write(Serial.read()); // write it to the GPRS shield  
    30. }  
    31. void clearBufferArray() // function to clear buffer array  
    32.     {  
    33.         for (int i = 0; i < count; i++)  
    34.         {  
    35.             buffer[i] = NULL;  
    36.         } // clear all index of array with command NULL  
    37.     }  

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.

AT Commands and responding
                                                                        Figure 2: AT Commands and responding

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.

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

AT command
                                                               Figure 3: AT command

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.

The following 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.

  1. string[] ports = SerialPort.GetPortNames();  
  2. // Add all port names to the combo box:  
  3. foreach (string port in ports)  
  4. {  
  5.    this.cboPortName.Items.Add(port);  
  6. }  
Next we see how to open a serial connection. In the UI, you see a “Connect” button. Clicking that triggers the following code. You can see below, we are making use of SMSHelper for opening the COM port with the specified COM port name, baud rate, data bits, etc.

Port Setting
                                                               Figure 4: Port Setting
  1. private void btnOK_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         //Open communication port   
  6.         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));  
  7.         if (this.port != null)  
  8.         {  
  9.             this.gboPortSettings.Enabled = false;  
  10.             this.statusBar1.Text = "Modem is connected at PORT " + this.cboPortName.Text;  
  11.             // Add tab pages  
  12.             // Code for adding tabs goes here  
  13.         }  
  14.         else  
  15.         {  
  16.             //MessageBox.Show("Invalid port settings");  
  17.             this.statusBar1.Text = "Invalid port settings";  
  18.         }  
  19.     }  
  20.     catch (Exception ex)  
  21.     {  
  22.         ErrorLog(ex.Message);  
  23.     }  
  24. }  
Now we take a look into the understanding on how to open and close serial com port by using System.IO.Ports. SerialPort class. The following 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 following code snippet, on data received event you will see that when we received some data, we can set the wait handle event signalling 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.
  1. public SerialPort OpenPort(string portName, int baudRate, int dataBits, int readTimeout, int writeTimeout)  
  2. {  
  3.     receiveNow = new AutoResetEvent(false);  
  4.     SerialPort port = new SerialPort();  
  5.     try  
  6.     {  
  7.         port.PortName = portName; //COM1  
  8.         port.BaudRate = baudRate; //9600  
  9.         port.DataBits = dataBits; //8  
  10.         port.StopBits = StopBits.One; //1  
  11.         port.Parity = Parity.None; //None  
  12.         port.ReadTimeout = readTimeout; //300  
  13.         port.WriteTimeout = writeTimeout; //300  
  14.         port.Encoding = Encoding.GetEncoding("iso-8859-1");  
  15.         port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);  
  16.         port.Open();  
  17.         port.DtrEnable = true;  
  18.         port.RtsEnable = true;  
  19.     }  
  20.     catch (Exception ex)  
  21.     {  
  22.         throw ex;  
  23.     }  
  24.     return port;  
  25. }  
  26. public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  27.     {  
  28.         try  
  29.         {  
  30.             if (e.EventType == SerialData.Chars)  
  31.             {  
  32.                 receiveNow.Set();  
  33.             }  
  34.         }  
  35.         catch (Exception ex)  
  36.         {  
  37.             throw ex;  
  38.         }  
  39.     }  
  40.     //Close Port  
  41. public void ClosePort(SerialPort port)  
  42. {  
  43.     try  
  44.     {  
  45.         port.Close();  
  46.         port.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);  
  47.         port = null;  
  48.     }  
  49.     catch (Exception ex)  
  50.     {  
  51.         throw ex;  
  52.     }  
  53. }  
Send/Execute AT Command

The following 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.
  1. public string SendATCommand(SerialPort port, string command, int responseTimeout, string errorMessage)  
  2. {  
  3.     try  
  4.     {  
  5.         port.DiscardOutBuffer();  
  6.         port.DiscardInBuffer();  
  7.         receiveNow.Reset();  
  8.         port.Write(command + "\r");  
  9.         string input = ReadResponse(port, responseTimeout);  
  10.         if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n")))) throw new ApplicationException("No success message was received.");  
  11.         return input;  
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.         throw ex;  
  16.     }  
  17. }  
Reading AT Command Response

The following 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 that 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.
  1. public string ReadResponse(SerialPort port, int timeout)  
  2. {  
  3.     string serialPortData = string.Empty;  
  4.     try  
  5.     {  
  6.         do {  
  7.             if (receiveNow.WaitOne(timeout, false))  
  8.             {  
  9.                 string data = port.ReadExisting();  
  10.                 serialPortData += data;  
  11.             }  
  12.             else  
  13.             {  
  14.                 if (serialPortData.Length > 0) throw new ApplicationException("Response received is incomplete.");  
  15.                 else throw new ApplicationException("No data received from phone.");  
  16.             }  
  17.         }  
  18.         while (!serialPortData.EndsWith("\r\nOK\r\n") && !serialPortData.EndsWith("\r\n> ") && !serialPortData.EndsWith("\r\nERROR\r\n"));  
  19.     }  
  20.     catch (Exception ex)  
  21.     {  
  22.         throw ex;  
  23.     }  
  24.     return serialPortData;  
  25. }  
Sending SMS

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

SMS Application
                                                            Figure 5: SMS Application

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.

  1. public bool SendMessage(SerialPort port, string phoneNo, string message)  
  2. {  
  3.     bool isSend = false;  
  4.     try  
  5.     {  
  6.         string recievedData = SendATCommand(port, "AT", 300, "No phone connected");  
  7.         string command = "AT+CMGF=1" + char.ConvertFromUtf32(13);  
  8.         recievedData = SendATCommand(port, command, 300, "Failed to set message format.");  
  9.         // AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/  
  10.         command = "AT+CMGS=\"" + phoneNo + "\"" + char.ConvertFromUtf32(13);  
  11.         recievedData = SendATCommand(port, command, 300, "Failed to accept phoneNo");  
  12.         command = message + char.ConvertFromUtf32(26);  
  13.         recievedData = SendATCommand(port, command, 3000, "Failed to send message"); //3 seconds  
  14.         if (recievedData.EndsWith("\r\nOK\r\n")) isSend = true;  
  15.         else if (recievedData.Contains("ERROR")) isSend = false;  
  16.         return isSend;  
  17.     }  
  18.     catch (Exception ex)  
  19.     {  
  20.         throw ex;  
  21.     }  
  22. }  
The following 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.

Text visualize
                                                         Figure 6: Text visualize

Reading SMS Messages

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

Read SMS
                                                            Figure 7: Read SMS

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 retuning back to the caller.

  1. public ShortMessageCollection ReadSMS(SerialPort port, string atCommand)  
  2. {  
  3.     // Set up the phone and read the messages  
  4.     ShortMessageCollection messages = null;  
  5.     try  
  6.     {#  
  7.         region Execute Command  
  8.         // Check connection  
  9.         SendATCommand(port, "AT", 300, "No phone connected");  
  10.         // Use message format "Text mode"  
  11.         SendATCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");  
  12.         // Read the messages  
  13.         string input = SendATCommand(port, atCommand, 5000, "Failed to read the messages.");#  
  14.         endregion# region Parse messages  
  15.         messages = ParseMessages(input);#  
  16.         endregion  
  17.     }  
  18.     catch (Exception ex)  
  19.     {  
  20.         throw ex;  
  21.     }  
  22.     if (messages != nullreturn messages;  
  23.     else return null;  
  24. }  
The following 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.
  1. public ShortMessageCollection ParseMessages(string input)  
  2. {  
  3.     ShortMessageCollection messages = new ShortMessageCollection();  
  4.     try  
  5.     {  
  6.         Regex r = new Regex(@"\+CMGL: (\d+),"  
  7.             "(.+)"  
  8.             ","  
  9.             "(.+)"  
  10.             ",(.*),"  
  11.             "(.+)"  
  12.             "\r\n(.+)\r\n");  
  13.         Match m = r.Match(input);  
  14.         while (m.Success)  
  15.         {  
  16.             ShortMessage msg = new ShortMessage();  
  17.             msg.Index = m.Groups[1].Value;  
  18.             msg.Status = m.Groups[2].Value;  
  19.             msg.Sender = m.Groups[3].Value;  
  20.             msg.Alphabet = m.Groups[4].Value;  
  21.             msg.Sent = m.Groups[5].Value;  
  22.             msg.Message = m.Groups[6].Value;  
  23.             messages.Add(msg);  
  24.             m = m.NextMatch();  
  25.         }  
  26.     }  
  27.     catch (Exception ex)  
  28.     {  
  29.         throw ex;  
  30.     }  
  31.     return messages;  
  32. }  
Delete SMS Message

Let us see how we can remove or delete message by index or delete all message (All or Read) ones. The following 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.

+CMGD=index[,flag]

In order to delete all SMS, the following 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
  1. public bool DeleteMessage(SerialPort port, string atCommand)  
  2. {  
  3.     bool isDeleted = false;  
  4.     try  
  5.     {#  
  6.         region Execute Command  
  7.         string recievedData = SendATCommand(port, "AT", 300, "No phone connected");  
  8.         recievedData = SendATCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");  
  9.         String command = atCommand;  
  10.         recievedData = SendATCommand(port, command, 300, "Failed to delete message");#  
  11.         endregion  
  12.         if (recievedData.EndsWith("\r\nOK\r\n"))  
  13.         {  
  14.             isDeleted = true;  
  15.         }  
  16.         if (recievedData.Contains("ERROR"))  
  17.         {  
  18.             isDeleted = false;  
  19.         }  
  20.         return isDeleted;  
  21.     }  
  22.     catch (Exception ex)  
  23.     {  
  24.         throw ex;  
  25.     }  
  26. }  
The following is the snapshot of the Delete SMS tab

Delete SMS
                                                                     Figure 8: Delete SMS

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 applications that you can think off in using the SMS functionality. Of course, this is not the only solution but it is a cost effective one as you really don't have to worry about third party services for sending SMS. There are times when the IOT devices can function based on receiving some specific message, in such scenarios you can definitely make use of this solution.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now