LTE Cat M1/NB-IoT Shield Hookup Guide

Contributors: jimblom
Favorited Favorite 3

Example 1: Send an SMS

Our next example in the LTE Shield Arduino library demonstrates how to send an SMS. Note that this example does require your SIM card's plan supports outbound SMS text messages -- they may incur a fee, so be mindful of how often you run the sketch.

To load this sketch, navigate to File > Examples > SparkFun LTE Shield Arduino Library > 01_SMS_Send.

Before uploading the code, modify the value of DESTINATION_NUMBER to that of your desired text message destination.

// Set the cell phone number to be texted
String DESTINATION_NUMBER = "11234567890";

Note that the SMS destination number should include the country code (e.g. "1" for US). For example, to text SparkFun (if SparkFun's corporate phones could receive SMS messages) at 303-284-0979, you'd set the DESTINATION_NUMBER string to "13032840979".

Once your destination phone number is set, upload the code and open your serial monitor (9600 baud). In the serial monitor, set the line-ending dropdown to "Newline.".

Sending a text message via the LTE Shield library

Then type a message and send it. After a few seconds you should see a text message appear on your destination phone.

Using the Arduino Library's SMS-Send Functionality

The sendSMS function should be pretty straightforward to use. The function takes two parameters: a String'ed phone number and a String'ed message to send.

LTE_Shield_error_t LTE_Shield::sendSMS(String number, String message);

Consider building a String variable before sending it to the sendSMS function. You can add variables -- including digitalRead()'s and analogRead()'s to the String by using the String() operator. The powerful String object includes easy concatenation with the += operator (or concat()). For example:

String messageToSend;
int time = millis();
messageToSend = "A0 = " + String(analogRead(A0)); // Add A0 to 
messageToSend += "\r\n"; // Create a new line
messageToSend += "Time = " + String(time);
lte.sendSMS(DESTINATION_NUMBER, messageToSend);

The sendSMS function does return an error/success response. Check for a return value of LTE_SHIELD_SUCCESS (set to 0) on success, or a value greater than 0 on an error.