M2M to SMS: Send a text from your IoT device with Twilio M2M Commands and Twilio Functions

May 04, 2020
Written by
Reviewed by
Paul Kamp
Twilion

M2M-to-SMS-twilio-functions-header

Twilio’s Machine-to-Machine (M2M) commands help you quickly send data directly from your IoT device to the internet and vice versa. These messages are good options for occasional commands; you could wake a device by sending a command from a server, or you could send a message to your server when your device wakes up.

If your device can send an SMS, it can also send a Twilio M2M command. Development boards like the Adafruit Feather FONA 32u4 are well set up to send an SMS using a Programmable Wireless SIM. You simply have your device send an SMS to the short code 2936, and the command will appear in your Twilio Console.

You can take these commands and use them with Twilio’s other fantastic APIs to send texts, WhatsApp messages, and emails, make phone calls, start video chats, or trigger additional M2M commands.

The possibilities are endless, but let’s start small. Today we’ll use M2M commands to send a text to a phone number using Twilio M2M Commands and Twilio Functions.

What you’ll need

  • A Twilio account. If you sign up for a new account through this link, you’ll get an extra $10 in credit when you upgrade.
  • A Twilio phone number.
  • A Twilio Programmable Wireless SIM. Log in and order one here. Create an order using the + button.
  • A device that can text a short code using the Twilio SIM, such as the Adafruit Feather FONA 32u4.

You can only buy SIMs in an upgraded account (one which has payment info added)

Set up M2M commands in your firmware

Set up your firmware to send an M2M command. The details will vary based on the board you are using and the language. Here is an example function to send an M2M command on the Adafruit Feather FONA 32u4:

void sendM2M() {
  // send an SMS to the shortnumber 2936, which triggers a Twilio M2M command
  if (!fona.sendSMS("2936", "on")) { // in this case, we'll have the command be "on"
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("Sent!"));
    state=2;
  }
}

When we call the function sendM2M(), we should see this in our console:

A list of commands being sent from my device

I sent the M2M command "on" a bunch of times from the same device.

Write a Twilio Function to handle incoming M2M commands

Now we know how to send a command and we need to do something with it. We can set up a Twilio Function to watch for incoming M2M commands from this SIM, and send us a text when one comes in.

To build this part, you’ll need to enable your Account SID and Auth Token to make them available as environment variables in your functions. This will let Twilio Functions access your Twilio account to monitor the Programmable Wireless API for your device’s M2M commands.

The configuration section that allows you to check a box to enable ACCOUNT_SID and AUTH_TOKEN

Next, we’ll build our actual function. Go to the Functions page in the console and click the red + sign to create a new function. In the menu that appears, select “Blank” to make a blank function.

Copy the following code into your function, changing the [INFO IN BRACKETS] to your info. Remember to put your phone numbers in E.164 format.

exports.handler = function(context, event, callback) {
  context.getTwilioClient().messages.create({
    to: '[YOUR PHONE NUMBER]',
    from: '[YOUR TWILIO NUMBER]',
    body: 'Ahoy, from your Twilio IoT device!'
  }).then(msg => {
    callback(null, msg.sid);
  }).catch(err => callback(err));
}

Name and save the function, and give it a path. (In this case, I was texting myself info about my mail, so I named it send-mail-sms with a path of /send-mail-sms.)

Copy the path to your clipboard.

The properties window that shows the function name and path, and allows you to copy the path

Configure your SIM to use your Twilio Function

Navigate to the SIMs page in your console and find your SIM in the list. Click on it to view more info.

Scroll to the bottom of the Configure menu. There should be a section called Commands where you can paste the commands callback URL.

The commands area where you can add a callback URL for the M2M commands sent from your device

Enjoy receiving Twilio IoT-powered SMS

Time to get a lot of IoT texts from your IoT device. Turn it on and test it by sending another message to short code 2936 from your dev kit! You should now receive texts whenever your device sends an M2M command.

a gif of an owl texting and moving its head around like it is excited

Want to build something more? Try a Twilio-powered IoT Mailbox Sensor that tells you when your mail has been delivered.

Questions about Twilio? Sign up for an account to build one yourself, or talk to Sales about IoT connectivity solutions.

Christine Sunu is the Internet of Things Developer Community Engagement Manager at Twilio. She's currently working on IoT, ambient computing, and robots that pretend to be alive. Find Christine on Twitter (@christinesunu) or Github (cmsunu28)