Want to start Innovating with Arduino, Raspberry Pi and Internet of things (IoT) ? Click here to learn about our July 2017 training

ESP8266 WiFi Controlled Home Automation

Posted by Momen 29/06/2017 0 Comment(s)

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

 

Our Arduino training series:

1. Start with Arduino - Zeroohm Arduino Training

2. Start with Arduino - Arduino with Motors and sensors

3. Start with Arduino - Wireless communication

4. How to setup Arduino with room temperature monitoring using LM36 sensor

5. Introduction to IoT using Arduino

5. Introduction to IoT using Arduino

6. IoT: Humidity and Temperature Sensor

7. ADXL345 Module Communication with Arduino UNO Board

8. ESP8266 WiFi Controlled Home Automation System

 

In this tutorial, I will try to explain how to implement a WiFi controlled home automation system using ESP8266 WiFi module and the famous Arduino UNO board. Let’s start by knowing what components we need:

Hardware Components:

Let’s head to the hardware design!

Hardware Design

If you are just starting to get WiFi and IoT (Internet of Things) stuff then ESP8266 is the best way to begin since it already come pre-programmed with an AT command set firmware, meaning you can just hook it up with any Arduino device which in this case is UNO and get as much as the same WiFi ability as a WiFi shield would offer. Another great thing about this module is that it is extremely cost effective with lots of applications! Now let’s have a look at the hardware overview of the entire circuit shown in the fritzing diagram below. Connect them as shown.

wifi-home-automation-system-circuit

NOTE: One of the pushbuttons used here is for resetting the WiFi module in case it gets hanged or stop working and the other one is connected to GPIO pin in the module which is Normal Mode for HIGH Programming Mode for LOW.

The WiFi module even though cost-efficient is known for being rude to the breadboard. There are many tutorials in the net showing how to place the module on the breadboard without much fuss. Go check it out!

We are finally done with the hardware, now onto the software design!

 

Software Design

Step 1- Have the latest version of Arduino IDE software. (Arduino 1.6.8)

Step 2- For installing the WiFi module, we need to first add it to the ‘Additional Boards Manager’ URL’s field. This is the link: http://arduino.esp8266.com/stable/package_esp8266com_index.json

esp8266-installation-addiotional-boards-manager

 

 

 

 

 

 

 

 

 

 

 

 

Step 3- Now go to ‘Boards Manager’ in the tools section and install ESP8266 WiFi module.

boards-manager-esp8266

 

 

 

 

 

 

 

 

 

 

 

 

Step 4- Select the WiFi module as your board.

esp8266-board-selection

Step 5- Now we will conduct a small experiment called ‘Blink Sketch’ which is the same used for UNO and LED to check if the circuit is working or not. Write the code below:

int ledPin=2;

void setup() {

pinMode(ledPin, OUTPUT);

}

void loop() {

digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

We can now focus on the main part of the experiment which is to control the LED through a webpage. ‘Web Controlled Home Automation’ would probably be a good name for this.

Step 6- Keeping the circuit same, write the code below:

<p>#include <ESP8266WiFi.h><br></p><p>const char* ssid = "viper";

const char* password = "chips";</p><p>int ledPin = 2; // GPIO2

WiFiServer server(80);</p><p>void setup() {

Serial.begin(115200);

delay(10);</p><p>pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);</p><p>// Connect to WiFi network

Serial.println();

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);</p><p>WiFi.begin(ssid, password);</p><p>while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");</p><p>// Start the server

server.begin();

Serial.println("Server started");</p><p>// Print the IP address

Serial.print("Use this URL to connect: ");

Serial.print("http://");

Serial.print(WiFi.localIP());

Serial.println("/");</p><p>}</p><p>void loop() {

// Check if a client has connected

WiFiClient client = server.available();

if (!client) {

return;

}</p><p>// Wait until the client sends some data

Serial.println("new client");

while(!client.available()){

delay(1);

}</p><p>// Read the first line of the request

String request = client.readStringUntil('\r');

Serial.println(request);

client.flush();</p><p>// Match the request</p><p>int value = LOW;

if (request.indexOf("/LED=ON") != -1) {

digitalWrite(ledPin, HIGH);

value = HIGH;

}

if (request.indexOf("/LED=OFF") != -1) {

digitalWrite(ledPin, LOW);

value = LOW;

}</p><p>// Set ledPin according to the request

//digitalWrite(ledPin, value);</p><p>// Return the response

client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html");

client.println(""); // do not forget this one

client.println("");

client.println("

");</p><p>client.print("Led pin is now: ");</p><p>if(value == HIGH) {

client.print("On");

} else {

client.print("Off");

}

client.println("<br><br>");

client.println("Click <a>here</a> turn the LED on pin 2 ON<br>");

client.println("Click <a>here</a> turn the LED on pin 2 OFF<br>");

client.println("</p><p>");</p><p>delay(1);

Serial.println("Client disonnected");

Serial.println("");</p><p>}</p>

NOTE: Be careful when copy-pasting the code since the format of text used here is slightly different than that in the software code.  Remember to change the SSID and password according to your network.

Step 7- Check the serial monitor to see the WiFi address of your module.

Step 8- Now copy-paste the IP address in the web browser search bar and click ‘Enter’. You will be taken to a page shown below.

html-led-turn-on-and-off

You can now check if the circuit is working or not by making the LED On and Off.

Signing Off Note: This project can be further improved by introducing IoT technology to it for example you can make a home control box to control home appliances. This can be done by linking the WiFi module to a mobile application called ‘Blynk’ which is compatible with both MAC and Windows. So, what will happen is, the circuit used here will stay the same and the ‘Blynk’ app will control it. Quite cool, isn’t it?! Unfortunately, this topic is too big to cover in this tutorial.

Since I have nothing more to add, I’ll say my goodbyes and I hope this tutorial was useful for you and you enjoyed it!

Leave a Comment