Building a light-activated relay

Now that we know how to build M2M projects using the ESP8266, we are going to apply it to a simple project, in which you won't even have to press a push button at all. We are going to build a light-activated relay that will control a relay connected to one ESP8266 module depending on the light level measured by another ESP8266. This project will simulate a part of a home automation system, where you automatically want to switch the lights on when it's getting dark outside.

First, we need to assemble both boards. Let's start with the board that will host the relay, as it is the easiest one to assemble:

  • Simply connect the VCC pin of the relay to the VCC pin of the ESP8266
  • GND to GND
  • SIG or EN pin of the relay to pin 5 of the ESP8266

This is the result for this board:

Building a light-activated relay

For the board that will host the photocell, first place the ESP8266 on the breadboard, and then the 10K Ohm resistor in series with the photocell on the breadboard as well. Then, connect the common pin of the photocell and the resistor to pin A or ADC of the ESP8266, which is the analog input pin on the ESP8266 board. Then, connect the other end of the photocell to VCC, and the other end of the resistor to GND.

This is the final result for this board:

Building a light-activated relay

Let's now see how to configure those boards. For the relay board, you can simply use the same sketch as for the LED board in the previous project, as we are again going to use the aREST cloud server.

For the photocell board, it will be very similar to the push button board in the first project, so we will only talk about the main differences here.

First, we set a variable that will define whether the light level is currently low or high. We'll set it to low by default, by assigning a false value to the variable:

bool lightLevel = false;

In the loop() function of the sketch, we first measure the state of the analog pin, and we print it on the Serial monitor:

Serial.print("Light level: ");
Serial.println(analogRead(A0));

Next, we check whether the light level is below a given threshold, and also whether the previous light level state was high. Note that the threshold is arbitrary here, it is just to illustrate M2M communications between the two boards. If we are indeed in those conditions, we send an event, light_level_low:

if (analogRead(A0) < 700 && lightLevel) {

    lightLevel = false;
    makeRequest("light_level_low");
  
  }

On the other end, if the light level is high, and we were in the low state before, we send a light_level_high event to IFTTT:

if (analogRead(A0) > 800 && !lightLevel) {

    lightLevel = true;
    makeRequest("light_level_high");
  
  }

You can now configure the board with the photocell using this code, which you can also find in the GitHub repository for the book. Again, we now need to create recipes on IFTTT to link the two boards.

Go to IFTTT again and create a new recipe. Again, use the Maker channel as the trigger, and set the event name to light_level_low as defined in the sketch:

Building a light-activated relay

For the action, select the Maker channel again, and create a web request with the following parameters:

Building a light-activated relay

Here, we are setting pin 5 to a high state on the target board, as we want to switch the relay on if the light levels are low. Of course, make sure to change the device ID of your device inside the URL to be called by IFTTT.

Create the recipe, and do exactly the same with the other scenario by creating another recipe. This time, choose light_level_high as the trigger event:

Building a light-activated relay

For the action, enter exactly the same request as before, but this time with a 0 at the end, as we want to switch the lights off if the light level gets high again:

Building a light-activated relay

You should now see both recipes active inside your IFTTT dashboard:

Building a light-activated relay

You can now test the project! To simulate it being dark outside, just place your hand on top of the photocell. You should see that 2-3 seconds after that, the relay should automatically switch to an active state on the other board. If you remove your hand, the relay should switch off again. Note that because we are using M2M communications via a cloud service, both boards can of course be placed in different locations. For example, you could easily have one board outside and one inside your home, even if that means they are connected to different Wi-Fi networks.

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

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