Automating your home

Now that we have our central server running and you can use it to control & monitor your home from a single interface, we can actually define some behaviors inside the server in order to create some automation within your smart home.

As an example, we are going to automatically switch on the lamp when motion is detected by the sensor. You can imagine the scenario in which the appliance control module is connected to a lamp in your hallway and that you want it to automatically switch on whenever a movement is detected by the motion sensor.

For that, here is the code you need to add into the server code:

setInterval(function() {

  // Check sensor
  request("http://" + motionSensorPi + "/digital/" + motionSensorPin,
    function (error, response, body) {

      // If motion was detected
      if (body.return_value == true) {

        request("http://" + lampPi + "/digital/" + lampPin + '/1');

      }
      else if {

        request("http://" + lampPi + "/digital/" + lampPin + '/0');

      }

  });

}, 1000);

Let's now see what this code does exactly. We basically check the state of the motion sensor every second and then control the lamp accordingly. Note that every request is taking the correct IP address of the Raspberry Pi board to control.

You can now use the same approach to build more complex behaviors into your home, for example by linking the measurements made by the sensor module to an appliance or an electrical heater, which we already saw earlier in the book.

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

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