Libelium and scriptr.io partnership

Scriptr.io has recently been certified as a Libelium cloud partner. This means you now have the option to configure your Meshlium gateways to send the data they receive from their sensor nodes to your scriptr.io accounts, where it can be processed, leveraged within business rules and processes, and/or forwarded to other systems

Libelium is one of the major actors of the IoT hardware market. The company offers a wide range of devices, stemming from sensor-equipped nodes (around 130 sensors); to IoT gateways (Meshlium) that support multiple communication protocols; to cloud services for device management or easy implementation of logic on the sensor nodes. Thanks to this offering, Libelium currently covers most of the vertical IoT application domains (smart cities, smart farming, transportation, etc.).

The combination of Meshlium and scriptr.io allows you to implement very sophisticated IoT applications. All you need is to configure your gateway to start pushing data to scriptr.io, as explained in the following paragraphs.

Configure communication between Meshlium and scriptr.io

Create a device on scriptr.io

The requests and messages issued by your Meshlium device towards scriptr.io need to be authenticated. Consequently, it is recommended that you create a device for your Meshlium gateway in your scriptr.io account, then use its credentials when sending data from the gateway to scriptr.io.

Sign-in to your scriptr.io workspace then click on the drop-down near your username in the top right corner. Select Device Directory, then enter an id (e.g. meshlium) and a password for your device. Confirm the password then save your changes by clicking on the check button on the right.

new-device-1-small

A token is automatically generated for your device. You will use this token later on.

new-device-2-small

Configure Meshlium to send data to scriptr.io

Meshlium gateways host a web application (the Manager System) that you can use to administer the device. You should be able to sign-in to it by typing something similar to https://local_meshlium_ip/ManagerSystem (e.g. http://10.0.3.240/ManagerSystem) in any web browser. Use the credentials that were sent to you by Libelium or those specified in the technical guide.

From the Manager System menu, select Cloud Connector.

cloud-connector

Select Basic Cloud Partner from the left menu then select scriptr; from the list of partners.

basic-partner

You are now presented with scriptr.io’s configuration screen, which allows you to send data to scriptr.io using the HTTP or the AMQP protocols. Using the latter allows you to queue the execution of the instances of the script that receives the data, which can be an advantage. Note that AMQP and queuing are permanently available in script.io’s paid subscriptions, and only available for free for one month on the free tier.

Sending data to scriptr.io using http

Fill the form as follows:

  • Script name: enter the name of a script you created in your scriptr.io account (e.g. “libelium”)
  • Message template: configure the message to send to scriptr.io (this depends on the measures sent by your sensors)
  • Log level: specify a log level
  • Interval: specify a time in seconds between two synchronizations with your scriptr.io account. This value cannot be less than 30
  • Server adddress: type “api.scriptrapps.io”
  • Protocol: choose “HTTP” (default value)
  • In the “HTTP” section, fill the Auth token field by entering a valid scriptr.io authentication token (the one you obtained when creating the meshlium device in your scriptr.io account, as explained above).

http-meshlium

Note that the “Message template” field describes how the data, which is received from your Libelium sensor nodes, should be sent to scriptr.io. By default, you will receive a message containing a timestamp, a device id, a sensor id and the value measured by the sensor. You may modify this template depending on the devices you are using.

Sending data to scriptr.io using amqp

Important: to send data from Meshlium to your scriptr.io account using AMQP, you need to do some configuration work on scriptr.io’s side, as explained in the next section.

From the Manager System screen in Meshlium:
Fill the form as follows:

  • Script name: enter the name of a script you created in your scriptr.io account (e.g. “libelium”)
  • Message template: configure the message to send to scriptr.io (this depends on the measures sent by your sensors)
  • Log level: specify a log level
  • Interval: specify a time in seconds between two synchronizations with your scriptr.io account. This value cannot be less than 30
  • Server address: type “amqp.scriptr.io”
  • Protocol: choose “AMQP”
  • In the “AMQP” section, fill the User by entering the value of the AMQP “Username” obtained from scriptr.io)
  • In the “AMQP” section, fill the Password by entering the value of the “Password” field obtained from scriptr.io)
  • In the “AMQP” section, fill the Virtual host by entering the value of the “Virtual host” field obtained from scriptr.io)
  • In the “AMQP” section, fill the Exchange by entering the value of the “Exchange” field obtainexchangeed from scriptr.io)
  • In the “AMQP” section, fill the Routing key by entering the value of the “Routing key” obtained from scriptr.io)

amqp-config_

Configure your scriptr.io account to receive AMQP messages

To leverage AMQP you need to create a channel in your scriptr.io account (or reuse an existing one). To create a new channel from your scriprt.io workspace, click on the drop-down arrow near your username at the top-right corner of the screen, then select Settings. In the configuration dialog that is displayed, select the Channels tab. Click on +Add Channel then enter a name (e.g. “libelium”). Keep the checkboxes unchecked so that only authorized entities can publish or subscribe to this channel. Click on the check sign on the right to save your changes.

Once you have a channel, you need to configure AMQP for your Meshlium device. Click on your username on the top-right corner of the screen and select Messaging Protocols. In the resulting panel, select “AMQP” as protocol then select the Meshlium device from then drop-down list of the “Credentials” section. In the “Publishing Details” section, select the channel created above (or any other channel). Copy the value of “Virtual host”, “Username” and “Password” from the “Credentials” section,  and “Exchange Name” and “Script Invocation Routing-Key” from the “Publishing Details” section, and paste them in the “AMQP” section of the configuration form in the Manager System.

amqp_scriptr

Implementing a script to process Meshlium’s data

As mentioned above, you must have created a script in your scriptr.io account to receive the data that is sent by your Meshlium gateways. Copy the below code and paste it at the beginning of the script to parse any HTTP request or AMQP message sent by Meshlium. Add you custom code in the // ADD YOUR CUSTOM LOGIC HERE section of the script.

// require the scriptr.io's log module and configure it to the "info" level
var log = require("log");
log.setLevel("info");
try {
    var payload = null;
    // Check if the payload is available in request.parameters
    // (AMQP message) or in request.body (HTTP message)
    if (request.parameters && Object.keys(request.parameters).length > 0) {
        payload = JSON.parse(request.parameters.data)
    }else {
        payload = JSON.parse(request.body);
    }
    log.info("Received the following payload:\n" + JSON.stringify(payload));
    
    // ADD YOUR CUSTOM LOGIC HERE     
    
}catch(exception){
    log.error("Something went wrong\n" + JSON.stringify(exception));
}