Send SMS Messages Using Twilio* on UP Squared* with Ubuntu*

Introduction

This document demonstrates how to send Short Message Service (SMS) messages to your phone using a Twilio* phone number on the UP Squared* board with Ubuntu* when the light sensor indicates it is getting dark. Sending SMS messages using Twilio is expected to work on any IoT device that supports Intel microcontrollers running on Ubuntu.

Twilio allows software developers to programmatically receive SMS and Multimedia Messaging Service (MMS) messages to their Twilio phone numbers and reply back with SMS and MMS messages. You can send SMS and MMS messages using Twilio’s REST API, track SMS conversations, and send SMS messages during phone calls.

About the UP Squared* Board

The UP Squared board is a low-power and high performance platform ideal for Internet of Things (IoT) applications. Depending on your performance requirements, you can purchase an UP Squared board based on either the Intel® Celeron® processor (N3350) or Intel® Pentium® processor (N4200). For more information, visit the official UP Squared site.

Gather Your Materials

Hardware

The hardware components used in this project are listed below:

For detailed assembly and instructions for powering on the UP Squared board, check out the UP Squared Get Started Guide at the Intel® Developer Zone.

image of UP Squared board hardware
Figure 1: Hardware

Create a Twilio* Account

To receive SMS and MMS messages, you need an SMS-enabled Twilio phone number. To get your own dedicated phone number, sign up for a Twilio trial account. Once SMS-enabled Twilio access is granted, go to your account dashboard to find your Twilio Account SID and Twilio Auth Token.

screenshot of Ubuntu dashboard
Figure 2: Twilio Credentials from Account Dashboard

Install the Ubuntu* Operating System

Before you begin, the Ubuntu* operating system should be installed on the UP Squared platform. For this guide, the Ubuntu 16.04.1 LTS 64 bit desktop was used. For more information on how to install the Ubuntu operating system, visit https://wiki.up-community.org/Ubuntu.

When you boot the Up Squared board after Ubuntu desktop was installed, a menu will appear with options to boot either Ubuntu or other operating system. Ubuntu is highlighted and will be executed by default.

Install Twilio*

  1. To ensure that the Ubuntu operating system is up to date and dependent Ubuntu packages are installed, open a command prompt (terminal) and type:

    sudo apt-get update
  2. Install the Node Version Manager (NPM) package:

    sudo apt-get install npm
  3. Install the Twilio node package from NPM:

    npm install twilio

Install Ubuntu* Kernel

The default kernel in the Ubuntu Core doesn’t give access to the GPIO pins. Install the required kernel below to have access to the GPIO pins.

  1. Before updating the kernel, use the following command to check whether you have the right kernel for Up Squared.

    uname -srv

    The kernel for Up Squared should look like this:

    Linux 4.10.0-42-generic #5000~upboard9-Ubuntu SMP Tue Dec 12 11:46:16 UTC 2017
  2. Add the repository:

    sudo add-apt-repository ppa:ubilinux/up
  3. Update the repository list:

    sudo apt update
  4. Remove the generic installed kernel:

    sudo apt-get autoremove --purge ‘linux-.*generic’
  5. Install the kernel for Up Squared:

    sudo apt-get install linux-image-generic-hwe-16.04-upboard
  6. Reboot:

    sudo reboot
  7. Verify that the kernel was installed:

    uname -srv

Install MRAA and sensor repository packages

Install the MRAA package to manage IO communications:

sudo npm install -g mraa
sudo npm install jsupm_grove

Read Light Sensor

To interface with the Grove shield, add this line into your code:

mraa.addSubplatform(mraa.GROVEPI, "0 ")

Using GROVEPI will shift all the pin numbers by 512, therefore, pin A1 for the light sensor will be pin 512 + 1:

var OFFSET = 512

// Light sensor is connected to the analog A1
var light = new groveSensor.GroveLight(OFFSET + 1);

Read the light sensor value from the analog pin:

// Load Grove module
var groveSensor = require('jsupm_grove');

// Light sensor is connected to the analog A1
var light = new groveSensor.GroveLight(OFFSET + 1);
var lightValue = light.value();

If the light sensor indicates it is getting dark, this will turn on the LED and send an SMS message:

if(light.value() < 10) {
        // Turn off the LED
        ledPin.write(1);

        // Send an SMS message
…
}

