Receive SMS
Read SMS messages and prompt them to the Serial Monitor.
This sketch waits for an SMS message and prints it to the serial monitor. It uses the GSM library of the Arduino GSM Shield and an active SIM card. To operate, the SIM card doesn't need a data plan.
Hardware Required
- Arduino Board 
- SIM card 
Circuit
 
  
    
    
Code
First, import the GSM library
#include <GSM.h>SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :
#define PINNUMBER ""Initialize instances of the classes you're going to use. You're going to need both the GSM and GSM_SMS class.
1GSM gsmAccess;2
3GSM_SMS sms;Create a
charchar remoteNumber[20];In
setup1void setup(){2
3  Serial.begin(9600);4
5  Serial.println("SMS Messages Receiver");Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :
1boolean notConnected = true;Connect to the network by calling
gsmAccess.begin()while()gsmAccess()GSM_READYnotConnectedtruefalsesetup1while(notConnected)2
3  {4
5    if(gsmAccess.begin(PINNUMBER)==GSM_READY)6
7      notConnected = false;8
9    else10
11    {12
13      Serial.println("Not connected");14
15      delay(1000);16
17    }18
19  }Finish
setup1Serial.println("GSM initialized.");2
3  Serial.println("Waiting for messages");4}SMS messages are received by the modem. SIM cards have some memory space to store incoming SMS. The number of SMS the card can hold can be as few as 10, or as many as 200, depending on the SIM. You should check with your provider to determine how many your SIM can keep in memory.
In
loop()charsms.available()1void loop()2{3
4  char c;5
6  if (sms.available())7
8  {If a SMS is available, retrieve the remote sender's number by calling
sms.remoteNumber(remoteNumber, 20)remoteNumberchar1Serial.println("Message received from:");2
3    sms.remoteNumber(remoteNumber, 20);4
5    Serial.println(remoteNumber);It's possible to delete SMS messages by calling
sms.flush()sms.peek()The code below won't remove any from the SIM, but you could iterate through a
for1if(sms.peek()=='#')2
3    {4
5      Serial.println("Discarded SMS");6
7      sms.flush();8
9    }To read a message, use
sms.read()c1while(c=sms.read())2
3      Serial.print(c);Indicate the message is complete and remove it from memory with
sms.flush()1Serial.println("\nEND OF MESSAGE");2
3    sms.flush();4
5    Serial.println("MESSAGE DELETED");6
7  }Add a brief delay and close the
loop1delay(1000);2}Once your code is uploaded, open the serial monitor. With a phone, or other SMS enabled service, send a SMS to the number of your SIM. You should see the message print out on screen when it is received.
Complete Sketch
The complete sketch is below.
1/*2
3 SMS receiver4
5 This sketch, for the Arduino GSM shield, waits for a SMS message6
7 and displays it through the Serial port.8
9 Circuit:10
11 * GSM shield attached to and Arduino12
13 * SIM card that can receive SMS messages14
15 created 25 Feb 201216
17 by Javier Zorzano / TD18
19 This example is in the public domain.20
21 http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveSMS22
23*/24
25// include the GSM library26#include <GSM.h>27
28// PIN Number for the SIM29#define PINNUMBER ""30
31// initialize the library instances32
33GSM gsmAccess;34
35GSM_SMS sms;36
37// Array to hold the number a SMS is retrieved from38char senderNumber[20];39
40void setup() {41
42  // initialize serial communications and wait for port to open:43
44  Serial.begin(9600);45
46  while (!Serial) {47
48    ; // wait for serial port to connect. Needed for native USB port only49
50  }51
52  Serial.println("SMS Messages Receiver");53
54  // connection state55
56  bool notConnected = true;57
58  // Start GSM connection59
60  while (notConnected) {61
62    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {63
64      notConnected = false;65
66    } else {67
68      Serial.println("Not connected");69
70      delay(1000);71
72    }73
74  }75
76  Serial.println("GSM initialized");77
78  Serial.println("Waiting for messages");79}80
81void loop() {82
83  char c;84
85  // If there are any SMSs available()86
87  if (sms.available()) {88
89    Serial.println("Message received from:");90
91    // Get remote number92
93    sms.remoteNumber(senderNumber, 20);94
95    Serial.println(senderNumber);96
97    // An example of message disposal98
99    // Any messages starting with # should be discarded100
101    if (sms.peek() == '#') {102
103      Serial.println("Discarded SMS");104
105      sms.flush();106
107    }108
109    // Read message bytes and print them110
111    while (c = sms.read()) {112
113      Serial.print(c);114
115    }116
117    Serial.println("\nEND OF MESSAGE");118
119    // Delete message from modem memory120
121    sms.flush();122
123    Serial.println("MESSAGE DELETED");124
125  }126
127  delay(1000);128
129}Last revision 2018/08/23 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.