Send an SMS with MKR NB 1500

Learn how to use the Serial Monitor to send text messages to different numbers.

Introduction

In this tutorial, we will create a simple sketch that allows us to send a text message, using the MKRNB library.

The message is sent over Narrow Band IoT (NB IoT) or LTE CAT M1 network.

The sketch will be setup to record input from the Serial Monitor, where we can enter a number and a message, and send it.

Note: The Arduino SIM card does not work with this tutorial. A SIM card with a plan from an operator in your country is required.

Goals

The goals of this tutorial are:

  • Create an input for phone number and message.
  • Send a message to a phone over NB IoT or LTE CAT M1 network.

Hardware & Software Needed

NB IoT and LTE CAT M1

LTE-M and NB-IoT are standardized, secure, and operator-managed in licensed spectrum. They are designed for IoT applications that are low cost, use low data rates, require long battery lives and often operate in locations that are hard to reach.

Even though LTE CAT M1 supports voice calls, it was designed for better scalability and wireless broadband. Providing a low speed, low power and long range protocol for the transmission of small amounts of data. LTE CAT M1 supports cellular tower handoffs, so it works with mobile applications

NB-IoT is best suited for simple IoT devices that require small, intermittent data transmissions where latency doesn’t matter. The cellular technology offers long range support and strong signal penetration, so it’s good for long distances, indoor or underground usage. NB-IoT runs in the mobile telephone radio spectrum, and piggybacks on old, unused GSM channels, or free space between LTE channels. NB-IoT can only be used for stationary IoT applications since it doesn’t handle cellular tower handoffs.

Circuit

The circuit for this tutorial is easy: simply attach the dipole antenna to the board.

Simple circuit of the board with antenna.
Simple circuit of the board with antenna.

Programming the Board

In this tutorial, we will use one of the examples from the MKRNB library. The end goal is simple: we want to send a text message from the MKR NB 1500 to a phone number.

1. Let's begin by taking a looking at some of the functions of the sketch we are going to use:

  • NB nbAccess
    - base class for all NB functions.
  • NB_SMS sms
    - base class for all NB functions for SMS.
  • nbAccess.begin(pin)
    - connects to the selected network with the pin number as a parameter, e.g. 0123.
  • sms.beginSMS(number);
    - creates an SMS for a specific number
  • sms.print(message);
    - prints the content of the SMS.
  • sms.endSMS()
    - sends the SMS.

2. We need to 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.

3. 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 MKRNB and install it.

4. After the library is installed, we can now navigate to File > Examples > MKRNB > SendSMS. This will open a new sketch window (or direct you to the sketch if you are using the Web Editor). There will also be a separate tab called

arduino_secrets.h
. Here we will simply fill in the pin number of our SIM card.

Secrets tab in the editor.
Secrets tab in the editor.

Note: A standard pre-paid SIM card typically have 0000 or 1234 as a pin code. This varies from operator to operator,and it is important to find out this before uploading the code. Otherwise, too many unsuccessful attempts may block the SIM card.

We can now upload the sketch to the board. The code can also be found in the snippet below.

1// Include the MKRNB library
2#include <MKRNB.h>
3// #include "arduino_secrets.h"
4// Please enter your sensitive data in the Secret tab or arduino_secrets.h
5// PIN Number
6const char PINNUMBER[] = SECRET_PINNUMBER;
7
8// initialize the library instance
9NB nbAccess;
10NB_SMS sms;
11
12void setup() {
13 // initialize serial communications and wait for port to open:
14 Serial.begin(9600);
15 while (!Serial) {
16 ; // wait for serial port to connect. Needed for native USB port only
17 }
18
19 Serial.println("SMS Messages Sender");
20
21 // connection state
22 bool connected = false;
23
24 // If your SIM has PIN, pass it as a parameter of begin() in quotes
25 while (!connected) {
26 if (nbAccess.begin(PINNUMBER) == NB_READY) {
27 connected = true;
28 } else {
29 Serial.println("Not connected");
30 delay(1000);
31 }
32 }
33
34 Serial.println("NB initialized");
35}
36
37void loop() {
38
39 Serial.print("Enter a mobile number: ");
40 char remoteNum[20]; // telephone number to send sms
41 readSerial(remoteNum);
42 Serial.println(remoteNum);
43
44 // sms text
45 Serial.print("Now, enter SMS content: ");
46 char txtMsg[200];
47 readSerial(txtMsg);
48 Serial.println("SENDING");
49 Serial.println();
50 Serial.println("Message:");
51 Serial.println(txtMsg);
52
53 // send the message
54 sms.beginSMS(remoteNum);
55 sms.print(txtMsg);
56 sms.endSMS();
57 Serial.println("\nCOMPLETE!\n");
58}
59
60//Read input serial
61int readSerial(char result[]) {
62 int i = 0;
63 while (1) {
64 while (Serial.available() > 0) {
65 char inChar = Serial.read();
66 if (inChar == '\n') {
67 result[i] = '\0';
68 Serial.flush();
69 return 0;
70 }
71 if (inChar != '\r') {
72 result[i] = inChar;
73 i++;
74 }
75 }
76 }
77}

Testing It Out

After the sketch has been successfully uploaded to the MKR NB 1500 board, we need to open the Serial Monitor. After we open the Serial Monitor, the program starts, and it will start an attempt to connect to the NB network.

If unsuccessful, it will print

"Not connected"
in the Serial Monitor. Otherwise, it will print,
"NB initialized"
.

Enter a phone number.
Enter a phone number.

If we get the latter, we can proceed by entering a phone number we want to send a SMS to and hit enter. We will now be asked to write our message. We can write something simple, such as:

1Hello, this is NB 1500 speaking. Can you read?

We can of course be a bit more creative.. But we are just testing things out!

Once we send this message, if it is successful, we will see the message

"COMPLETE!"
in the Serial Monitor.

Sending the message
Sending the message

Now, let's take a look at our phone and see if it worked. We should have received the exact same text message.

Message received on the phone.
Message received on the phone.

Congratulations, you have now managed to connect your MKR NB 1500 board to a network, and successfully sent an SMS to your phone.

Troubleshoot

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

  • We have not installed the MKRNB library.
  • We have entered the wrong pin number.
  • We are out of coverage (no signal).
  • We have entered the wrong number.
  • SIM card may not be activated.

Conclusion

In this tutorial, we have used the simple SendSMS example from the MKRNB library. We have explored a little bit about the features of the different networks, and finally, we have sent an SMS from our MKR NB 1500 board to a phone number.

Feel free to explore the MKRNB 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.