Make a motion-activated lamp using IFTTT

In this section, we are now going to use what we learned in this chapter and combine it with what we already learned in the previous chapters about the web service IFTTT. We are going to use this knowledge to build a lamp that is automatically activated when motion is detected by a motion sensor.

For this section, you will need two Raspberry Pi modules: one with a motion sensor and one connected to a lamp via the PowerSwitch tail. To learn how to assemble these modules, please refer to the previous chapters of the book.

This is the assembled Raspberry Pi Zero with a PIR motion sensor on GPIO18:

Make a motion-activated lamp using IFTTT

We are first going to create the IFTTT recipes so you and the two boards can communicate. First, make sure that the Maker channel is activated on your IFTTT account:

Make a motion-activated lamp using IFTTT

Then, create a new recipe, with the Maker channel as the trigger:

Make a motion-activated lamp using IFTTT

For the event, enter motion_detected:

Make a motion-activated lamp using IFTTT

Choose the Maker channel as the action channel:

For the action itself, choose Make a web request:

Make a motion-activated lamp using IFTTT

We want to activate the lamp if motion is detected, enter the following URL as the action when this recipe is activated:

Make a motion-activated lamp using IFTTT

You can now save this recipe. Of course, we also want to switch the light off again when no motion is detected, so we need to create another recipe with this event:

Make a motion-activated lamp using IFTTT

This action is the same as the previous one, but with a 0 at the end, meaning we are switching the light off:

Make a motion-activated lamp using IFTTT

At the end, you should have both recipes active inside the dashboard:

Make a motion-activated lamp using IFTTT

Let's now see how to configure the boards. For the board connected to the PowerSwitch tail, you can use the same code that we used in the previous sections of this chapter.

For the motion sensor board, the code starts by including the required modules:

var request = require('request');
var gpio = require('rpi-gpio');

Then, we define the IFTTT, as well as the name of the two events we used in recipes:

var key = "key"
var eventOnName = 'motion_detected';
var eventOffName = 'no_motion';

After that, we define the pin on which the sensor is connected:

var motionSensorPin = 18;
var motionSensorState = false;

We create a main measurement loop in which we check the status of the sensor every second:

setInterval(function() {

  // Check sensor
  gpio.setup(motionSensorPin, gpio.DIR_IN, checkSensor);

}, 1000);

If the state of the sensor changes, we send the right command to IFTTT:

function checkSensor() {
  gpio.read(motionSensorPin, function(err, value) {

      // If motion is detected
      if (value == true && motionSensorState == false) {

        // Send event
        alertIFTTT(eventOnName);

      }

      // No motion anymore
      if (value == false && motionSensorState == true) {

        // Send event
        alertIFTTT(eventOffName);

      }

      // Set status
      motionSensorState = value;
  });
}

Here is the detail of the function that sends the alert to IFTTT:

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");
    }
  });
}

You can now grab all the code from the GitHub repository of the book and extract it in a folder on the Pi with the motion sensor. Then, inside a terminal, install the required modules with:

sudo npm install request rpi-gpio

Once that's done, start the software with:

sudo node motion_trigger.js

Also make sure that the aREST sketch is still running the other board. Then, simply pass your hand in front of the sensor; it should immediately light up the lamp that is connected to the other Raspberry Pi board.

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

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