Build Your Own Arduino Home Automation System


Build a Home Automation System using ESP8266




We are going to start by setting up the ESP8266 chip. We will learn how to choose the right module for your project and get all the additional hardware you need to use the chip. We will also see how to connect the ESP8266 to your computer so you can program it using a USB cable. Then, we are going to see how to configure and upload code to the ESP8266 chip. For that, we will be using the Arduino IDE. This makes using the ESP8266 much easier, as we will be using a well-known interface and language to configure the chip. We will also be able to use most of the already existing Arduino libraries for our projects. Let's start!

We are first going to see how to choose the right ESP8266 module for your project. There are many modules available in the market and it is quite easy to get lost with all the choices available.

How to choose your ESP8266 module
This is the small ESP8266 Serial Wireless
Transceiver module:




This module is the most famous one, as it is really small and only costs $5. However, the number of accessible GPIO pins (input/output pins) is quite limited. It is also difficult to plug it into a standard breadboard. If you choose this module, you might not be able to do. For example, you won't be able to do the projects using analog sensors, as the analog input pin is not accessible. But there are many other modules on the market that give you access to all the pins of the ESP8266. For example, I really like the ESP8266 Olimex module, which is also cheap (around $10)





This module can easily be mounted on a breadboard and you can easily access all the pins of the ESP8266. One other choice is to use a board based on the ESP-12, which is a version of the ESP8266 made to be integrated on PCBs. This version also gives you access to all the pins of the ESP8266. It is relatively easy to find breakout boards for this chip. For example, this is a board that I bought on Tindie:




