Real time temperature sensing with Rasperry Pi and NodeJS

For this post I used as Usual the groove pi kit, and as in the last post, the same libraries.

In order to read from the sensor, be sure that you connect the sensor to a digital input/output in the groove pi board, and take note of the number, then be sure the second parameter is DHT11 (blue sensor)

var dhtSensor = new DHTDigitalSensor(7, DHTDigitalSensor.VERSION.DHT11, DHTDigitalSensor.CELSIUS)

Thats how we instantiate the sensor, then we can read it like this:

   dhtSensor.on('change', function(res) {
   var temp= res[2];

The result is an array with 3 values, first one, is a heat index I belibve, 2nd one is humidity, and last one is temperature, in celsius or fahrenheit as wished according to the constructor.

Then, I wanted this to interact with my LCD Screen, If its above 28 degrees, I wanted to make the LCD red, if its between 27 and 28 yellow and if its below 27 I wanted to make it blue.

Full code is here:

var i2c = require('i2c-bus')  
var sleep = require('sleep')  
var GrovePi = require('node-grovepi').GrovePi  
var DHTDigitalSensor = GrovePi.sensors.DHTDigital


var Board = GrovePi.board

var DISPLAY_RGB_ADDR = 0x62;  
var DISPLAY_TEXT_ADDR = 0x3e;

function setRGB(i2c1, r, g, b) {  
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,0,0)
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,1,0)
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,0x08,0xaa)
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,4,r)
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,3,g)
  i2c1.writeByteSync(DISPLAY_RGB_ADDR,2,b)
}

function textCommand(i2c1, cmd) {  
  i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x80, cmd);
}

function setText(i2c1, text) {  
  textCommand(i2c1, 0x01) // clear display
  sleep.usleep(50000);
  textCommand(i2c1, 0x08 | 0x04) // display on, no cursor
  textCommand(i2c1, 0x28) // 2 lines
  sleep.usleep(50000);
  var count = 0;
  var row = 0;
  for(var i = 0, len = text.length; i < len; i++) {
    if(text[i] === '\n' || count === 16) {
      count = 0;
      row ++;
        if(row === 2)
          break;
      textCommand(i2c1, 0xc0)
      if(text[i] === '\n')
        continue;
    }
    count++;
    i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x40, text[i].charCodeAt(0));
  }
}

var board = new Board({  
    debug: true,
    onError: function(err) {
      console.log('Something wrong just happened')
      console.log(err)
    },
    onInit: function(res) {
      if (res) {
        console.log('GrovePi Version :: ' + board.version())
         var dhtSensor = new DHTDigitalSensor(7, DHTDigitalSensor.VERSION.DHT11, DHTDigitalSensor.CELSIUS)


        // DHT Sensor
        console.log('DHT Digital Sensor (start watch)')
        dhtSensor.on('change', function(res) {
            var temp= res[2];
            //var humidity = res[1];
            console.log(res);
            var i2c1 = i2c.openSync(1);
            if(temp>28){
                setText(i2c1, 'Temp:' + temp);
                setRGB(i2c1, 255,0, 0);
            }
            else if (temp>27 && temp <28){
                setText(i2c1, 'Temp:' + temp);
                setRGB(i2c1, 255,255, 0);
            }
            else{
                setText(i2c1, 'Temp:' + temp);
                setRGB(i2c1, 0, 0, 255);
            }



            i2c1.closeSync();
        })
        dhtSensor.watch(500) // milliseconds


      }
    }
  })
board.init();