Testing the Sensor & the Camera

We are now going to test the sensor first, and then the camera.

Because we want to build an application based on Node.JS, we will use the Node to interface with the DHT11 sensor. To do so, we will use a specialized Node module that already exists.

You will find everything under a folder called sensors_test inside the code of the project. Below is the complete code for this part:

Download: file
var sensorLib = require('node-dht-sensor');
 
var dht_sensor = {
    initialize: function () {
        return sensorLib.initialize(11, 4);
    },
    read: function () {
        var readout = sensorLib.read();
        console.log('Temperature: ' + readout.temperature.toFixed(2) + 'C, ' +
            'humidity: ' + readout.humidity.toFixed(2) + '%');
        setTimeout(function () {
            dht_sensor.read();
        }, 2000);
    }
};
 
if (dht_sensor.initialize()) {
    dht_sensor.read();
} else {
    console.warn('Failed to initialize sensor');
}

The code starts by importing the required module for the DHT sensor. Then, every 2000 ms, we read data from the sensor, and display it inside the terminal using console.log(). The code for this part is available in the GitHub repository of the project:

https://github.com/openhomeautomation/rpi-web-control

It’s now time to test the code. After downloading the code from GitHub, go to the sensors_test folder, and type:

sudo npm install node-dht-sensor

This might take a while, so be patient. After it loads, type:

sudo node sensors_test.js

You should see the values of the temperature and humidity printed in the terminal:

Temperature: 21.00C, humidity: 35.00%

The camera is much easier to test. Simply go to a terminal window and type:

raspistill -o cam.jpg

You can then go to the folder where you executed this command. You should be able to see that a picture was created as cam.jpg.

This guide was first published on Feb 12, 2015. It was last updated on Feb 12, 2015. This page (Testing the Sensor & the Camera) was last updated on May 25, 2019.