Testing individual components

As the first project of this chapter, we are simply going to check that each individual component (the sensor and the PowerSwitch tail) are working correctly.

I will now go through the main parts of this first piece of code. It starts by including the DHT sensor module for Node.js:

var sensorLib = require('node-dht-sensor');

Then, we create an object to read data from the sensor, and also initialize it when we start the software:

var 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 () {
            sensor.read();
        }, 2000);
    }
};

if (sensor.initialize()) {
    sensor.read();
} else {
    console.warn('Failed to initialize sensor');
}

You can now either copy the code inside a file called sensor_test.js, or just get the complete code from the GitHub repository of the project:

https://github.com/openhomeautomation/smart-homes-pi-zero

Next, use the terminal to navigate to the folder where the files are, and type the following:

npm install node-dht-sensor

This will install the module to read data from the sensor; it can take a while, so be patient. In case it doesn't work, try using sudo in front of the command. Next, actually start the software with the following command:

sudo node sensor_test.js

This should print the readings of the sensor at regular intervals inside the terminal:

Testing individual components

We are now going to see how to test if the PowerSwitch Tail is working and wired correctly, and how to control it remotely. For that, we are going to use the aREST module for the Raspberry Pi, which will give us an easy way to control the outputs of the board.

Here is the complete code for this part:

// Start
var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);

piREST.set_id('34f5eQ');
piREST.set_name('my_rpi_zero');

var server = app.listen(80, function() {
    console.log('Listening on port %d', server.address().port);
});

The code is pretty simple, and we are going to try it right now. First, you need to install the required modules by typing the following in a terminal (where the files of the project are located):

sudo npm install express pi-arest

Then, simply start the application with the following command:

sudo node heaer_test.js

You should now see the confirmation in your console.

Now, let's go ahead and try to control the heater, for example, to turn it on. You need to make sure that it is actually turned on if there is any mechanical switch on the heater itself.

Then, go to your favorite web browser, and type the following:

http://raspberrypi.local/digital/29/1

You should see that the heater turns on instantly, and you should also have a confirmation inside your web browser. Then, type the following command to turn it off again:

If that works, you can now control your heater from your Raspberry Pi Zero! You can now move to the next section, in which we are going to code the thermostat.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset