M2M to SMS: Send a text from your IoT device with Twilio M2M Commands and Twilio Functions
Time to read: 3 minutes
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.
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:
When we call the function sendM2M()
, we should see this in our console:
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.
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.
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.
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.
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.
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)
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.