Home Automation With ESP8266 and Relay Controller

by Dcube Tech Ventures in Circuits > Sensors

5347 Views, 52 Favorites, 0 Comments

Home Automation With ESP8266 and Relay Controller

Home Automation All Parts.jpg

Everyone loves a good sunny day. And if there are some cool breeze and good music, then that's the best feeling in the world. But when the sun goes down, we all hate it when we have to go in each room and switch on the lights. It's like an irritating job that we have to do. A simple solution for this is to convert your Simple Homes into Smart Homes with Home-Automation.

Home-Automation is currently the hottest topic of Internet-of-Things (IoT). From new start-up's ventures to the big giant's organization, all are working in this field. Every day, we see new technologies and devices that are introduced in the market. But, most of them are way too expensive and some are not helpful.

So, we picked some tools and made a project which is accessible and affordable for you all. And with this, you can also make your house - a Smart Home.

Let's start then.. !!

Tools We Need

TSL45315.jpg
Relay.jpg
ESP8266 Side 1.jpg
USB Programmer Side 2.jpg
I2C Cable.jpg

1. TSL45315 Light Sensor

The primary tool we need is a - Light Sensor with a powerful range. The TSL45315 device provides ambient light sensing (ALS), giving direct lux output that approximates human eye response under a variety of lighting conditions. It comes with a wide dynamic range - 3 lux to 220k lux.

2. MCP23008 Relay Controller 8-Channel

MCP23008 from Microchip is an Integrated port expander that controls eight relays through the I²C bus. You can add more relays, digital I/O, analog to digital converters, sensors, and other devices using the integrated I²C expansion port.

3. Adafruit Huzzah ESP8266

The ESP8266 is an incredible platform for IoT application development. The ESP8266 processor from Espressif is an 80 MHz microcontroller with a full WiFi front-end and TCP/IP stack with DNS support as well. The ESP8266 provides a mature platform for monitoring and control applications using the Arduino Wire Language and the Arduino IDE.

4. ESP8266 USB Programmer

This ESP8266 host adapter was created specifically by us for the Adafruit Huzzah version of the ESP8266, allowing I²C communication connections.

5. I²C Connection Cable

We also designed the I²C connection cable which is available at Contol Everything.

6. Mini USB Cable

The mini USB cable Power supply is a perfect choice for powering the Adafruit Huzzah ESP8266.

7. 12V Power Adapter

MCP23008 Relay controller ideally works on 12V and this can be simply given by a 12V Power Adapter.

Hardware Connections

ESP-USB Conn.jpg
TSL45315 Cable Conn.jpg
TSL54315 ESP Conn.jpg
TSL45315 Relay Conn.jpg

In general, making connections is the simplest part of this project. Follow the instructions and images, and you should have no problems.

1. Connection of Adafruit Huzzah ESP8266 and USB Programmer

