In this tutorial you will see how to launch a test application sending random data to an Oficloud channel. You can then read that data from any authorized node connected to the channel.
The materials needed are:
You also need one of these options:
STEPS
$ git clone https://github.com/juanjo75es/oficloud-sdk-base .
var oficloud = require('./oficloud');
var dateFormat = require('dateformat');
function mytick()
{
var today0 = new Date();
var today=dateFormat(today0, "yyyy-mm-dd H:MM:ss");
//generate random data
var temperature=Math.random()*45;
var humidity=Math.random()*100;
//post data to Oficloud channel
var s="{\"date\":\""+today+"\",\"temperature\":"+temperature.toFixed(1)+",\"humidity\":"+humidity.toFixed(1)+"}";
console.log(s);
oficloud.send_post('MYCHANNEL',s,function(err){
if(err.e!="0")
{
console.log("exited timeout: "+err.e);
return;
}
//call tick again with 30 seconds delay
setTimeout(mytick,30000);
});
}
function init(ondone)
{
//call tick after 1 seconds
setTimeout(mytick,1000);
//call return function
ondone();
}
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
}
oficloud.open_channel('MYCHANNEL',onmsg,function(err){
//Do your stuff here once you are logged in and joined a channel
//(...)
if(typeof err !="undefined")
{
console.log("open channel error: "+err);
return;
}
init(function (){
});
})
});
$ npm install
$ node main.js
Your program will post a message to the 'MYCHANNEL' channel every 30 seconds reporting temperature and humidity.
You can read that data from any authorized node connected to the channel. For example, you can build an HTML dashboard like the one in this tutorial or import the data into a ThingsBoard dashboard like in this other tutorial.