Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* Constants */
const request = require('request-promise');
const TOKEN = "PUT_YOUR_UBIDOTS_TOKEN_HERE"; // Assign your Ubidots TOKEN
/* Decodes the hex frame coming from the SigFox callback – Thinxtra device */
function decodeHexFrame(data) {
var buf = new Buffer.from(data, "hex");
var temperatura = buf.readInt16LE() / 100;
var presion = buf.readInt16LE(2) * 3;
var luz = buf.readInt16LE(4) / 1000;
var aceleracion_x = buf.readInt16LE(6) / 250;
var aceleracion_y = buf.readInt16LE(8) / 250;
var aceleracion_z = buf.readInt16LE(10) / 250;
payload = {
"aceleracion_x": aceleracion_x,
"aceleracion_y": aceleracion_y,
"aceleracion_z": aceleracion_z,
"luz": luz,
"presion": presion,
"temperatura": temperatura
}
return payload;
}
/* Send data to ubidots */
async function sendToUbidots(device, payload) {
var options = {
method: 'POST',
url: 'https://industrial.api.ubidots.com/api/v1.6/devices/' + device,
body: payload,
json: true,
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': TOKEN
}
};
return await request.post(options);
}
async function main(args) {
console.log(args);
var device_label = args["device"]; // Device label match SigFox ID
var payload = decodeHexFrame(args["data"]);
console.log(payload);
var response = await sendToUbidots(device_label, payload);
console.log(response);
return {"status_code": response.status_code};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.