Wi-Fi se Ghar ke Devices Kaise Control Karein | 1-Channel Home Automation Banayein

How to Set Up Arduino IDE, Configure NodeMCU, and Create a 1-Channel Home Automation System

Hello everyone, I'm Ritik, and in today's post, we'll learn how to create a simple 1-channel home automation system using NodeMCU. This project will let you control a device (like a light or fan) with your smartphone via Wi-Fi. We'll walk through the steps to set up Arduino IDE, configure NodeMCU, and write the code for the home automation system. So, let's get started!

If you prefer watching the full process, here's the video link:

Full Process Video

Step 1: Install Arduino IDE

Follow these steps to install Arduino IDE:

  • Go to the official Arduino website: Arduino Software.
  • Download the version compatible with your operating system (Windows/Mac/Linux).
  • Run the installer and follow the on-screen instructions to complete the installation.

Once installed, open Arduino IDE and proceed to the next step.

Step 2: Install NodeMCU Driver

Before uploading code, you need to install the NodeMCU driver:

  • Download the CP2102 driver from: Silicon Labs.
  • Install the driver on your computer by running the downloaded setup file.
  • Once installed, connect your NodeMCU to your computer via USB. It should now be recognized by your system.

Step 3: Configure Arduino IDE for ESP8266

Follow these steps to configure Arduino IDE:

  1. Open Arduino IDE and go to File > Preferences.
  2. In the "Additional Board Manager URLs" field, paste this URL:
  3. http://arduino.esp8266.com/stable/package_esp8266com_index.json
  4. Click "OK" and then go to Tools > Board > Board Manager.
  5. Search for ESP8266 and install the "ESP8266 by ESP8266 Community" package.
  6. Now, select the correct board and port:
    • Go to Tools > Board and select NodeMCU 1.0 (ESP-12E Module).
    • Go to Tools > Port and select the COM port where your NodeMCU is connected.

Step 4: Upload Code to NodeMCU

Copy the code below and paste it into the Arduino IDE. Make sure to replace YourWiFiSSID and YourWiFiPassword with your Wi-Fi details.

#include <ESP8266WiFi.h>
#include <EEPROM.h>

// Wi-Fi credentials
const char* ssid = "vinod";             // Hotspot Name
const char* password = "123456789";     // Hotspot Password

// Static IP configuration
IPAddress staticIP(192, 168, 1, 100);   // Static IP Address
IPAddress gateway(192, 168, 1, 1);      // Gateway (usually your router's IP)
IPAddress subnet(255, 255, 255, 0);     // Subnet Mask

WiFiServer server(80);                  // HTTP server on port 80

#define RELAY_PIN 2 // GPIO2 (D4)

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);

  pinMode(RELAY_PIN, OUTPUT);

  // Restore previous relay state
  int savedState = EEPROM.read(0);
  digitalWrite(RELAY_PIN, savedState == 1 ? LOW : HIGH);

  // Configure Wi-Fi with static IP
  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.println("Connecting to Wi-Fi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("\nConnected to Wi-Fi!");
  Serial.print("Static IP Address: ");
  Serial.println(WiFi.localIP());

  server.begin();
}

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

  while (client.connected()) {
    if (client.available()) {
      String request = client.readStringUntil('\r');
      Serial.println(request);
      client.flush();

      if (request.indexOf("/ON") != -1) {
        digitalWrite(RELAY_PIN, LOW);
        EEPROM.write(0, 1);
        EEPROM.commit();
      } else if (request.indexOf("/OFF") != -1) {
        digitalWrite(RELAY_PIN, HIGH);
        EEPROM.write(0, 0);
        EEPROM.commit();
      }

      // Send the HTML response
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      client.println("<!DOCTYPE html>");
      client.println("<html lang='en'>");
      client.println("<head>");
      client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
      client.println("<title>Home Automation - RD Kala</title>");
      client.println("<style>");
      client.println("body { font-family: Arial, sans-serif; text-align: center; margin: 0; padding: 0; background-color: #f0f0f5; color: #333; }");
      client.println(".container { margin: 50px auto; padding: 20px; background: white; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); border-radius: 10px; max-width: 600px; }");
      client.println(".header { font-size: 24px; font-weight: bold; color: #4CAF50; margin-bottom: 20px; }");
      client.println(".welcome { font-size: 16px; color: #555; margin-bottom: 20px; }");
      client.println(".button { padding: 15px 30px; font-size: 18px; color: white; border: none; border-radius: 5px; cursor: pointer; margin: 10px; }");
      client.println(".on { background-color: #4CAF50; }");
      client.println(".off { background-color: #f44336; }");
      client.println(".footer { margin-top: 30px; padding: 20px; background-color: #333; color: white; border-radius: 10px; }");
      client.println(".footer a { color: #4CAF50; text-decoration: none; margin: 0 10px; }");
      client.println(".footer a:hover { text-decoration: underline; }");
      client.println("</style>");
      client.println("</head>");
      client.println("<body>");
      client.println("<div class='container'>");
      client.println("<div class='header'>Welcome to RD Kala</div>");
      client.println("<p class='welcome'>Where creativity meets innovation! We constantly launch exciting new projects, and this platform lets you apply, collaborate, and stay connected with ease. Built with passion, this code is a reflection of our commitment to excellence.</p>");
      client.println("<p>Control your devices below:</p>");
      client.println("<button class='button on' onclick=\"location.href='/ON'\">Turn ON</button>");
      client.println("<button class='button off' onclick=\"location.href='/OFF'\">Turn OFF</button>");
      client.println("</div>");
      client.println("<div class='footer'>");
      client.println("<p><strong>Stay Connected with RD Kala!</strong></p>");
      client.println("<p>Website: <a href='https://ritikdevinfo.blogspot.com' target='_blank'>https://ritikdevinfo.blogspot.com</a></p>");
      client.println("<p>Instagram: <a href='https://instagram.com/vinodkyadav23' target='_blank'>@vinodkyadav23</a></p>");
      client.println("<p>Twitter: <a href='https://twitter.com/vinodkyadav23' target='_blank'>@vinodkyadav23</a></p>");
      client.println("<p>YouTube: <a href='https://youtube.com/@Rd_kala' target='_blank'>RD Kala</a></p>");
      client.println("<p>Designed and Developed by <strong>RD Kala</strong>.</p>");
      client.println("</div>");
      client.println("</body>");
      client.println("</html>");
      break;
    }
  }
  delay(10);
}


            

Step 5: Test Your Setup

  • Connect your NodeMCU to your Wi-Fi network using the uploaded code.
  • Check the Serial Monitor in Arduino IDE to find your NodeMCU's IP address.
  • Open the IP address in your browser to access the control panel.
  • Use the "Turn ON" and "Turn OFF" buttons to control your device.

Your setup is now ready. Enjoy your smart home automation system!

Conclusion

Congratulations! You have successfully built and tested your 1-Channel Home Automation system using ESP8266. For any questions or issues, feel free to comment below. 😊

Post a Comment

0 Comments