Book Image

ESP8266 Home Automation Projects

By : Catalin Batrinu, Constantin Tambrea
Book Image

ESP8266 Home Automation Projects

By: Catalin Batrinu, Constantin Tambrea

Overview of this book

The ESP8266 is a low-cost yet powerful Wi-Fi chip that is becoming more popular at an alarming rate, and people have adopted it to create interesting projects. With this book, you will learn to create and program home automation projects using the ESP8266 Wi-Fi chip. You will learn how to build a thermostat to measure and adjust the temperature accordingly and how to build a security system using the ESP8266. Furthermore, you will design a complete home automation system from sensor to your own cloud. You will touch base on data monitoring, controlling appliances, and security aspects. By the end of the book, you will understand how to completely control and monitor your home from the cloud and from a mobile application. You will be familiar with the capabilities of the ESP8266 and will have successfully designed a complete ready-to-sell home automated system.
Table of Contents (16 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback
Building a Home Thermostat with the ESP8266
Using ESP8266 to Build a Security System

It is time for your first program

To begin this, let's evaluate the basic input and output of the Witty ESP8266 module.

The definition of pins is Light Dependent Resistor (LDR) on Witty and is attached to A0 (the analog input), the push button is connected to GPIO 4, and the LEDs are connected to GPIO 12, GPIO 13, and GPIO 15:

Delete everything that is in your Arduino IDE and replace it with the following code:

#define LDR     A0 
#define BUTTON  4 
#define RED     15 
#define GREEN   12 
#define BLUE    13 

The setup section will run only once after the module is reset or powered. The serial UART is started with 115200 bps, so the messages can be seen in the Serial Monitor window, where you also need to set the same speed in the lower-right corner of the window; otherwise, weird characters will be seen.

All pins are defined as INPUT or OUTPUT depending on their usage. The button and LDR are configured as input pins and all LED connected pins are set as output:

void setup()  
pinMode(LDR, INPUT); 
pinMode(BUTTON, INPUT); 
pinMode(RED, OUTPUT); 
pinMode(GREEN, OUTPUT); 
pinMode(BLUE, OUTPUT); 

The loop() function is continuously running after the setup() and, in it:

  1. The analogRead function reads the value of the ambient light provided as 0-1 V by the LDR.
  2. The digitalRead function reads the value of GPIO 4, that can be either 0 V when the button is pressed or VCC 3.3 V if the button is not pressed.
  3. Show the data to the Serial Monitor with the Serial.print function. Serial.println just adds a new line.
  4. Write a random value between 0 and 1023 to GPIO 15 and GPIO 12 that will control the red and green LED color intensity. This is Pulse Width Modulation (PWM).
  5. Turn on the blue LED connected to GPIO 13.
  6. Wait 1000 ms (one second).
  7. Turn off the blue LED and continue from step 1:
void loop() 
Serial.print("LDR: "); 
Serial.print("BUTTON: "); 
analogWrite(RED,   random(0,1023)); 
analogWrite(GREEN, random(0,1023)); 
digitalWrite(BLUE, HIGH); 
digitalWrite(BLUE, LOW); 

In order to compile and flush the binary into the ESP8266 chip you need to press the Upload button.

Seeing the result

In the Serial Monitor output, as shown in the following image, we can see the values for the ambient light and the status of the button, where 0 means pressed and 1 means not pressed:

If you don't have a Witty module, you will need some extra parts such as resistors, LED, push buttons, and LDR sensors, according to the following schematics:

Let's review now the functions that allow you to control GPIO pins and the function that will print values in the Serial Monitor:

  • analogRead(pin): This reads the value on the A0 pin
  • digitalRead(pin): This reads the value for a specified pin, either LOW or HIGH
  • digitalWrite(pin, value): This writes a LOW or HIGH value to a digital pin
  • Serial.println (val): This prints data to a serial port as human-readable ASCII characters ending with \r and a new line character \n


Using analogWrite(val), where val can be in the 0 to 1023 interval, a PWM digital output pin will have a voltage between 0 and 3.3V in 1023 steps.