Posts

Quick Guide to Cloud Code

As you know, Internet of Things is the next big thing. Considered the fourth biggest revolution (as important as the industrial and the Internet ones), all the machines connected to the Internet are saving huge amounts of data. But data on its own form does not matter. What is really important is the information we can get from it and services that you can build on top of the information. Here is where thethings.iO can help you make IoT simple!

Cloud Code

Welcome to the new Cloud Code feature. The business logic could not be in your things or Apps but in the Cloud. thethings.iO Cloud Code feature transforms the data from your connected things into information that will be useful to your daily activities.

Nowadays, the Cloud Code is a Javascript runtime programmable tool that enables you to program business logics, artificial intelligence algorithms or others on the top of the data stored. We can find 3 different categories: triggers, jobs and functions. Let’s talk a little bit more about each one. In the future we are going to integrate other languages such as Python, GO or others. What languages would you like to have?

Triggers

Firstly, we find Triggers. A trigger is a piece of code that is executed automatically on our Cloud, after a write command is received. That means that every time a temperature sensor sends the temperature, a trigger is launched in order to convert that data into celsius degrees or launch an alarm if the temperature goes below zero.

A typical use of a trigger is to create an alert. You can code the trigger in a way that when an alert condition happens, you can send an email, send an SMS and make a call with Twilio or tweet a message. And of course, store an event to build reports later. The execution of a trigger is limited to 2 seconds.

Following the previous example, if you have a temperature sensor you can build a Javascript code with the logics of the trigger to alert you when the temperature is higher than X value. When it happens, a message is sent (SMS, e-mail or tweet) to your smartphone or tablet to notify you about the condition. Additionally, if you store an event, later via Jobs, you can generate a report that shows how many times in one day this event get triggered.

Jobs

The Jobs is Javascript code that can be executed once every hour or once a day. The Jobs allow you to set up long running tasks. The execution of a job is limited to 10 minutes. Jobs lets you create map-reduce functions, reports and analytics aggregating the data from the events that your triggers or functions generate.

Current examples of the Jobs are related with mathematical functions or artificial intelligence and predictive maintenance scenarios. For example, a job synchronized with a trigger can help you to predict malfunctions every hour. Another example is a Job that analyzes how your customers use their devices and sends a daily report to the marketing people of your company.

Functions

Finally, we have Functions. Functions are algorithms that could be executed when they are needed. Functions can be called by triggers or jobs and even from outside thethings.iO via an API REST endpoint. Jobs encapsulate business logic that you don’t want to couple into a trigger or a job and of course not into the devices or mobile apps. The execution of a Function is limited to 20 seconds.

If you are using Sigfox, the functions are the place where you need to code the Sigfox payload parser in order to visualize the Sigfox data at thethings.iO.

All Cloud Code plugins

These are thethings.iO Cloud Code plugins:

  • emailing: it sends emails.
  • httpRequest: it sends an http request.
  • console: logs in your code.
  • analytics.events: allows to create and retrieve events.
  • analytics.kpis: to create metrics to be displayed on your dashboard.
  • thethingsAPI: allows to read and write on a device resource.
  • DataSet: represents the data returned from analytics.events.getByName.
  • Twilio: it sends SMS or phone calls. In case you missed it, read the post about Twilio here.
  • Geolib: Integrates Geolib library to provide basic geospatial operations like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc.
  • Twitter.

You can find more information on thethings.iO documentation site.

Know how our Cloud Code works

Do you want to see how it works? Discover how easy it is to use thethings.iO’s Cloud Code! We have created a thing which measures temperature. The next screenshot shows how our Dashboard looks like, with the ‘Thermostat Alerts’ graphic.

In this second screenshot, we see the Job code. In this case, the job get all the Alarm events of the day and store a KPI with the total events. This KPI is consumed by the dashboard widget ‘Thermostat Alerts’ to generate the graphic.

And finally, we see the screenshot of the Trigger code. In this case, we wanted to be notified with an alarm in case the sensor gets a temperature of 50ºC or above. We also store the event ‘Alarm’, which is later consumed by the previous Job.

If you want to learn more about thethings.iO features then you can not miss these posts about Twilio and Sigfox! And make sure you follow us on Twitter to keep you posted about the Internet of Things world and thethings.iO latest news!

,

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.

Twilio with thethings.iO

Twilio with thethings.iO

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.

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

Further reading