Send SMS messages from an Arduino Yún


We'll show you how to get your Arduino Yún to send SMS with Twilio. Then your Arduino can text whenever it's running late.

This sketch uses our Twilio > SMSMessages > SendSMS Choreo.

Get Set Up

1Make sure you have a Temboo account. If you don't already have one, you can register for free.

2You'll also need a Twilio account, which you can create for free here.

3The sketch needs your Twilio phone number, along with the Account SID and Auth Token you get when you register with Twilio. Make sure to use the Account SID and Auth Token from your Twilio Console Dashboard, shown below, as opposed to the test credentials from the Dev Tools panel.

The Twilio Console Dashboard

A Twilio Account SID and Auth Token in the Twilio Console Dashboard

4When using a free Twilio account, you'll need to verify the phone number to which messages are being sent by going to Twilio and following the instructions under the "Numbers > Verified Caller IDs" tab.

5Make sure that you have the latest version of the Arduino IDE. You should also be sure that you have the newest version of the Temboo Library by checking the Arduino Library Manager

6Make sure that your Yún is connected to the Internet. Arduino has a helpful guide if you need assistance.

Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the SendSMS Choreo, and you will need to replace the placeholder values in the code with your own Twilio account details.

/*
  SendAnSMS

  Demonstrates sending an SMS via a Twilio account using the Temboo Arduino Yun SDK.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// the Account SID from your Twilio account
const String TWILIO_ACCOUNT_SID = "xxxxxxxxxx";

// the Auth Token from your Twilio account
const String TWILIO_AUTH_TOKEN = "xxxxxxxxxx";

// your Twilio phone number, e.g., "+1 555-222-1212"
const String TWILIO_NUMBER = "xxxxxxxxxx";

// the number to which the SMS should be sent, e.g., "+1 555-222-1212"
const String RECIPIENT_NUMBER = "xxxxxxxxxx";

boolean success = false; // a flag to indicate whether we've sent the SMS yet or not

void setup() {
  Serial.begin(9600);

  // for debugging, wait until a serial console is connected
  delay(4000);
  while(!Serial);

  Bridge.begin();
}

void loop()
{
  // only try to send the SMS if we haven't already sent it successfully
  if (!success) {

    Serial.println("Running SendAnSMS...");
    
    // we need a Process object to send a Choreo request to Temboo
    TembooChoreo SendSMSChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    SendSMSChoreo.begin();
    
    // set Temboo account credentials
    SendSMSChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendSMSChoreo.setAppKey(TEMBOO_APP_KEY);

    // identify the Temboo Library choreo to run (Twilio > SMSMessages > SendSMS)
    SendSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS");

    // set the required choreo inputs
    // see https://www.temboo.com/library/Library/Twilio/SMSMessages/SendSMS/ 
    // for complete details about the inputs for this Choreo

    // the first input is a your AccountSID
    SendSMSChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
    
    // next is your Auth Token
    SendSMSChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
 
    // next is your Twilio phone number
    SendSMSChoreo.addInput("From", TWILIO_NUMBER);
    
    // next, what number to send the SMS to
    SendSMSChoreo.addInput("To", RECIPIENT_NUMBER);

    // finally, the text of the message to send
    SendSMSChoreo.addInput("Body", "Hey, there! This is a message from your Arduino Yun!");
    
    // tell the Process to run and wait for the results. The 
    // return code (returnCode) will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = SendSMSChoreo.run();

    // a return code of zero (0) means everything worked
    if (returnCode == 0) {
        Serial.println("Success! SMS sent!");
        success = true;
    } else {
      // a non-zero return code means there was an error
      // read and print the error message
      while (SendSMSChoreo.available()) {
        char c = SendSMSChoreo.read();
        Serial.print(c);
      }
    } 
    SendSMSChoreo.close();

    // do nothing for the next 60 seconds
    Serial.println("Waiting...");
    delay(60000);
  }
}

Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Copy the code snippet into a new tab in Arduino and call it TembooAccount.h.

#define TEMBOO_ACCOUNT "ACCOUNT_NAME"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "APP_NAME"  // your Temboo app key name
#define TEMBOO_APP_KEY  "APP_KEY"  // your Temboo app key

With both files in place you are ready to upload the sketch and send an SMS from your Yún. Text away!

Convert the sketch to work with the Yún Shield

If you're working with the Yún Shield paired with another Arduino board, you'll need to make some small changes to the Yún sketch above it so that it's compatible with the Arduino Yún Shield.

1First, change the include statement #include <Temboo.h> to #include <TembooYunShield.h>. Your include statements should look like this:

#include <Bridge.h>
#include <TembooYunShield.h>
#include "TembooAccount.h" 

2Next, change the Temboo object name from TembooChoreo to TembooYunShieldChoreo. Your code should look like this:

TembooYunShieldChoreo  SendSMSChoreo;

3Now your code is ready to run on your Yún Shield!

What's Next?

Now that you've mastered SMS, why not check out the rest of the 2000+ Choreos in our Library and start planning your next Yún project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back