Example: temperature sensor

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:

  • An Oficloud gateway based on a Raspberry Pi model 3 B (or B+). See instructions.
  • A DHT22-compatible temperature sensor.
  • 3 female-to-female cables.

You also need to choose between one of these options:

  • Configure an SSH connection to the gateway.
  • Configure a remote connection (VNC, etc) to the gateway.
  • Connect the gateway to a screen using an HDMI cable.


1. Connect sensor
Connect the sensor to the gateway. Follow instructions at this link.

2. Create base project and channel
Follow Quick Start guide to install a base project and create an Oficloud channel for communications.

3. Edit package configuration file
Edit package.json so that it looks like this .

    "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"

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

5. Edit script
Edit main.js to add the code to read the temperature and humidity sensor every 30 seconds and post the data to 'MYCHANNEL'.

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("exited timeout: "+err.e);
                //call tick again with 30 seconds delay

function init(ondone)
    //call tick after 1 seconds

    //call return function

    console.log("login res: "+res.res);

    function onmsg(msg)
        //Process received messages here

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

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

        init(function (){



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.