Another solution is to use the NodeMCU development kit, which is similar to the Olimex board but also has an integrated USB-to-Serial converter, as well as an onboard power supply. It is easier to use
Parts you will need
• ESP8266 Olimex module (https://www.olimex.com/Products/IoT/MODWIFI-ESP8266-DEV/open-source-hardware)
• Breadboard 3.3V power supply (https://www.sparkfun.com/products/13032)
• 3.3V FTDI USB module (https://www.sparkfun.com/products/9873)
• Breadboard (https://www.sparkfun.com/products/12002)
• Jumper wires (https://www.sparkfun.com/products/12795)

Connect the hardware components
We are now going to take a look at the way to configure the hardware for the first use of your ESP8266 board. This is how we connect the different components:


Depending on the board you are using, the pins can have different names; these are the pins you will need on the small ESP board:




This is the same for the ESP-12 board mounted on a breadboard adapter:





Finally, this is the picture for the Olimex board:



This is what the Olimex board will look like at the end:


Make sure that you connect everything according to the schematics or you won't be able to continue. Also, connect one wire to the GPIO 0 pin of the ESP8266. Don't connect it to anything else for now, but you will need it later to put the chip in programming mode.

Installing the Arduino IDE for the ESP8266

Now that we have completely set up the hardware for the ESP8266, we are ready to configure it using the Arduino IDE.

The most basic way to use the ESP8266 module is to use serial commands, as the chip is basically a Wi-Fi/Serial transceiver. However, this is not convenient and this is not what I recommend doing.

What I recommend is simply using the Arduino IDE, which you will need to install on your computer. This makes it very convenient to use the ESP8266 chip, as we will be using the well-known Arduino IDE
  
We are now going to configure your ESP8266 chip using the Arduino IDE. This is a great way to use the chip, as you will be able to program it using the well-known Arduino IDE and also re-use several existing Arduino libraries.
If this is not done yet, install the latest version of the Arduino IDE. You can get it from http://www.arduino.cc/en/main/software.

Now, you need to take a follow steps to be able to configure the ESP8266 with the
Arduino IDE:
       1. Start the Arduino IDE and open the Preferences window.
2. Enter the following URL into the Additional Board Manager URLs field:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
3. Open Boards Manager from the Tools | Board menu and install the esp8266
platform as shown here:



Connecting your module to your Wi-Fi network now, we are going to check whether the ESP8266 and the Arduino IDE are working correctly, and connect your chip to your local Wi-Fi network. To do so, let's perform the following steps:
1. First, we need to write the code and then upload it to the board. The code is simple; we just want to connect to the local Wi-Fi network and print the IP address of the board. This is the code to connect to the network:
// Import required libraries
#include <ESP8266WiFi.h>
// WiFi parameters
constchar* ssid = "your_wifi_name";
constchar* password = "your_wifi_password";
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
}
You can simply copy the lines of the preceding code and then paste them into the ESP8266 Arduino IDE that you downloaded earlier. Of course, put your own Wi-Fi name and password in the code. Save this file with a name of
your choice. 
2    2. Now, navigate to Tools | Boards and select Generic ESP8266 Module. Also, select the correct Serial port that corresponds to the FTDI converter that you are using.
3. After that, we need to put the board in the bootloader mode so we can program it. To do so, connect the pin GPIO 0 to the ground, via the cable we plugged into GPIO 0. Then, power cycle the board by switching the power
supply off and then on again.

4. Now, upload the code to the board and open the Serial monitor when this is done. Set the Serial monitor speed to 115200. Now, disconnect the cable between GPIO 0 and GND and power cycle the board again. You should see
The following message:
WiFi connected
192.168.1.103

Controlling an LED

First, we are going to see how to control a simple LED. The GPIO pins of the ESP8266 can be configured to realize many functions: inputs, outputs, PWM outputs, and also SPI or I2C communications. This first project will teach you how to use the GPIO pins of the chip as outputs:
1. The first step is to add an LED to our project. These are the extra components you will need for this project:
° 5 mm LED (https://www.sparkfun.com/products/9590)
° 330 Ohm resistor to limit the current in the LED (https://www.sparkfun.com/products/8377)
2. The next step is to connect the LED with the resistor to the ESP8266 board. To do so, the first thing to do is to place the resistor on the breadboard.
3. Then, place the LED on the breadboard as well, connecting the longest pin of the LED (the anode) to one pin of the resistor.
4. Then, connect the other end of the resistor to GPIO pin 5 of the ESP8266, and the other end of the LED to the ground

5. We are now going to light up the LED by programming the ESP8266 chip, just as we did by connecting it to the Wi-Fi network
// Import required libraries
#include <ESP8266WiFi.h>
void setup() {
// Set GPIO 5 as output
pinMode(5, OUTPUT);
// Set GPIO 5 on a HIGH state
digitalWrite(5, HIGH);
}
void loop() {
}
This code simply sets the GPIO pin as an output and then applies a HIGH state to it. The HIGH state means that the pin is active and that positive voltage (3.3V) is applied to the pin. A LOW state would mean that the
output is at 0V.
6. You can now copy this code and paste it into the Arduino IDE.
7. Then, upload the code to the board, 
 You should immediately see that the LED lights up. You can shut it
down again by using digitalWrite(5, LOW) in the code. You could also, for example, modify the code so the ESP8266 switches the LED on and off every second.
Reading data from a GPIO pin
We are going to read the state of a GPIO pin. You can therefore Now, simply connect this pin (GPIO 5) of the board to the positive power supply on your breadboard with a wire, applying a 3.3V signal on this pin
// Import required libraries
#include <ESP8266WiFi.h>
void setup(void)
{
// Start Serial (to display results on the Serial monitor)
Serial.begin(115200);
// Set GPIO 5 as input
pinMode(5, INPUT);}
void loop() {
// Read GPIO 5 and print it on Serial port
Serial.print("State of GPIO 5: ");
Serial.println(digitalRead(5));
// Wait 1 second
delay(1000);
}
We simply set the pin as an input, read the value of this pin, and print it out every second. Copy and paste this code into the Arduino IDE, then upload it to the board This is the result you should get in the Serial monitor:
State of GPIO 5: 1 We can see that the returned value is 1 (digital state HIGH), which is what we expected, because we connected the pin to the positive power supply. As a test, you can also connect the pin to the ground, and the state should go to 0. 

Grabbing the content from a web page

We will simply use the www.example.com
page, as it's a basic page largely used for test purposes
// Import required libraries
#include <ESP8266WiFi.h>
// WiFi parameters
constchar* ssid = "your_wifi_network";
constchar* password = "your_wifi_password";
// Host
constchar* host = "www.example.com";
void setup() {
// Start Serial
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to
Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
delay(5000);
}
The code is really basic: we first open a connection to the example.com website, and then send a GET request to grab the content of the page. Using the while(client. available()) code, we also listen for incoming data, and print it all inside the Serial monitor. This is what you should see in the Serial monitor:


Reading data from a digital sensor

We are going to connect a digital sensor to our ESP8266 chip, and read data from it. As an example, we will use a DHT11 sensor, which can be used to get ambient temperature and humidity. You will need to get this component for this section, the DHT11 sensor  (https://www.adafruit.com/products/386)

1.    1 First, place the sensor on the breadboard. Then, connect the first pin of the sensor to VCC, the second pin to pin 5 of the ESP8266, and the fourth pin of the sensor to GND.


2.    2. It starts by including the required libraries:
       #include "ESP8266WiFi.h"
#include <aREST.h>
#include "DHT.h"
3.  To install those libraries, simply look for them inside the Arduino IDE library manager. Next, we need to set the pin that the DHT sensor is connected to:
#define DHTPIN 5
#define DHTTYPE DHT11
4.  After that, we declare an instance of the DHT sensor:
DHT dht(DHTPIN, DHTTYPE, 15);
5. As earlier, you will need to insert your own Wi-Fi name and password into the code:
const char* ssid = "wifi-name";
const char* password = "wifi-pass";
6. We also defne two variables that will hold the measurements of the sensor:
float temperature;
float humidity;
7. In the setup() function of the sketch, we initialize the sensor:
dht.begin();
8. Still in the setup() function, we expose the variables to the aREST API, so we can access them remotely via Wi-Fi:
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);
9. Finally, in the loop() function, we make the measurements from the sensor:
humidity = dht.readHumidity();
temperature = dht.readTemperature();
10. It's now time to test the project! Simply grab all the code and put it inside the Arduino IDE. Also make sure to install the aREST Arduino library using the Arduino library manager.
11. Now, put the ESP8266 board in bootloader mode, and upload the code to the board. After that, reset the board, and open the Serial monitor. You should see the IP address of the board being displayed:

13. Now, we can access the measurements from the sensor remotely. Simply go to your favorite web browser, and type: 192.168.115.105/temperature You should immediately get the answer from the board, with the
temperature being displayed:
{
"temperature": 25.00,
"id": "1",
"name": "esp8266",
"connected": true
}

After showing you the basics let's build our project which is the home automation system

 Parts you will need
• Adafruit ES8266 module (x3) (https://www.adafruit.com/
products/2471)
• FTDI USB module (https://www.adafruit.com/products/284)
• DHT11 sensor (https://www.adafruit.com/products/386)
• LED (https://www.sparkfun.com/products/9590)
• 330 Ohm resistor (https://www.sparkfun.com/products/8377)
• PIR motion sensor (https://www.sparkfun.com/products/13285)
• Breadboard (x3) (https://www.sparkfun.com/products/12002)
• Jumper wires (x3) (https://www.sparkfun.com/products/9194)

On the software side, you will need the aREST library, the PubSub library, and also the DHT sensor library

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.


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


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.


Controlling your home from a dashboard

We are going to learn how to control all the modules we assembled before from a cloud dashboard, using the aREST framework
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"
#include <PubSubClient.h>
#include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// Unique ID to identify the device for cloud.arest.io
char* device_id = "6g37g4";
// Create aREST instance
aREST rest = aREST(client);
// WiFi parameters
const char* ssid = "wifi-name";
const char* password = "wifi-pass";
// The port to listen for incoming TCP connections
#define LISTEN_PORT 80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Set callback
client.setCallback(callback);
// Give name and ID to device
rest.set_id(device_id);
rest.set_name("dimmer");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Handle REST calls
rest.handle(client);
}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
// Handle
rest.handle_callback(client, topic, payload, length);
}

It's now time to control all our boards from the cloud! First, go over to the aREST dashboard website:
http://dashboard.arest.io/ Create a new dashboard for your home automation system:

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:

Comments

Popular posts from this blog

Embedded Systems

"Gas Alert!" Gas Project Prototype Overview