In this example we will develop an IoT project consisting on a temperature sensor directly connected to an Oficloud gateway. We will take advantage of the Node JS open source repository of IoT libraries for this purpose.
The materials needed are:
You also need to choose between one of these options:
STEPS
{
"dependencies": {
"@trust/webcrypto": "^0.9.2",
"aes-js": "^3.1.1",
"argon2": "^0.19.3",
"atob": "^2.1.2",
"blob": "0.0.4",
"btoa": "^1.2.1",
"crypto": "^1.0.1",
"dateformat": "^3.0.3",
"form-data": "^2.3.2",
"fs": "0.0.1-security",
"get-random-values": "^1.2.0",
"http": "0.0.0",
"line-reader": "^0.4.0",
"node-forge": "^0.7.6",
"querystring": "^0.2.0",
"rsa-pem-to-jwk": "^1.1.3",
"secrets.js": "^0.1.8",
"sha1": "^1.1.1",
"streamifier": "^0.1.1",
"xmlhttprequest": "^1.8.0",
"node-dht-sensor": "0.0.34"
}
}
$ npm install
var oficloud = require('./oficloud');
var dateFormat = require('dateformat');
var sensor = require("node-dht-sensor");
function mytick()
{
//read data from the sensor
sensor.read(22, 4, function(err, temperature, humidity) {
if (!err) {
console.log('{"temperature":' + temperature.toFixed(1) +
',"humidity": ' + humidity.toFixed(1) + '}'
);
var today0 = new Date();
var today=dateFormat(today0, "yyyy-mm-dd H:MM:ss");
//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);
});
}
else{
console.log(err);
}
});
}
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 (){
});
})
});
$ 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.