Translate

Arduino IoT Home Automation Using ESP8266

 1. Introduction

Block Diagram

In this project, we will build an IoT-based home automation system using the ESP8266 WiFi module. This system allows you to control electrical appliances like lights and fans using a smartphone from anywhere.

The ESP8266 connects to WiFi and hosts a web server, enabling wireless control without Bluetooth limitations. This project is widely used in smart homes and automation systems.


2. Components Required

  • NodeMCU (ESP8266)
  • Relay Module (1/2/4 Channel)
  • Bulb / Fan (Load)
  • Jumper Wires
  • Breadboard
  • Power Supply

3. Circuit Diagram and Connections

PInout Diagram NodeMCU ESP8266


Pinout Diagram of 2 Channel Relay Module

🔌 Connections:

Relay ModuleNodeMCU
VCC    3.3V
GND    GND
IN1    D1

⚡ Load Connection:

  • Connect Phase line → Relay COM
  • Connect NO → Bulb/Fan
  • Neutral directly to load

4. Circuit Working

  1. ESP8266 connects to WiFi
  2. It creates a web server (IP address)
  3. User opens IP in mobile browser
  4. Buttons ON/OFF control relay
  5. Relay switches appliance

5. Arduino Code

#include <ESP8266WiFi.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(80);

int relayPin = D1;

void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);

WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("\nConnected!");
Serial.println(WiFi.localIP());

server.begin();
}

void loop() {
WiFiClient client = server.available();
if (!client) return;

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

if (request.indexOf("/ON") != -1) {
digitalWrite(relayPin, HIGH);
}
if (request.indexOf("/OFF") != -1) {
digitalWrite(relayPin, LOW);
}

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");

client.println("<html><body>");
client.println("<h1>Home Automation</h1>");
client.println("<a href=\"/ON\"><button>ON</button></a>");
client.println("<a href=\"/OFF\"><button>OFF</button></a>");
client.println("</body></html>");

delay(1);
}

