Building the thermostat

We are now going to see how to build the code for the thermostat, which will run on your Raspberry Pi Zero board. As the code is quite long, I will only highlight the most important parts here, but you can of course find the complete code inside this book's GitHub repository.

Start by importing the required modules:

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

Then, we create an Express app, which will allow us to easily structure our application:

var app = express();

Next, we define some variables that are important for our thermostat:

var targetTemperature = 25;
var threshold = 1;
var heaterPin = 29;

The threshold is here so the thermostat doesn't constantly switch between the on and off states when it is near the target temperature. A lower threshold means that you will have a temperature closer to what you want, but also that the heater will switch more frequently.

After that, we are going to define the routes that will structure our application. The first one is a route to get the thermostat's current target temperature:

app.get('/get', function (req, res) {

  answer = {
    targetTemperature: targetTemperature
  };
  res.json(answer);

});

We will also define another route to set this target temperature, which will be called by the interface we will code in a moment:

app.get('/set', function (req, res) {

  // Set
  targetTemperature = req.query.targetTemperature;

  // Answer
  answer = {
    targetTemperature: targetTemperature
  };
  res.json(answer);

});

Finally, we also need a route to get the current value of the temperature by performing a measurement on the sensor:

app.get('/temperature', function (req, res) {

  answer = {
    temperature: sensor.read().temperature.toFixed(2)
  };
  res.json(answer);

});

Now, we also need to integrate all the code that we will use to control the heater from the Raspberry Pi. We saw this before, when we tested the PowerSwitch Tail:

var piREST = require('pi-arest')(app);
piREST.set_id('34f5eQ');
piREST.set_name('my_rpi_zero');
sd
app.listen(3000, function () {
  console.log('Raspberry Pi Zero thermostat started!');
});

We still need to write the code for the core of the thermostat function. Indeed, we want the Pi Zero to regulate the temperature in your home, whether you are currently using the interface or not. This is done with the following piece of code:

setInterval(function () {

  // Check temperature
  temperature = parseFloat(sensor.read().temperature);
  console.log('Current temperature:' + temperature);
  console.log('Target temperature: ' + parseFloat(targetTemperature));

  // Too high?
  if (temperature > parseFloat(targetTemperature) + 1) {
    console.log('Deactivating heater');
    piREST.digitalWrite(heaterPin, 0);
  }

  // Too low?
  if (temperature < parseFloat(targetTemperature) - 1) {
    console.log('Activating heater');
    piREST.digitalWrite(heaterPin, 1);
  }

}, 10 * 1000);

Basically, we check every 10 seconds and compare the current temperature to the target temperature defined inside the thermostat. If it's too low, for example, we activate the heater.

Finally, we also define the function to read data from the temperature sensor:

var sensor = {
    initialize: function () {
        return sensorLib.initialize(11, 4);
    },
    read: function () {

        // Read
        var readout = sensorLib.read();
        return readout;
    }
};

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

It's now time to test the thermostat! Make sure to grab all the code from this book's GitHub repository, navigate to the folder for this chapter, and type the following:

npm install node-dht-sensor

Then type the following command:

sudo npm install express pi-arest

You can then start the project with the following command:

sudo node thermostat_server.js

You should immediately see a message similar to the following on the console:

Building the thermostat

You can now test all the routes we defined earlier:

You should quickly see the thermostat reacting to this new target by activating the heater:

Building the thermostat

This is great, but every modern thermostat has some kind of interface where you can set the temperature of the thermostat. This is exactly what we are going to do in the final part of this chapter.

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

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