Build an automated cloud thermostat

In the last section of this chapter, we are going to apply what we learned in the previous section, but this time to build a cloud thermostat that will work using IFTTT.

Apart from the Raspberry Pi Zero that will control an electrical heater via the PowerSwitch Tail, you will need another Raspberry Pi Zero with a DHT11 sensor that we have already used several times in this book. In order to assemble this module, I recommend checking for example the second chapter of this book.

Once you have your two modules assembled, go again to IFTTT and create a new recipe, using the Maker channel for the trigger and for the action channels.

For the trigger, enter the following event:

Build an automated cloud thermostat

Of course, if the temperature is too low, it means that we want to activate the heater. We therefore need to send this command to the board that controls the heater:

Build an automated cloud thermostat

Once this recipe is created, create another for the temperature_high event:

Build an automated cloud thermostat

When the temperature is too high, we automatically switch off the heater:

Build an automated cloud thermostat

At the end, you should have two new recipes in your dashboard:

Build an automated cloud thermostat

For the board that controls the electrical heater, just use the same software as before.

For the board with the DHT11 sensor, we first need to include the required modules:

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

Then, we define the key for the Maker channel and the name of the two events:

var key = "key";
var eventNameLow = 'temperature_low';
var eventNameHigh = 'temperature_high';

We also declare on which pin the DHT11 sensor is connected to:

var sensorPin = 18;

Then, we set a target (the temperature we want to reach) and a tolerance:

var target = 25;
var tolerance = 1;

After that, we create the main measurement loop, in which we check for the right event to send to IFTTT:

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

        // Read
        var readout = sensorLib.read();
        temperature = readout.temperature.toFixed(2);
        console.log('Current temperature: ' + temperature);

          if (temperature < target - tolerance) {

            // Send event
            alertIFTTT(temperature_low);

          }

          if (temperature > target + tolerance) {

            // Send event
            alertIFTTT(temperature_high);

          }

        // Repeat
        setTimeout(function () {
            sensor.read();
        }, 2000);
    }
};

We also end the sketch by initializing the sensor:

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

Here are the details of the function that we use to make a request:

// Make request
function alertIFTTT(eventName) {

  // Send alert to IFTTT
  console.log("Sending alert to IFTTT");
  var url = 'https://maker.ifttt.com/trigger/' + eventName + '/with/key/' + key;
  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log("Alert sent to IFTTT");
    }
  });
}

It's now time to test the thermostat project! Simply get all the code from the GitHub repository of the project and then type:

sudo npm install node-dht-sensor request

Once that's done, start the software with:

sudo node temperature_trigger.js

You should now be able to see that the Raspberry Pi, with the electrical heater control, will automatically react based on the temperature, even if both boards are not in the same Wi-Fi network!

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

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