6. Code Explanation

  • 🔹 Step 1: Include Required Library

    #include <ESP8266WiFi.h>

    👉 This library is essential for ESP8266.

    What it does:

    • Enables WiFi functionality
    • Provides functions like:
      • WiFi.begin()
      • WiFi.status()
      • WiFi.localIP()

    👉 Without this, ESP8266 cannot connect to WiFi


    🔹 Step 2: Store WiFi Credentials

    const char* ssid = "YOUR_WIFI_NAME";
    const char* password = "YOUR_WIFI_PASSWORD";

    Explanation:

    • const char* → pointer to text (string)
    • ssid → WiFi name
    • password → WiFi password

    👉 These values are used to authenticate connection


    🔹 Step 3: Create Web Server Object

    WiFiServer server(80);

    Explanation:

    • WiFiServer → creates a server object
    • 80 → HTTP port (default web port)

    👉 This means:

    • ESP8266 behaves like a mini website server

    🔹 Step 4: Define Output Pin

    int relayPin = D1;

    Explanation:

    • D1 is GPIO5 in NodeMCU
    • Connected to relay input

    👉 This pin controls:

    • HIGH → Relay ON
    • LOW → Relay OFF

    ⚙️ SETUP FUNCTION (Runs Once)


    🔹 Step 5: Start Setup

    void setup() {

    👉 This runs only once during power ON/reset


    🔹 Step 6: Initialize Serial Communication

    Serial.begin(115200);

    Explanation:

    • Starts communication between:
      • ESP8266 ↔ Computer
    • Baud rate = 115200 (fast)

    👉 Used for:

    • Debugging
    • Showing IP address

    🔹 Step 7: Configure Relay Pin

    pinMode(relayPin, OUTPUT);
    digitalWrite(relayPin, LOW);

    Explanation:

    • pinMode() → sets pin as OUTPUT
    • digitalWrite(LOW) → relay OFF initially

    👉 Prevents unwanted switching at startup


    🔹 Step 8: Start WiFi Connection

    WiFi.begin(ssid, password);
    Serial.print("Connecting to WiFi");

    Explanation:

    • Starts connection process
    • Uses SSID + password

    👉 At this stage:

    • ESP8266 tries to connect to router

    🔹 Step 9: Wait Until Connected

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }

    Explanation:

    • WiFi.status() returns connection state
    • WL_CONNECTED means success

    👉 Loop runs until connected

    Output Example:

    Connecting to WiFi....

    👉 Each dot = 0.5 second delay


    🔹 Step 10: Print Connection Info

    Serial.println("\nConnected!");
    Serial.println(WiFi.localIP());

    Explanation:

    • Prints success message
    • Displays IP address

    👉 Example:

    Connected!
    192.168.1.5

    ⚠️ This IP is VERY IMPORTANT
    👉 Used to control device


    🔹 Step 11: Start Server

    server.begin();

    Explanation:

    • Starts listening for incoming clients

    👉 Now ESP8266 is:
    ✔ Connected to WiFi
    ✔ Acting as web server


    🔁 LOOP FUNCTION (Runs Continuously)


    🔹 Step 12: Start Loop

    void loop() {

    👉 Runs repeatedly (infinite loop)


    🔹 Step 13: Check for Client Connection

    WiFiClient client = server.available();
    if (!client) return;

    Explanation:

    • server.available() checks if:
      • Any device (phone) is connected

    👉 If no client:

    • Exit loop immediately

    🔹 Step 14: Read Client Request

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

    Explanation:

    • Reads HTTP request sent by browser

    👉 Example request:

    GET /ON HTTP/1.1
    • \r = end of line
    • flush() clears buffer

    🔹 Step 15: Check ON Command

    if (request.indexOf("/ON") != -1) {
    digitalWrite(relayPin, HIGH);
    }

    Explanation:

    • indexOf() searches text
    • If /ON found → returns position
    • If not → returns -1

    👉 So:
    ✔ If found → Relay ON


    🔹 Step 16: Check OFF Command

    if (request.indexOf("/OFF") != -1) {
    digitalWrite(relayPin, LOW);
    }

    👉 Same logic:
    /OFF → Relay OFF


    🔹 Step 17: Send HTTP Response Header

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");

    Explanation:

    • Tells browser:
      • Request successful
      • Content is HTML

    🔹 Step 18: Send Web Page Content

    client.println("<html><body>");
    client.println("<h1>Home Automation</h1>");
    client.println("<a href=\"/ON\"><button>ON</button></a>");
    client.println("<a href=\"/OFF\"><button>OFF</button></a>");
    client.println("</body></html>");

    Explanation:

    👉 Creates simple webpage:

    • Heading
    • ON button
    • OFF button

    👉 When clicked:

    • /ON or /OFF sent to ESP8266

    🔹 Step 19: Small Delay

    delay(1);

    👉 Ensures:

    • Smooth communication
    • Prevents overload

    🧠 FULL SYSTEM FLOW (VERY CLEAR)

    1. ESP8266 connects to WiFi
    2. Gets IP address
    3. User opens IP in browser
    4. Webpage loads
    5. User clicks button
    6. Request sent (/ON or /OFF)
    7. ESP8266 reads request
    8. Relay switches appliance

    ⚠️ IMPORTANT PRACTICAL POINT

    👉 Relay logic may differ:

    Type
    Behavior
    Active HIGH
    HIGH = ON
    Active LOW
    LOW = ON

    👉 If reversed:

    HIGH ↔ LOW

7. Output




  • Open IP address in browser
  • Click ON/OFF buttons
  • Appliance turns ON/OFF

8. Applications

  • Smart home systems
  • Remote appliance control
  • Energy saving systems
  • Industrial automation

9. Advantages

  • Wireless control
  • Low cost
  • Easy to implement
  • Scalable system

10. Conclusion

This project demonstrates how IoT technology can be used to control home appliances remotely using WiFi. It is a practical and powerful system widely used in modern smart homes.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.