Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Last active May 1, 2019 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubi-gists/c55a7f3a8e49d52e2736181709fee898 to your computer and use it in GitHub Desktop.
Save ubi-gists/c55a7f3a8e49d52e2736181709fee898 to your computer and use it in GitHub Desktop.
/* 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