If you've got WiFi available, the Particle Photon is an amazing little device for building your Internet of Things... Thing. Measuring a mere 1.44" x .8", it still packs a powerful 120 MHz ARM Cortex M3 from STMicroelectronics onboard, as well as a WiFi device from Cypress Semiconductor. That sums to quick build cycles and a powerful IoT product base for not-a-lot of money.
Today we're going to show you how to get your Particle Photon sending MMSes and SMSes with Twilio. All you'll need is your Photon, a friendly WiFi network, and about 20 minutes of time.
Let's do it!
For this to work, you need to use an SMS (or SMS and MMS) enabled number.
- Enter the Twilio Console
- Select the Phone Numbers ('#' / hash tag) section on the left side
- Navigate to your current number inventory
Under 'Capabilities,' you will see the phone number capabilities. You'll need numbers with SMS or both SMS and MMS enabled:
If you don't currently have a number with SMS or MMS capabilities, you can purchase one. After navigating to the 'Buy a Number' link, click the SMS and/or MMS checkbox:
Save the number that you use in a browser tab, so it is readily available.
In order to call the Twilio API from the Photon, you will need to authenticate. You need to have your Twilio Account SID and a Twilio Auth Token handy.
You can find these both in the Twilio Console:
As before, keep these handy.
To flash your Particle Photon, you'll first need to ensure it's connected to your WiFi network.
If you haven't yet, please follow Particle's Getting Started guide to get your Photon set up.
Particle uses Webhooks (just like Twilio!) to trigger external APIs when events occur. When you publish an event to Particle's cloud, the webhook you create is then used to trigger whatever external event you'd like. That might mean your own server or business logic, but for today we're going to link our Particle Webhooks to Twilio.
Log into the Particle Web IDE with your account.
There are two ways to get the sample code for this application:
That short snippet is all you need to send an SMS with the Photon.
Particle.publish("twilio_sms", body, PRIVATE);
This line of code is how we push an event to the cloud. We haven't yet added it (that's next!), but "
twilio_sms" is the event which will spawn a call to Twilio, while
body is a variable that gets passed along.
Flash the code to your Photon with the 'Flash' (Lightning) icon. You'll see the LEDs work, but you won't get a text yet.
Now's the time - have your Twilio credentials and an SMS-capable phone number handy. We're going to go through some very simple steps to get our Particle Webhook working.
- Navigate to the the Particle Console's Webhook Integration screen
- Click the '+' 'Plus' button icon to create a new integration
- Select 'Webhook'
- Event Name:
ACXXX...." with your Twilio Account SID)
- Request Type:
Next, we'll change some of the advanced options so we can give Twilio what it needs to send a text.
Make the following changes after expanding the 'Advanced Options' section.
- Send Custom Data: Form
- Under Data, set the following three:
To - [[YOUR CELL PHONE NUMBER/NUMBER THAT CAN RECEIVE SMSES]]
From - [[YOUR TWILIO NUMBER FROM ABOVE]]
- In HTTP BASIC AUTH:
Username - [[Your Account SID]]
Password - [[Your API Secret Key]]
After saving, feel free to send a 'Test' message with the 'Test' button. Or, you know, just wait for the next step - you're all glued together and good to go!
Hit the reset button on the Photon or power cycle it.
It should send you a text message (fingers crossed!). Nice!
The first example, by design, is very simple. In this example, we'll dial up the complexity by adding optional media messaging through MMSes to the app.
With properly formed JSON, Particle also allows you to pass multiple variables along. That's the mechanism we're going to use to build out MMS capability.
As before, there are two options to retrieve our code. You can:
Again, we've boiled down the code to the minimum. In your production application, you'll want to check on things like message size and text encoding, as well as add your business logic. You'll also need to consider special characters like double quotes.
Name a new Webhook
twilio_mms, but otherwise, go through the same steps as the above 'twilio_sms' Webhook.
Also, you need to change 'Send Custom Data' for 'Form':
- To - [[YOUR CELL PHONE NUMBER/NUMBER THAT CAN RECEIVE AN MMS]]
- From - [[YOUR TWILIO NUMBER FROM ABOVE]]
- Body -
- MediaUrl -
We now have 2 custom variables,
image. In C++, we manually craft JSON and add the image path and message body.
After a reset or power cycle (assuming you flashed it...) you should see a message and a nice picture of some wildlife.
20 minutes after starting this tutorial, you've now got snippets which let you send SMSes or MMSes from a Particle Photon. Whether you're automating your house, monitoring the weather, or making the Next Big ThingTM... we know you're going to build something awesome. Hopefully, this base proves useful!
We can't wait to see what you build, and please keep us posted on Twitter!