Example: integration with ThingsBoard using MQTT

In this example we will write an Oficloud project for sending the data obtained form an Oficloud channel to the ThingsBoard Live Demo. You need to create a free personal account. You also can download ThingsBoard Community Edition and use it in your own server. In that case you should install the script of this example in that server.

STEPS

1. Download base project
Create a folder for your node js project and download oficloud-base-project.
$ git clone https://github.com/juanjo75es/oficloud-sdk-base . 

2. Install package
Install the Node JS mqtt package:
$ npm install --save mqtt

3. Edit main.js
Edit main.js so that it listens for data in a channel and writes received data to an MQTT session.
                                    
var oficloud = require('./oficloud');
var mqtt = require('mqtt')

var options={username:"YOUR_THINGSBOARD_KEY",password:""};
var client  = mqtt.connect('mqtt://demo.thingsboard.io', options);

client.on('connect', function () {
    
    
})
 
client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString())
  
})

oficloud.login('myuser@myemail.com','MYPASSWORD',function(res){
    console.log("login res: "+res.res);
    
    if(res.e==-1)
        return;
    

	function onmsg(msg)
	{
        //Process received messages here         
         client.publish("v1/devices/me/YOUR_THINGSBOARD_PROJECT_NAME", JSON.stringify(msg))
	}

	oficloud.open_channel('p',onmsg,function(err){

        if(typeof err !="undefined")
        {
            console.log("open channel error: "+err);
            return;
        }

        //Do your stuff here once you are logged in and joined a channel
        //(...)
        

	})
});


                                    
                                

4. Run script
You can now execute the script. You should make it run everytime your server is restarted. You can see your data published at the ThingsBoard Live Demo or your local ThingsBoard installation (some configuration in your ThingsBoard account is required previously).