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!