» » » Home Automation With ESP8266 and Relay Controller

Home Automation With ESP8266 and Relay Controller

posted in: Others, Sensors | 0

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 yourSimple 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.. !!

Step 1: Tools We Need

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.

tsl45315

 

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.

relay

 

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.

esp8266-side-1

 

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.

usb-programmer-side-1

 

5. I²C Connection Cable

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

i2c-cable

 

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.

Step 2: Hardware Connections

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).

esp-usb-conn

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 theUSB Programmer with Adafruit Huzzah ESP8266 mounted over it (See picture #3).

tsl45315-cable-conn

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.

tsl54315-esp-conn

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!

tsl45315-relay-conn

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 precautionsbefore doing this step.

Step 3: 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 theReadme 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:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>

// TSL45315 I2C address is 0x29(41)
#define Addr_Sensor 0x29
// MCP23008_I2CR8G5LE I2C address is 0x20(32)
#define Addr_Relay 0x20

const char* ssid = "your ssid network";
const char* password = "your password";
int luminance;

ESP8266WebServer server(80);

void handleroot()
{
 unsigned int data[2];

 // 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);

 // Start I2C Transmission
 Wire.beginTransmission(Addr_Sensor);
 // Select control register
 Wire.write(0x80);
 // Normal operation
 Wire.write(0x03);
 // Stop I2C transmission
 Wire.endTransmission();

 // 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);

 // Start I2C Transmission
 Wire.beginTransmission(Addr_Sensor);
 // Select data register
 Wire.write(0x84);
 // Stop I2C transmission
 Wire.endTransmission();

 // Request 2 bytes of data
 Wire.requestFrom(Addr_Sensor, 2);

 // Read 2 bytes of data
 // luminance lsb, luminance msb
 if (Wire.available() == 2)
 {
 data[0] = Wire.read();
 data[1] = Wire.read();
 }

 // Convert the data
 luminance = data[1] * 256 + data[0];

 // Output data to Serial Monitor
 Serial.print("Ambient Light Luminance :");
 Serial.print(luminance);
 Serial.println(" lux");
 delay(300);

 // 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");

 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();

 // Output data to serial monitor
 Serial.print("House Lights : ON");

 // 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();

 // Output data to serial monitor
 Serial.print("House Lights : OFF");

 // Output data to Web Server
 server.sendContent
 ("<h3 style=text-align:center;font-family:tahoma;> House Lights : OFF");
 }
}

void setup()
{
 // Initialise I2C communication as MASTER
 Wire.begin(2, 14);
 // Initialise serial communication, set baud rate = 115200
 Serial.begin(115200);

 // Connect to WiFi network
 WiFi.begin(ssid, password);

 // Wait for connection
 while (WiFi.status() != WL_CONNECTED)
 {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.print("Connected to ");
 Serial.println(ssid);

 // Get the IP address of ESP8266
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());

 // Start the server
 server.on("/", handleroot);
 server.begin();
 Serial.println("HTTP server started");
}

void loop()
{
 server.handleClient();
}

Step 4: Working of the Code

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.

Step 5: 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 orHeater. There are many possibilities by which you can upgrade the project and make your simple home into – a Smart Home.

Step 6: Resources for Going Further

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

Leave a Reply