First of all, take the Adafruit Huzzah ESP8266 and place it on the USB Programmer (with Inward Facing I²C Port). Press the ESP8266 gently into the USB Programmer and we are done with this step (See picture #1).

2. Connection of the Sensor and Adafruit Huzzah ESP8266

Take an I²C Cable and connect it to the Input port of the Sensor. For proper operation of this cable, please remember I²C Output ALWAYS connects to the I²C Input. Now, connect the other end of the same I²C Cable to the USB Programmer with Adafruit Huzzah ESP8266 mounted over it (See picture #3).

Note: The brown wire should always follow the Ground (GND) connection between the output of one device and the input of another device.

3. Connection of the Sensor and MCP23008 Relay Controller

Take another I²C Cable, connect one end to the Output port of the Sensor and another end on the Input port of the MCP23008 Relay Controller.

4. Powering of the Circuit

Plug in the Mini USB cable into the power jack of Adafruit Huzzah ESP8266. Also, don't forget to power up the Relay Controller with 12V Power Adapter. Plug it in and voila, we are good to go!

5. Connecting Home Appliances to MCP23008 Relay Controller

Connect all your home appliances that you wish to control on the relay part of the MCP23008 Relay Controller. Make sure you take all the precautions before doing this step.

Code

The code for the MCP23008 Relay Controller and TSL45315 Sensor is available on our GitHub repository.

Before going on to the code, make sure you read the instructions given in the Readme file and setup your Adafruit Huzzah ESP8266 accordingly. It will take just 5 minutes to set up the ESP.

For your convenience, you can copy the working ESP code for this sensor from here also:

<p>// Distributed with a free-will license.<br>// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// Home Automation with ESP8266
// This code is designed to work with the TSL45315_I2CS I2C Mini Module available from Dcubestore.com.
// http://dcubestore.com/product/tmg39931-light-sensor-gesture-color-als-and-proximity-sensor-i%C2%B2c-mini-module/
// http://dcubestore.com/product/i%C2%B2c-relay-controller-8-channel-spdt-10-amp/</p><p>#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h></p><p>// TSL45315 I2C address is 0x29(41)
#define Addr_Sensor 0x29
// MCP23008_I2CR8G5LE I2C address is 0x20(32)
#define Addr_Relay 0x20</p><p>const char* ssid = "your ssid network";
const char* password = "your password";
int luminance;</p><p>ESP8266WebServer server(80);</p><p>void handleroot()
{
  unsigned int data[2];</p><p>  // Start I2C Transmission
  Wire.beginTransmission(Addr_Relay);
  // Select IODIR register
  Wire.write(0x00);
  // All pins are configured as output
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);</p><p>  // Start I2C Transmission
  Wire.beginTransmission(Addr_Sensor);
  // Select control register
  Wire.write(0x80);
  // Normal operation
  Wire.write(0x03);
  // Stop I2C transmission
  Wire.endTransmission();</p><p>  // Start I2C Transmission
  Wire.beginTransmission(Addr_Sensor);
  // Select configuration register
  Wire.write(0x81);
  // Multiplier 1x, Tint : 400ms
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);</p><p>  // Start I2C Transmission
  Wire.beginTransmission(Addr_Sensor);
  // Select data register
  Wire.write(0x84);
  // Stop I2C transmission
  Wire.endTransmission();</p><p>  // Request 2 bytes of data
  Wire.requestFrom(Addr_Sensor, 2);</p><p>  // Read 2 bytes of data
  // luminance lsb, luminance msb
  if (Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }</p><p>  // Convert the data
  luminance = data[1] * 256 + data[0];</p><p>  // Output data to Serial Monitor
  Serial.print("Ambient Light Luminance :");
  Serial.print(luminance);
  Serial.println(" lux");
  delay(300);</p><p>  // Output data to Web Server
  server.sendContent
  ("<html><head><meta http-equiv='refresh' content='2'</meta>"
   "<h1 style=text-align:center;font-size:300%;color:blue;font-family:britannic bold;>CONTROL EVERYTHING</h1>"
   "<h3 style=text-align:center;font-family:courier new;><a href=http://www.controleverything.com/ target=_blank>www.controleverything.com</a></h3><hr>"
   "<h2 style=text-align:center;font-family:tahoma;><a href=https://www.controleverything.com/content/Light?sku=TSL45315_I2CS#tabs-0-product_tabset-2 \n"
   "target=_blank>TSL45315 Sensor I2C Mini Module</a></h2>");
  server.sendContent
  ("<h3 style=text-align:center;font-family:tahoma;>Ambient Light Luminance : " + String(luminance) + " lux");</p><p>  if (luminance < 200)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr_Relay);
    // Select GPIO register
    Wire.write(0x09);
    // All pins are set to logic HIGH
    Wire.write(0xFF);
    // Stop I2C transmission
    Wire.endTransmission();</p><p>    // Output data to serial monitor
    Serial.print("House Lights : ON");</p><p>    // Output data to Web Server
    server.sendContent
    ("<h3 style=text-align:center;font-family:tahoma;> House Lights : ON");
  }
  else
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr_Relay);
    // Select GPIO register
    Wire.write(0x09);
    // All pins are set to logic HIGH
    Wire.write(0x00);
    // Stop I2C transmission
    Wire.endTransmission();</p><p>    // Output data to serial monitor
    Serial.print("House Lights : OFF");</p><p>    // Output data to Web Server
    server.sendContent
    ("<h3 style=text-align:center;font-family:tahoma;> House Lights : OFF");
  }
}</p><p>void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin(2, 14);
  // Initialise serial communication, set baud rate = 115200
  Serial.begin(115200);</p><p>  // Connect to WiFi network
  WiFi.begin(ssid, password);</p><p>  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);</p><p>  // Get the IP address of ESP8266
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());</p><p>  // Start the server
  server.on("/", handleroot);
  server.begin();
  Serial.println("HTTP server started");
}</p><p>void loop()
{
  server.handleClient();
}</p>

Working of the Code

Output5.gif

Download (git pull) or copy the code and open it in the Arduino IDE.

Compile and Upload the code and see the output on your Serial Monitor.

Note: Before uploading, make sure you enter your SSID network and password in the code.

Now, copy the IP address of ESP8266 from the Serial Monitor and paste it in your web browser. You will see a web page with ambient light luminance reading and status of your home appliances. When the luminance is below a particular value, say at the time of a sunset, it will automatically turn ON the lights and other devices you desire that are present in your home.

The output of the sensor on the Serial Monitor and Web Server are shown in the picture above.

Applications & Upgrading

With this, you can control the devices and monitor their performance from your desktops and mobile devices. You can store and manage the data online and study it anytime for changes. More applications include Mesh Network, Industrial Wireless Control, Baby Monitors, Sensor Networks, Wearable Electronics, Wi-Fi Location-aware Devices, Wi-Fi Position System Beacons.

You can also upgrade the project by adding an ON/OFF button on the web page for all the device for controlling them individually. Also, you can add more sensor like Temperature and Humidity Sensors for controlling devices like AC or Heater. There are many possibilities by which you can upgrade the project and make your simple home into - a Smart Home.

Resources for Going Further

For more information about TSL45315, MCP23008 Relay Controller and ESP8266, check out the links below: