Quick Start Guide

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:

  • Have configured an SSH connection to the gateway.
  • Have configured a remote connection (VNC, etc) to the gateway.
  • Connect the gateway to a monitor using an HDMI cable.

STEPS

1. Create channel
Create the channel in your Oficloud account if it doesn't exist yet.
Note: this step is provisional until an specialized interface for IoT projects is developed.

2. 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 . 

3. Edit script
3. Edit main.js to add the code to generate random temperature and humidity data every 30 seconds and post the data to 'MYCHANNEL'.

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 (){

        });

    })
});
                            

4. Install auxiliar libraries
Install Node JS auxiliar libraries with:
$ npm install

5. Done
That's all. Execute it with:
$ 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.