Triggering SMS And Voice Notifications For IoT Devices

February 17, 2016
Written by

Triggering SMS And Voice Notifications For IoT Devices

People throw around the term “the internet of things” as if there’s an internet essence in all devices that you unlock via secret code. The truth is connecting one device to another and building a truly awesome bit of IoT hacking takes some engineering. But after that engineering you get sofas vibrating to music, and IoT connected boxing gloves.

Getting up and running with connected IoT devices isn’t as simple as setting up your dev environment. Marc Pous of TheThings.io knows that, and hopes his platform can take some of that work off of devs’ plates so they can focus on shipping code.

TheThings.io helps you manage those nagging dependencies, interoperability worries, and libraries. It also gives you monitoring reports and data analytics. So if you need to monitor your sofa that vibrates based on the vibe of a music festival, you can do that. Or if you need to get data from the Electric Imp installed inside the boxing glove you outfitted with a force sensor to see who punches the hardest, you can do that too.

For now, we’ll focus on setting up SMS alerts for IoT devices. Let’s say you have a trigger set for temperature monitoring. If your device gets too hot, you’ll get a text  or call so you can tend to the issue immediately. In the following example, originally published on TheThings.io’s blog, they’ll show you how to set that up. (Read their original blog post here)

Let’s Get Coding

You’ll need to have a TheThings.io account and a Twilio account to make this jam work. Once you’ve got both of those things sorted, go to the Cloud Code feature on TheThings.io and click on the ‘Cloud code’ link at the left sidebar.

cloud-code-main-view

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()
  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()
}

Blam, now you have SMS and Voice alerts in 32 lines of Javascript.

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
)

Here are a few resources if you need any help getting this up and running.

Twilio API & Docs
Learn more about cloud code
Learn more about triggers