All Collections
Technical Resources
Ubidots Decoders as a service, SenseCap S2102 and TTS LNS: Practical example.
Ubidots Decoders as a service, SenseCap S2102 and TTS LNS: Practical example.

The following technical guide depicts how to use Ubidots Decoders as a service by using it in a real application

S
Written by Sergio M
Updated over a week ago

Requirements

  • Any device sending data to either, a Plugin or a UbiFunction on Ubidots from any LNS

  • The URL of the repository in which the device's decoder is hosted

Table of Contents

1. Preliminary comments

  • If you haven't yet checked on the previous technical guide regarding Ubidots DaaS, it is highly advisable to do so.

  • This technical guide uses a SenseCAP S2102 - LoRaWAN Wireless Light Intensity Sensor which is registered on TTS LNS configured so that no decoding is performed on TTS's side. In other words, the raw binary data will arrive into Ubidots to use DaaS.

2. Identify the decoder's entry point as well as the arguments it expects

  • There are probably several different functions defined, so you need to identify the decoders entry point. Thankfully, most decoders are well commented and tell you exactly which is the entry point

  • Also, note in the following image, that this decoder declares and initializes a bytes variable with data contained inside the bytes key in the incoming arguments object, so if the incoming object doesn't have such key, the decoder will fail to execute from the very beginning.

  • With the above in mind, the decoder expects an object like the following on its arguments:

{
'bytes' : '01ABEDF5CD'
}

3. Build the request to use Ubidots DaaS

  • As the previous guide dictates the body of the request to use Ubidots DaaS is a JSON as follows:

{
"url": "DECODER-URL-RAW-FORMAT",
"payload": {
DECODER-EXPECTED-OBJECT
},
"function": "DECODERS-ENTRY-POINT"
}

Where:

Key

Value

url

The URL of the repository hosting the decoder

payload

An object matching the one expected by the decoder

function

The name of the decoders main function or entry point

With that in mind, then the body of a request for using Ubidots DaaS on the SenseCAP S2102 - LoRaWAN Wireless Light Intensity should look like:

{
"url": "https://raw.githubusercontent.com/Seeed-Solution/TTN-Payload-Decoder/master/decoder_new-v3.js",
"payload": {
'bytes' : '01ABEDF5CD' (A hex string)
},
"function": "decodeUplink"
}

Head to the plugin receiving the data and go to its decoder section, the in the code editor do:

Import the library required for issuing the HTTP request:

const axios = require('axios');

Define and initialize the decoder's URL and entry point:

const decoderURL = 'https://raw.githubusercontent.com/Seeed-Solution/TTN-Payload-Decoder/master/decoder_new-v3.js';
const decoderEntryPoint = 'decodeUplink';

Declare the function to issue the request to Ubidots DaaS endpoint

async function DaaS(decoderURL, entryPoint, payloadToDecode, ubidotsToken)
{
var headers = {"X-Auth-Token": ubidotsToken, "Content-Type": "application/json"};

var payload =
{
"url" : decoderURL,
"payload" : payloadToDecode,
"function" : entryPoint
}

var options =
{
"method" : 'post',
"url" : 'http://functions.ubidots.com/pub/decode/custom',
"data" : payload,
"json" : true,
"headers" : headers,
};

const req = await axios.request(options);
return req.data.decoded;

}

Declare the function to build an Ubidots compatible payload from the one returned by the device's decoder. For this particular example, such function you can find on previous technical guides. If you are using another device, you require to know what object does the device's decoder return in order to format it to an Ubidots compatible JSON.

function buildUbidotsPayload(args) 
{
var ubidotsPayload = {};
var messages = args.data.messages;
var varLabelMap =
{
4097:"air-temperature",
4098:"air-humidity",
4099:"light-intensity",
4100:"CO2-concentration",
4102:"soil-temperature",
4103:"soil-humidity",
4108:"soil-electrical-conductivity",
};
messages.forEach(msg =>
{
if (msg.hasOwnProperty("measurementId"))
{
ubidotsPayload[varLabelMap[msg.measurementId]] = msg["measurementValue"];
}
});

return ubidotsPayload;
}


Declare the main function:

async function formatPayload(args)
{
var ubidotsToken = args._parameters.token;
var incomingBinaryData = args.uplink_message.decoded_payload.payload;
var decodedData = await DaaS(decoderURL,decoderEntryPoint, {"bytes" : Buffer.from(incomingBinaryData,'hex')},ubidotsToken);
var ubidotsaPyload = buildUbidotsPayload(decodedData);
return ubidotsaPyload;
}


Lastly, the whole Plugin's decoder should look like:

const axios = require('axios');

const decoderURL = 'https://raw.githubusercontent.com/Seeed-Solution/TTN-Payload-Decoder/master/decoder_new-v3.js';
const decoderEntryPoint = 'decodeUplink';


async function formatPayload(args)
{
var ubidotsToken = args._parameters.token;
var incomingBinaryData = args.uplink_message.decoded_payload.payload;
var decodedData = await DaaS(decoderURL,decoderEntryPoint, {"bytes" : Buffer.from(incomingBinaryData,'hex')},ubidotsToken);
var ubidotsaPyload = buildUbidotsPayload(decodedData);
return ubidotsaPyload;
}

function buildUbidotsPayload(args)
{
var ubidotsPayload = {};
var messages = args.data.messages;
var varLabelMap =
{
4097:"air-temperature",
4098:"air-humidity",
4099:"light-intensity",
4100:"CO2-concentration",
4102:"soil-temperature",
4103:"soil-humidity",
4108:"soil-electrical-conductivity",
};
messages.forEach(msg =>
{
if (msg.hasOwnProperty("measurementId"))
{
ubidotsPayload[varLabelMap[msg.measurementId]] = msg["measurementValue"];
}
});

return ubidotsPayload;
}

async function DaaS(decoderURL, entryPoint, payloadToDecode, ubidotsToken)
{
var headers = {"X-Auth-Token": ubidotsToken, "Content-Type": "application/json"};

var payload =
{
"url" : decoderURL,
"payload" : payloadToDecode,
"function" : entryPoint
}

var options =
{
"method" : 'post',
"url" : 'http://functions.ubidots.com/pub/decode/custom',
"data" : payload,
"json" : true,
"headers" : headers,
};

const req = await axios.request(options);
return req.data.decoded;

}



module.exports = { formatPayload };

4. Visualizing the data

After successfully completing the steps above, your device should be displayed on Ubidots with its data:

5. Feedback, suggestion and related articles

Feel free to post questions or suggestions in our community portal, or contact us via support@ubidots.com.


Other users also found helpful...

Did this answer your question?