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

Remote Control of LED with MKR GSM 1400

Learn how to control LEDs connected to your board, by sending text messages from your phone.

Introduction

In this tutorial we will learn how to use the Global Mobile Communication System (GSM) to turn ON or OFF an LED, using text messages (SMS). The sketch that we will create for the MKR GSM 1400 board will allow a phone to send a text message to it. If the message matches a string defined in the code, it will either turn the in-built LED on the board, ON or OFF.

Additionally, to receive feedback on whether the operation has worked or not, the board sends a message back to the sender number, to let the phone user know that it worked.

Goals

The goals of this project are:

  • Learn more about using the
    SMS
    class in the
    MKRGSM
    library.
  • Create a SMS receiving functionality for the sketch.
  • Create a string comparison that decides whether to take action or not.
  • Send a feedback message back to the sender.

Hardware & Software Needed

Flexible Connectivity

The MKR GSM 1400 is one of the best options to choose when you want to work with projects in remote areas, but also for urban projects. Since it is capable of connecting to the same network as our phones operate over, it becomes increasingly accessible, and allows a very flexible connectivity approach. As long as there is a radio tower nearby, we can access it from wherever we are, even a thousand miles away.

Other connectivity types, such as Wi-Fi and LoRa®, do not have the same level of effortless access. Wi-Fi, of course is a great option for stationary projects that have access to a Wi-Fi network, LoRa® is great for low power consumption and for extremely remote areas where there is no GSM coverage. But with the MKR GSM 1400, we can, with minimal effort, make it accessible using just our phone!

In this tutorial, we will create a sketch that allows us to send text messages that turn an LED ON or OFF, and replies with a confirmation text message.

Circuit

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

Creating the Program

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 will now write the program for the board. Let's start by opening an empty sketch, and creating a header file called

arduino_secrets.h
that we can store our credentials in. To create a tab in the offline editor, click the arrow symbol underneath the Serial Monitor symbol, and click on the "New tab" option.

Opening a new tab in the offline IDE.
Opening a new tab in the offline IDE.

Then, name the file "arduino_secrets.h".

Renaming new tab in offline IDE.
Renaming new tab in offline IDE.

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

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

Note that if you are using the Web Editor, the

arduino_secrets.h
tab will look a bit different.

4. We can now 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.
  • equals()
    - function that checks if a
    string
    is exactly the same as
    string2
    .
  • sms.beginSMS(number);
    - creates an SMS for a specific number.
  • sms.print(message);
    - prints the content of the SMS.
  • sms.endSMS()
    - sends the SMS.
  • sms.flush()
    - deletes the message from the modem memory.

The sketch can 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
13String message;
14String ledON = "ON";
15String ledOFF = "OFF";
16
17// Array to hold the number a SMS is retrieved from
18char senderNumber[20];
19
20void setup() {
21 // initialize serial communications and wait for port to open:
22 Serial.begin(9600);
23 pinMode(LED_BUILTIN, OUTPUT);
24
25 while (!Serial) {
26 ; // wait for serial port to connect. Needed for native USB port only
27 }
28
29 Serial.println("SMS LED ON/OFF");
30
31 // connection state
32 bool connected = false;
33
34 // Start GSM connection
35 while (!connected) {
36 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
37 connected = true;
38 } else {
39 Serial.println("Not connected");
40 delay(1000);
41 }
42 }
43
44 Serial.println("GSM initialized");
45 Serial.println("Waiting for messages");
46 Serial.println();
47}
48
49void loop() {
50 int c;
51
52 // If there are any SMS available()
53 if (sms.available()) {
54 Serial.println("Message received from:");
55
56 // Get remote number
57 sms.remoteNumber(senderNumber, 20);
58 Serial.println(senderNumber);
59
60 Serial.print("Message: ");
61 // Read message bytes and print them
62 while ((c = sms.read()) != -1) {
63 Serial.print((char)c);
64
65 //print incoming message to the "message" string
66 message += (char)c;
67 }
68
69 //print empty line to separate incoming message from LED status message
70 Serial.println();
71
72 //if incoming message is exactly "ON", turn on LED
73 if (message.equals("ON")) {
74 digitalWrite(LED_BUILTIN, HIGH);
75 Serial.println("LED: ON");
76
77 sms.beginSMS(senderNumber);
78 sms.print("LED has been turned ON!");
79 sms.endSMS();
80 Serial.println("Reply sent!");
81 }
82
83 //if incoming message is exactly "OFF", turn off LED
84 else if (message.equals("OFF")) {
85 digitalWrite(LED_BUILTIN, LOW);
86 Serial.println("LED: OFF");
87
88 sms.beginSMS(senderNumber);
89 sms.print("LED has been turned OFF!");
90 sms.endSMS();
91 Serial.println("Reply sent!");
92 }
93
94 Serial.println("\nEND OF MESSAGE");
95
96 // Delete message from modem memory
97 sms.flush();
98
99 // Clear message string
100 message = "";
101 Serial.println("MESSAGE DELETED");
102 Serial.println();
103 }
104
105 delay(1000);
106
107}

Testing It Out

After the code has been 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:

The offline IDE waiting for messages.
The 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.

In the program, whenever there's an incoming text message, the board reads it and stores it in the

message
string. This string is then compared with two other strings:
"ON"
and
"OFF"
.

  • If we send a text message that contains only

    ON
    , it will trigger a conditional, which turns ON the built-in LED.

  • If we send a text message that contains only

    OFF
    , it will trigger a conditional, which turns OFF the built-in LED.

Additionally, each time the conditional is triggered, it will also send a text message back to the number that originally sent the message. This message is either:

  • LED has been turned ON!
  • LED has been turned OFF!

The events will be visible in the Serial Monitor. Below is an example of sending two text messages: one to turn on the LED, and one to turn it OFF.

Incoming messages, status of LED and reply sent.
Incoming messages, status of LED and reply sent.

As we are sending a text message back, on the phone, we will receive the feedback that the operation has worked properly!

Troubleshoot

Note that the board does not always find a way to connect to the GSM network. There can be several issues behind this.

  • Antenna is not working.
  • SIM card is not working.
  • We're not in range of the GSM network.

We can check out the Scanning available networks tutorial to see if our board is within range of the GSM network.

Conclusion

This tutorial presents a basic, yet powerful way of remotely controlling a MKR GSM 1400 board over the GSM network. We have only created a way of turning ON or OFF an LED, but we can create a wide range of conditionals that can be triggered, depending on what type of messages we sent.

Additionally, by sending a feedback message back to the sender, we are also letting the phone user know that the operation worked. If we don't receive a message, we can assume that it is not working as intended!

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.