Sending text messages or making automated phone calls is a great way to add interactivity to a connected project.
It was unfortunately a pretty involved thing to set up until I heard about Cisco Tropo at the ArborHacks hackathon in Ann Arbor. Here's a step-by-step guide to start sending text messages from a Particle Photon.
The flow for sending a text message starts with publishing an event from Particle firmware. This triggers a webhook in the Particle Cloud that calls the Tropo service with the event name and data. A small script hosted on Tropo sends the actual SMS.
Tropo has a free development tier to set up your app initially. If you intend to send any SMS beyond a handful of test SMS to ensure the app is working, you'll need to move the application to the paid production tier.
Setting up TropoCreate an account at tropo.com
Create a new app. Pick an area code for the phone number in your country.
The next part is amazing: Tropo will host the short script that sends the SMS. You don't need an additional cloud server or script hosting platform! Click the New script button and paste this JavaScript code to send the SMS.
phoneNumber = event.split('/')[1];
call("+1" + phoneNumber, { network:"SMS"});
say(data);
This script uses the fact that we will publish events names with the phone number in them.
When this script runs the events
variable will hold the event name, the data
variable will hold the data published with the event and the coreid
variable will hold the device ID of the Particle device that published the event.
Give your script a name and hit Save.
Click the See token URL next below Messaging in the API keys section and copy the long line that starts with https://api.tropo.com
. This is the URL that will trigger the script above. We will use this URL when setting up the Particle webhook.
One last important note:
Before your able to send text messages with your Tropo account, your account needs to be activated by Tropo support. Send an email to support@tropo.com with the subject "Activate outbound SMS
" and include your 7 digit account number.
This part is very easy.
Just publish an event called sms
followed by slash followed by the phone number and the message as the data to send.
void setup() {
// Send an SMS when the Photon gets online
// IMPORTANT: replace the bogus phone number with your phone number
Particle.publish("sms/5551234", "Welcome to the Particle Cloud!", PRIVATE);
}
void loop() {
}
Copy-paste this code to a new sketch in the Particle Web IDE, replace the bogus phone number with your phone number and hit the lightning bolt icon to flash it to your device.
You can of course customize this code for your application. Just publish an sms/5551234
event when you want to send a text message!
To set the final connection, go to the Particle Console.
Open the Integration sections from the left hand side and click New integration then Webhook.
In Event name, type sms
. This will match all events starting with sms, including your sms/5551234
event no matter which phone number you put at the end of the event name. In URL, paste the Tropo message URL from before. Make sure the Request Type is POST
.
Hit Create Webhook, and you're done!
See it in action!At this point hit the RESET
button on your Photon and in a few seconds you'll see a text message pop up on your phone!
If it didn't work, make sure you contacted Tropo to activate the ability to send text messages from your account. Explore the Tropo docs to see what else you can do such as making phone calls.
Thanks to Cisco and Particle for sponsoring the ArborHacks event!
Comments