Internet of Things With ESP8266
Internet of Things With ESP8266
Automation System
In this chapter, we are going to use everything we learned so far in the book, and
apply it to the home automation field. We are going to build a simple but complete
home automation system that we will completely control from the cloud, thanks
to the ESP8266 Wi-Fi chip. The system will be composed of a motion sensor, a
temperature and humidity sensor, and an LED dimmer. This way, it will mimic all
the essential components of a real home automation system.
We are going to build three projects based on this system. We are first going to
see how to simply control every component of the system using an online
dashboard. Then, we are going to see how to send automated alarms to your
phone when motion is detected in your home. Finally, we are going to see how to
automate your home using IFTTT and the system we created. Let's start!
The only new component here is a PIR motion sensor that we will use to detect
motion in our home. I used a simple 5V-compatible PIR motion sensor, which is
a pretty standard component.
[1]
Cloud-Based Home Automation System
I listed all the components for this chapter, based on one motion sensor
module, one LED dimmer, and one sensor module. Of course, if you want to use
more of each module, this is no problem, you just need to add more ESP8266
modules to the project.
This is a list of all the components that will be used in this chapter:
On the software side, you will need the aREST library, the PubSub library, and also
the DHT sensor library. We already installed those libraries in previous chapters
of the book, but if that's not done yet, you can simply install them using the
Arduino IDE library manager.
Hardware configuration
We are now going to assemble the different parts of this project. First, we are going
to configure the motion sensor module. For this first module, after placing the
ESP8266 board on the breadboard, connect the VCC pin of the sensor to VCC, GND
to GND, and finally the OUT pin of the sensor to pin number 5 of the ESP8266.
[2]
Chapter 11
Let's now deal with the temperature and humidity module. Place the sensor on the
breadboard, and then connect the first pin to VCC, the second pin to pin number 5 of
the ESP8266 board, and finally the last pin of the sensor to GND.
[3]
Cloud-Based Home Automation System
Let's now assemble the LED dimmer module. Here, we are going to use a simple
LED as the output, but you can of course use this as the starting point of a module to
control more LEDs in your home, or even lamps.
To connect the LED to the ESP8266, simply place the LED in series with the 330
Ohm resistor on the breadboard, the longest pin of the LED in contact with the
resistor.
Then, connect the other end of the resistor to pin 5 of the ESP8266, and connect
the other end of the LED to GND.
First, let's configure all the modules. We are going to start with the LED dimmer
module, which is the easiest to configure. Here is the complete code for this
module:
// Import required libraries
#include "ESP8266WiFi.h"
[4]
#include <PubSubClient.h>
[5]
Chapter 11
#include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// WiFi parameters
const char* ssid = "wifi-name";
const char* password = "wifi-pass";
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Set callback
client.setCallback(callback);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
[6]
Cloud-Based Home Automation System
void loop() {
// Handle
rest.handle_callback(client, topic, payload, length);
There are several things you need to modify in this code. You need to substitute
the Wi-Fi name and password for your own. You also need to modify the device ID
of the device, so it has a unique identifier on the network. Finally, you can also
modify the name of the device, for example to add some data about where the
module is located in your home.
Once that's all done, upload the code to the board, and then move on to the next
device: the motion sensor.
For this module, the code is nearly the same, we just need to add some lines to
constantly measure the state of the motion sensor and make it available on the
cloud. For that, we first define a variable that will host the motion sensor state:
int motion;
[7]
Chapter 11
Next, inside the loop() function of the sketch, we simply measure the state of the
motion sensor:
motion = digitalRead(5);
After modifying the same parameters as for the LED module (Wi-Fi
credentials, device ID, and name), upload the code to the board.
Finally, let's deal with the sensor module. For this one, you will need to import the
DHT library:
#include "DHT.h"
Then, you will need to define which pin the sensor is connected to:
#define DHTPIN 5
#define DHTTYPE DHT11
We also create two variables that will hold the value of the measured temperature
and humidity:
float temperature;
float humidity;
Finally, inside the loop() function of the sketch, we measure the temperature and
humidity of the sensor:
humidity = dht.readHumidity();
temperature = dht.readTemperature();
Again, modify the required parameters inside the sketch and upload it to the board.
Note that to power all the modules, you can, for example, use either an external
battery or a breadboard power supply; you do not need to have one FTDI cable
per module.
[8]
Cloud-Based Home Automation System
It's now time to control all our boards from the cloud! First, go over to the aREST
dashboard website:
http://dashboard.arest.io/
Inside this dashboard, switch to edit mode and add the first element that will hold
the temperature measurement. Make sure to enter the correct ID of your temperature
and humidity module:
Next, do the same for humidity, and you should get the following result:
We are now going to add the LED dimmer module. As we want to be able to control
the intensity of the LED light, create a new element with the Analog option:
You should now be able to control the intensity of the LED via a slider inside
the dashboard:
[9]
Chapter 11
You should end up with a dashboard that has all the elements of the simple
home automation system we just built:
[ 10 ]
Cloud-Based Home Automation System
I will show you how to do it with just one PIR sensor, but you can of course add
more modules that will be dispersed around your home or any building you wish
to monitor. To do that, we are going to again use IFTTT, which will send you a text
message every time motion is detected by any of the motion sensor modules.
Let's first see how to configure a given module. As it's code that is very similar to
what we already saw earlier in this book, I will only highlight the most
important parts here.
You need to set the key that is linked to your Maker channel on IFTTT:
const char* host = "maker.ifttt.com";
const char* eventName = "motion_detected";
const char* key = "key";
Then, inside the loop() function of the sketch, we read the status of
the motion sensor:
bool motion = digitalRead(5);
[ 11 ]
Chapter 11
We then wait for a long time before sending new alerts; otherwise we'll just send
a lot of messages to your mobile phone:
delay(10 * 60 * 1000);
Now, grab the code (for example from the GitHub repository for the book), modify it
with your own credentials, and upload it to the board.
https://ifttt.com/
[ 12 ]
Cloud-Based Home Automation System
As the event, insert motion_detected, which is also what we put in the code:
As the action channel, we'll select SMS here, as it will be the fastest way to contact
you in case an alarm is triggered by your motion sensor:
You can insert whatever you wish as the message, for example:
[ 13 ]
Chapter 11
Now, create the recipe and activate it. You should see that whenever you pass your
hand in front of the sensor, it will almost immediately send an alert message to
your phone. If you wish to stop your alarm system, it's then as simple as
deactivating the recipe on IFTTT.
You can of course add more sensors to the system, making each publish the same
alert on IFTTT. Therefore, whenever any sensor detects motion, you will receive
an alert on your phone.
The Maker channel of IFTTT can also be used as an action channel, and this is
what we are going to use here. We will use it to call the aREST API whenever a
given condition is triggered.
[ 14 ]
Cloud-Based Home Automation System
We are first going to configure the module again, so it can receive commands
from the cloud. This is the part of the code before the setup() function:
// Import required libraries
#include "ESP8266WiFi.h"
#include <PubSubClient.h>
#include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// WiFi parameters
const char* ssid = "wifi-name";
const char* password = "wifi-pass";
// Set callback
client.setCallback(callback);
[ 15 ]
Chapter 11
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Handle
rest.handle_callback(client, topic, payload, length);
Now, modify the important credentials (Wi-Fi name and password, and device ID)
in the code, and upload it to the board.
Then, go back to IFTTT. The first thing we are going to do is to make a project to
light up the LED at a given time (for example, when it's becoming dark outside), and
then it off again at another time (for example, when you go to bed).
[ 16 ]
Cloud-Based Home Automation System
For that, create a new recipe with Date & Time as the channel. You might need
to connect it first if you have never used it before:
For the trigger type, select Every day, and enter the time at which you want the LED
to turn on.
Then, for the action channel in IFTTT, select the Maker channel, and then select
Make a web request. This will allow IFTTT to send a command to the
aREST.io cloud server.
As the request, enter the following parameters, of course changing the device ID
with the one you used inside the sketch:
[ 17 ]
Chapter 11
Now, do the same with the time you want the LED to turn off, for example
at 11.30 PM:
[ 18 ]
Cloud-Based Home Automation System
For the web request, enter the same parameters as before, but this time with the
command to switch pin number 5 to LOW:
[ 19 ]
Chapter 11
It's now time to see the recipe in action! Make sure the ESP8266 board is correctly
configured, and also that the recipes are activated in IFTTT. Whenever the time
conditions are met, you should immediately see the LED turn on or off. You can,
of course, modify the times inside the recipes to test them.
Now, let's play with another trigger channel to see how powerful IFTTT is. You can,
for example, use the Weather channel to check whether it's sunset, to automatically
turn on the LED at this time without having to enter a fixed time.
[ 20 ]
Cloud-Based Home Automation System
To do so, create a new recipe and select the Weather channel, which you need
to connect to:
To connect the Weather channel, you simply need to enter your current location:
[ 21 ]
Chapter 11
[ 22 ]
Cloud-Based Home Automation System
Also, just as in the previous recipe, I decided to turn the LED on whenever the recipe
is triggered:
You should now see that whenever sunset approaches, the LED will automatically
turn on. You can of course play with more conditions inside IFTTT, and even use
the Maker channel as both the trigger and the action channel, to link the different
modules of your home automation system via IFTTT.
[ 23 ]
Chapter 11
Summary
In this chapter, we built the
different components of a
home automation system
based on the ESP8266, and we
saw how to control everything
from the cloud. We first
created a cloud dashboard to
control all the devices from a
single interface. Next, we used
IFTTT again to create an alarm
system that automatically
sends you alerts on your phone
whenever motion is detected.
The easiest way to build your
own system based on this
project is to simply add more
modules, so it suits the needs
you have in your own home.
For example, it is really easy to
add several motion sensor
modules so you can have a
complete alarm system
deployed in your home, and
that you can manage using a
simple web browser.
[ 24 ]