And finally, use the following code to read and write to digital pin D5 to turn on the LED:

// LED is connected to the digital D5
var ledPin = new mraa
ledPin.dir(mraa.DIR_OUT);
// Turn on the LED
ledPin.write(1);

Send an Outbound SMS Message

To send a SMS message, we make a request to Twilio and specify the “to”, “from”, and “body”. The “to” is your mobile phone number, the “from” is the SMS-enabled Twilio phone number, and the “body” is the SMS message being sent. See the Twilio API documentation for Twilio client error codes.

// Twilio Account SID and Auth token
var ACCOUNT_SID[] = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var ACCOUNT_TOKEN[] = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ";

// SMS-enabled Twilio phone number
var twilio_number = "1480405xxxx";

// Your mobile phone number
var your_number = “1480xxxxxx”;

// Load the Twilio module
var twilio = require('twilio');

// Make authenticated requests
var client = new twilio(ACCOUNT_SID, ACCOUNT_TOKEN);

// Send the request to Twilio
message = client.messages.create({from: TWILIO_NUMBER,
                                                                to: YOUR_NUMBER,
                                                                body: "Hello World!!!"},
    function(error, message) {
        if (!error) {
            console.log(“The request was successful.”);
            console.log(message.Sid);
        } else {
            console.log(“The request was failed. Error code:”);
            console.log(message.ErrorCode);
            console.log(message.sid);
        }
});

Example Sketch

Following is an example of sending an SMS message to your cell phone when the light sensor indicates it’s getting dark.

Code Example: send an outbound SMS message example (send_sms_msg.js)

// Twilio Account SID and Auth token
var ACCOUNT_SID[] = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var ACCOUNT_TOKEN[] = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ";

// SMS-enabled Twilio phone number
var twilio_number = "1480405xxxx";

// Your mobile phone number
var your_number = “1480xxxxxx”;

// Load the Twilio module
var twilio = require('twilio');

// Make authenticated requests
var client = new twilio(ACCOUNT_SID, ACCOUNT_TOKEN);

// Load MRAA module
var mraa = require('mraa');
mraa.addSubplatform(mraa.GROVEPI, "0");
console.log("MRAA version: " + mraa.getVersion());

// Platform offset for conversion of the IO pins
var OFFSET = 512

// LED is connected to the digital D5
var ledPin = new mraa.Gpio(OFFSET + 5);
var ledState = true;
ledPin.dir(mraa.DIR_OUT);

// Load Grove module
var groveSensor = require('jsupm_grove');
// Light sensor is connected to the analog A1
var light = new groveSensor.GroveLight(OFFSET + 1);

// Load Sleep module
var sleep = require('sleep');

activity();

// if it is dark, turn on the LED and send the an SMS message.
function activity() {
    console.log("Enter activity()");
    console.log("ledState: " + ledState);

    // Check the light sensor whether it is dark
    var lightValue = light.value();
    if(light.value() < 10) {
        // Turn on the LED
        ledPin.write(1);

        // Send an SMS message
        message = client.messages.create({from: twilio_number,
                                  to: your_number,
                                  body: "Hello World!!!"},
            function(error, message) {
            if(!error) {
                console.log(message.Sid);
                console.log(message.ErrorCode);
                } else {
                    console.log("Error!");
                }
            });
    }

    ledState = !ledState;
    sleep.sleep(1);

    // Turn off the LED
    ledPin.write(0);
}

Run the Node Example

Export the MRAA dependent modules:

export NODE_PATH=/usr/local/lib/node_modules

Note that you need to be root user to access the GPIOs, so be sure to include “sudo”. To run the example, type the following command in a terminal:

sudo -E node send_sms_msg.js

When the light sensor indicates it’s getting dark, the LED will be blink and the SMS message will be sent. If your phone receives the message below, the SMS message was successfully sent.

Sent from your Twilio trial account - Hello World!!!

Summary

In this article, we have experimented with sending SMS messages using a Twilio account when the light sensor indicates dark conditions on the UP Squared board on Ubuntu. This experiment is designed to work on any IoT device that supports Intel microcontrollers running on Ubuntu such as Intel® IoT Gateway GB-BXTB-3825. Now try creating your own experiments with different sensors on the UP Squared board and other Intel® IoT devices.

UP Squared

References

About the Author

Nancy Le is a software engineer at Intel Corporation working to enable scalable IoT projects.

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.