All Collections
Developer Guides
UbiFunctions: Integrate TeCrea SigFox-ready Ibuttons with Ubidots
UbiFunctions: Integrate TeCrea SigFox-ready Ibuttons with Ubidots

Learn to integrate multi-purpose TeCrea's iButton from SigFox platform to Ubidots IoT App Enablement Platform

David Sepúlveda avatar
Written by David Sepúlveda
Updated over a week ago

TeCrea is a Colombian company based in Medellín. Mainly focused on developing on-demand electronic products applying its wide technological experience, it’s now advancing towards IoT devices and application solving. TeCrea is the first Colombian company developing IoT devices certified to communicate through SigFox network.

Ibutton is a device thought to satisfy multiple solutions and applications that directly impact industries and transform them. It allows to send alert messages, events and also can work as a real-time GPS tracker, without mentioning its version for temperature and humidity monitoring.

Following this guide you will be able to integrate any of the 3 type TeCrea Ibuttons with Ubidots to provide your application with visualization tools, complex event engine for data driven alerts and much more of Ubidots IoT application Enablement platform.

Ibutton 3 types are: 

  • Type 1: Ibutton tracker (GPS only)

  • Type 2: Temperature and Humidity sensors

  • Type 3: Both GPS tracking and Temperature and Humidity sensors

Requirements

Table of Contents

  1. UbiFunction setup

  2. SigFox setup

  3. Results

1. UbiFunction setup

Step 1: Create a new UbiFunction as described in this article’s 2nd and 3rd step.
Step 2:  Assign a name, change ‘Method’ to POST, and leave the Runtime as NodeJS.
Step 3: Replace
the default code by the following below
IMPORTANT NOTE: enter a valid Ubidots token from your account in the code’s second line.

//UbiFunction to integrate TeCrea's SigFox iButton to Ubidots

var request = require('request-promise');
var TOKEN = "PUT_YOUR_UBIDOTS_TOKEN_HERE"; // Enter your Ubidots TOKEN

FUNCS = {
    0: build_payload_type_0, // Button
    1: build_payload_type_1, // Position only
    2: build_payload_type_2, // Temperature y humidity
    3: build_payload_type_3 // Position, temperature and humidity
}

async function main(params) {
    console.log(params);
    var device_id = params['device_id'];
    type = button_type(params);
    payload = FUNCStype || {};
    // Make POST request to Ubidots
    var post_response = await ubidotsPost(TOKEN, device_id, payload);
    // Pass Ubidots' API response to the parser' response
    return await post_response;
}

async function ubidotsPost(token, device_label, data) {
    var options = {
        method: 'POST',
        url: 'https://industrial.api.ubidots.com/api/v1.6/devices/' + device_label + '?force=true',
        body: data,
        json: true,
        headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token': token
        }
    };
    return request.post(options);
}

// Sensor type 3: Position Temperature and Humidity
function build_payload_type_3(params) {
    var buf = new Buffer.from(params.data, 'hex');
    var lat = buf.readFloatBE(0);
    var lng = buf.readFloatBE(4);
    var info_byte = buf.readUInt8(8).toString(2).padStart(8, '0');
    var digital_input = info_byte[7];
    var low_battery = info_byte[6];
    var periodic = info_byte[0];
    var adc_values = buf.readUIntBE(9, 3).toString(2).padStart(24, '0');
    var temperatura = parseInt(adc_values.substr(12, 24), 2) * (165 / 4095) - 40;
    var humedad = parseInt(adc_values.substr(0, 12), 2) / 40.95;
    return_json = {
        "position": { "value": 1, "context": { "lat": lat, "lng": lng } },
        "digital-input": digital_input,
        "low-battery": low_battery,
        "periodic": periodic,
        "temperatura": temperatura,
        "humedad": humedad,
    }
    return return_json;
}

// Sensor type 2: Temperature, Humidity
function build_payload_type_2(params) {
    var buf = new Buffer.from(params.data, 'hex');
    var info_byte = buf.readUInt8(8).toString(2).padStart(8, '0');
    var digital_input = info_byte[7];
    var low_battery = info_byte[6];
    var periodic = info_byte[0];
    var adc_values = buf.readUIntBE(9, 3).toString(2).padStart(24, '0');
    var temperatura = parseInt(adc_values.substr(12, 24), 2) * (165 / 4095) - 40;
    var humedad = parseInt(adc_values.substr(0, 12), 2) / 40.95;
    return_json = {
        "digital-input": digital_input,
        "low-battery": low_battery,
        "periodic": periodic,
        "temperatura": temperatura,
        "humedad": humedad,
    }
    return return_json;
}

// Sensor type 1: Position
function build_payload_type_1(params) {
    var buf = new Buffer.from(params.data, 'hex');
    var lat = buf.readFloatBE(0);
    var lng = buf.readFloatBE(4);
    var info_byte = buf.readUInt8(8).toString(2).padStart(8, '0');
    var digital_input = info_byte[7];
    var low_battery = info_byte[6];
    var periodic = info_byte[0];
    return_json = {
        "position": { "value": 1, "context": { "lat": lat, "lng": lng } },
        "digital-input": digital_input,
        "low-battery": low_battery,
        "periodic": periodic,
    }
    return return_json;
}

// Sensor type 4: Button
function build_payload_type_0(params) {
    var buf = new Buffer.from(params.data, 'hex');
    var periodic = buf[8] >> 7;
    var low-battery  = (buf[8] >> 1) & 0x01
    var adc_values = buf.readUIntBE(9, 3).toString(2).padStart(24, '0');
    var battery = (parseInt(adc_values.substr(12, 24), 2) - 2340) / 58.5;
    return_json = {
        "battery": battery,
        "low_battery": low_battery,
        "periodic": periodic
    }
    return return_json
}

// Returns button type
function button_type(params) {
    var buf = new Buffer.from(params.data, 'hex');
    var info_byte = buf.readUInt8(8).toString(2).padStart(8, '0');
    var button_type = parseInt(info_byte.substr(1, 5), 2);
    return button_type;
}

Step 4: Click on the ‘Make it live’ button

2. SigFox callback setup

Step 1: Create a new SigFox callback for your TeCrea Ibuttons as explained here (first read all steps).
Step 2: In the ‘URL pattern’ field, enter your UbiFunction URL.
Step 3: Select POST as the HTTP method to make the request.
Step 4: Change
the ‘Content-type’ header value to “application/json”.
Step 5: Enter
the following as your custom JSON payload to be sent and process by the UbiFunction:

{
   "device_id": "{device}",
   "data": "{data}",
   "rssi": "{rssi}"
}

Step 6: Save the callback

3. Results

After completing the UbiFunction and Sigfox callback Setup, you'll be all setup to start receiving data of the Ibutton device directly to your account.

Did this answer your question?