This tutorial refers to a product that has reached its end-of-life status.

Receive an SMS with MKR GSM 1400

Learn how to setup your board to print incoming text messages in the Serial Monitor.

Introduction

This tutorial is a continuation of the Send an SMS with MKR GSM 1400 tutorial, where you can read more about how the Global System for Mobile Communication (GSM) works. In the previous tutorial we set up the MKR GSM 1400 to send a text message to a phone, and in this tutorial, we will also see how we can receive text messages!

Goals

The goals of this project are:

  • Receive an SMS using the GSM network.
  • Print the message and sender in the Serial Monitor.

Hardware & Software Needed

Useful Scenarios for Receiving SMS

The use of GSM technology is gaining more and more popularity, as it is gaining more coverage around the world. Practically speaking, wherever your phone has coverage, the MKR GSM 1400 has coverage too. This of course varies depending on what type of antenna you use. But often enough, even in a remote countryside, mountain and generally desolate places, we have coverage. We might not be able to stream movies or download large files, but we are able to send and receive messages, either over Internet or through calls or text messages.

If we manage to get access to one radio tower, it means we can call or text anyone in the country we live in, and, of course, the rest of world (this may be quite expensive however). This opens up many possibilities for projects in both rural and urban areas, when we either need to be updated, or to update something but we can't physically access it.

Now, in this tutorial, we will simply see how we can use a smartphone (or regular phone) to send a text message to our MKR GSM 1400 board, and print the message in the Serial Monitor.

Circuit

A simple circuit with the board and antenna.
A simple circuit with the board and antenna.

Programming the Board

We will now get to the programming part of this tutorial.

1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for MKRGSM and install it.

3. We can now go to File > Examples > MKRGSM > ReceiveSMS in the editor. This will open a new window, which has a sketch tab, but also a header file, called

arduino_secrets.h
. Inside this file, we need to enter our pin number between the " ".

1#define SECRET_PINNUMBER "" //enter pin code between ""

The pin number is often 1234 or 0000, but for more information, check the SIM plan that you bought.

4. Let's take a look at some of the core functions of this sketch:

  • GSM gsmAccess
    - base class for all GSM functions.
  • GSM_SMS sms
    - base class for all GSM functions for SMS.
  • gsmAccess.begin(pin)
    - connects to the GSM network with the pin number as a parameter, e.g. 0123.
  • sms.available()
    - checks to see if there is a SMS messages on the SIM card to be read.
  • sms.remoteNumber(number, 20)
    - retrieves a sender's number.
  • sms.beginSMS(number);
    - creates an SMS for a specific number.
  • sms.endSMS()
    - sends the SMS.
  • sms.flush()
    - deletes the message from the modem memory.

The sketch can also be found in the snippet below. Select the right board and port, and upload the sketch to the board.

1// include the GSM library
2#include <MKRGSM.h>
3
4#include "arduino_secrets.h"
5// Please enter your sensitive data in the Secret tab or arduino_secrets.h
6// PIN Number
7const char PINNUMBER[] = SECRET_PINNUMBER;
8
9// initialize the library instances
10GSM gsmAccess;
11GSM_SMS sms;
12
13// Array to hold the number a SMS is retrieved from
14char senderNumber[20];
15
16void setup() {
17 // initialize serial communications and wait for port to open:
18 Serial.begin(9600);
19 while (!Serial) {
20 ; // wait for serial port to connect. Needed for native USB port only
21 }
22
23 Serial.println("SMS Messages Receiver");
24
25 // connection state
26 bool connected = false;
27
28 // Start GSM connection
29 while (!connected) {
30 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
31 connected = true;
32 } else {
33 Serial.println("Not connected");
34 delay(1000);
35 }
36 }
37
38 Serial.println("GSM initialized");
39 Serial.println("Waiting for messages");
40}
41
42void loop() {
43 int c;
44
45 // If there are any SMS available()
46 if (sms.available()) {
47 Serial.println("Message received from:");
48
49 // Get remote number
50 sms.remoteNumber(senderNumber, 20);
51 Serial.println(senderNumber);
52
53 // An example of message disposal
54 // Any messages starting with # should be discarded
55 if (sms.peek() == '#') {
56 Serial.println("Discarded SMS");
57 sms.flush();
58 }
59
60 // Read message bytes and print them
61 while ((c = sms.read()) != -1) {
62 Serial.print((char)c);
63 }
64
65 Serial.println("\nEND OF MESSAGE");
66
67 // Delete message from modem memory
68 sms.flush();
69 Serial.println("MESSAGE DELETED");
70 }
71
72 delay(1000);
73
74}

Testing It Out

After the code has successfully uploaded, we need to open the Serial Monitor to initialize the rest of the program. For these type of projects, we use the

while(!Serial)
command, so we can read any available information only after we open the Serial Monitor.

After we open the Serial Monitor, the board will attempt to connect to the GSM network, and if it is successful, the following message can be seen in the Serial Monitor:

Offline IDE waiting for messages.
Offline IDE waiting for messages.

This means that we are ready to receive text messages. Now, we can open our phone, and send a text message to the number attached to your SIM card.

We can send something simple, such as:

1Hi there GSM 1400, can you read?

After we have sent the message, it should appear in the Serial Monitor after a few seconds. If it successful, we will see the following message printed:

SMS received, printed in the Serial Monitor.
SMS received, printed in the Serial Monitor.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • Wrong pin number.
  • SIM card lacking a data plan.
  • Out of GSM network range (very unlikely in urban areas).
  • Antenna not attached properly.
  • We have sent the message to the wrong number.

Conclusion

In this tutorial, we have used the

ReceiveSMS
example, which allows us to receive SMS from an external device, and print it in the Serial Monitor.

Feel free to explore the MKRGSM library further, and try out some of the many cool functions in this library.

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.