SMS & Voice Calls from Twilio meet thethings.iO
Internet of Things is about monitoring, alarms, notifications, among other, then it’s important to get real-time updates and the best way to get informed it’s probably through phone calls and SMSs. The IoT platform thethings.iO integrated Twilio, the communication platform that leads the voice and messaging applications.
Send SMS and make voice calls from your things with thethings.iO and Twilio
thethings.iO offers notifications (SMS and Voice calls) for your things through cloud code. This feature allows you to receive voice calls, SMS (with Twilio), emails, and tweets (with Twitter), whenever you trigger these messages from your networked products.
Recently, we have integrated Twilio on our Cloud Code feature. This allows our customers to easily program business logics on the top of connected devices and send SMS messages or make a scripted voice calls when needed. In this post I’m going to go through how to integrate Twilio on your IoT solution with a simple example.
Set up Twilio at thethings.iO
To start working with Twilio on thethings.iO IoT platform, you need an account on Twilio, because you need to introduce your Twilio credentials at thethings.iO. Don’t worry if you don’t have an account on Twilio, it’s free to create one and you get a phone number to start playing.
After that, you need a thethings.iO account to start sending data from your devices to our IoT platform. It’s very simple! Register here at thethings.iO follow the configuration step by step. And when you are ready, find some examples from some of the most famous IoT hardware platforms here, such as Arduino, ESP8266, Atmel, Electric Imp and other on our github account. An account on thethings.iO is free and simple to use. You can follow our getting started document and be ready in 1 minute.
Now that your thethings.iO and Twilio accounts are created and configured, you are ready to try the Cloud Code example that sends SMS when the temperature is too high.
Receive an SMS when the temperature of your device is too high
First, go to the Cloud Code feature clicking on the ‘Cloud code’ link at the left sidebar.
Create a trigger at the ‘Add Trigger‘ button. A form to create a trigger will appear. You have to name the trigger, link it to a product and insert the Javascript code that will be executed.
Triggers are executed when an event occurs like a thingWrite (when your thing post a value). So, we are going to code a script that sends an SMS when the temperature of a thing is up to 50. Here there is the JS code:
function trigger(params, callback){ console.log('trigger triggered!!') //ignore non write events if(params.action !== 'write') return callback()</code> var values = params.values var thingToken = params.thingToken //iterate over the values of the write for(var i=0; i<values.length; ++i){ if(values[i].key === 'temperature' && values[i].value > 50){ console.log('omg too hot') var telf = '+346661199991111' var message = 'Master, it’s too hot here! Device: ' + thingToken var twilio = new Twilio( 'YOUR Twilio AccountSid', 'YOU Twilio AuthToken' ) twilio.sendMessage({ to: telf, // Any number Twilio can deliver to from: '+346660099991111', // A number you bought from Twilio and can use for outbound communication body: message // body of the SMS message }, callback ) } } //end the trigger callback() }
Now, every time that the temperature of all the devices from the product are up to 50, you’ll receive an SMS alerting you in real-time. And all you need to include to enjoy this feature are 32 lines of JS on your thethings.iO cloud code feature.
If you prefer to receive a voice call instead an SMS, you only have to change the twilio.sendMessage to twilio.makeCall as in the following example:
twilio.makeCall({ to: telf, from: '+346660099991111', url: url_call // A URL that produces an XML document (TwiML) which contains instructions for the call }, callback )