Rudam Dewmith
Published

Smart Lighting System

Smart lamp system—control 10 bulbs wirelessly via Wi-Fi with a sleek dashboard for modern spiritual living.

IntermediateFull instructions provided1 hour197
Smart Lighting System

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
16 Channel Relay Module
×1
Jumper wires (generic)
Jumper wires (generic)
×1
AC/DC Power Supply, 12 V
AC/DC Power Supply, 12 V
×1
LED Light Bulb, Frosted GLS
LED Light Bulb, Frosted GLS
×1

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio Code

Story

Read more

Schematics

Wiring

Code

Arduino Code for ESP 32

C/C++
//Smart lighting system developed by rudam dewmith

#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>

//Smart lighting system developed by rudam dewmith

//wifi credentials
const char* ssid = "your wifi ssid";
const char* password = "your wifi password";

//wiring identification
#define NUM_RELAYS 10
const int relayPins[NUM_RELAYS] = {4, 5, 13, 14, 16, 17, 18, 19, 21, 22}; // Safe GPIOs
bool relayStates[NUM_RELAYS] = {false};

WebServer server(80);

void handleToggle() {
  if (!server.hasArg("relay") || !server.hasArg("state")) {
    server.send(400, "text/plain", "Missing parameters");
    return;
  }

  int relay = server.arg("relay").toInt();
  if (relay < 0 || relay >= NUM_RELAYS) {
    server.send(400, "text/plain", "Invalid relay index");
    return;
  }

  relayStates[relay] = server.arg("state").toInt() == 1;
  if(relayStates[relay]){
    pinMode(relayPins[relay], OUTPUT);   
    digitalWrite(relayPins[relay], LOW);    
  }else{
    pinMode(relayPins[relay], INPUT);   
  }
  
  Serial.printf("Relay %d (GPIO %d) set to %s\n", 
               relay, relayPins[relay], 
               relayStates[relay] ? "ON" : "OFF");
  
  server.send(200, "text/plain", "OK");
}

void handleStatus() {
  StaticJsonDocument<512> doc;
  for (int i = 0; i < NUM_RELAYS; i++) {
    doc[String("relay_") + i] = relayStates[i] ? 1 : 0;
  }
  String response;
  serializeJson(doc, response);
  server.send(200, "application/json", response);
}

void testAllRelays() {
  Serial.println("\nTesting all relays...");
  for (int i = 0; i < NUM_RELAYS; i++) {
    pinMode(relayPins[i],  OUTPUT);
    digitalWrite(relayPins[i], LOW);
    Serial.printf("Relay %d ON ", i);
    delay(300);
    pinMode(relayPins[i],  INPUT);
    Serial.println("OFF");
    delay(200);
  }
}

void setup() {
  Serial.begin(115200);
  delay(2000); // Important delay for serial monitor
  
  // Initialize all relays OFF
  for (int i = 0; i < NUM_RELAYS; i++) {
    pinMode(relayPins[i], INPUT);
    delay(50); // Prevent power surge
  }

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  
  Serial.print("Connected! IP: ");
  Serial.println(WiFi.localIP());

  testAllRelays(); // Verify each relay works
  
  server.on("/toggle", handleToggle);
  server.on("/status", handleStatus);
  server.begin();
}

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

Visual Studio Code for Web Dashboard

HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>NEXORA 1.0</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      margin: 0;
      font-family: 'Segoe UI', sans-serif;
      background: #0f1117;
      color: #ffffff;
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    header {
      margin: 30px 0 10px 0;
      text-align: center;
    }

    header img {
      width: 250px;
      height: auto;
    }

    .switch-container {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 15px;
      max-width: 500px;
      padding: 15px;
      width: 100%;
    }

    .switch-card {
      background: #1c1f2b;
      border-radius: 12px;
      padding: 12px;
      text-align: center;
      box-shadow: 0 0 8px rgba(0, 255, 255, 0.2);
    }

    .switch-card h3 {
      margin-bottom: 10px;
      font-size: 14px;
      color: #00FFFF;
    }

    .switch {
      position: relative;
      display: inline-block;
      width: 46px;
      height: 26px;
    }

    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }

    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #444;
      transition: 0.4s;
      border-radius: 26px;
    }

    .slider:before {
      position: absolute;
      content: "";
      height: 18px;
      width: 18px;
      left: 4px;
      bottom: 4px;
      background-color: #00FFFF;
      transition: 0.4s;
      border-radius: 50%;
    }
    footer{
      color: #444;
    }

    input:checked + .slider {
      background-color: #00FFFF;
    }

    input:checked + .slider:before {
      transform: translateX(20px);
      background-color: #0f0f0f;
    }
  </style>
</head>
<body>

  <header>
    <h1>Smart Light Controlling System</h1>
    <img src="Logo.png" alt="Logo" />
  </header>

  <div class="switch-container" id="switchPanel">
    <!-- Switches will be added here by JavaScript -->
  </div>

  <script>
    // Custom names for each switch
    const lampNames = [
      "TV Loby Lamp",
      "Living Room Lamp",
      "Washroom 1 Lamp",
      "Master Bedroom Lamp",
      "Garden Light",
      "Kitchen Lamp",
      "Hallway Lamp",
      "Balcony Lamp",
      "Prayer Lamp",
      "Bedroom Lamp"
    ];

    const panel = document.getElementById('switchPanel');

    for (let i = 0; i < 10; i++) {
      const card = document.createElement('div');
      card.className = 'switch-card';
      card.innerHTML = `
        <h3>${lampNames[i]}</h3>
        <label class="switch">
          <input type="checkbox" id="switch${i}" onchange="toggleRelay(${i})">
          <span class="slider"></span>
        </label>
      `;
      panel.appendChild(card);
    }

    function toggleRelay(relayIndex) {
      const checked = document.getElementById(`switch${relayIndex}`).checked;
      fetch(`http://192.168.1.17/toggle?relay=${relayIndex}&state=${checked ? 1 : 0}`); 
    }<!-- replace with actual ip address of ESP32 -->

    // Optional: Sync status every 2s
    setInterval(() => {
      fetch('http://192.168.1.17/status')<!-- replace with actual ip address of ESP32 -->
        .then(res => res.json())
        .then(states => {
          for (let i = 0; i < 10; i++) {
            document.getElementById(`switch${i}`).checked = states[i] === 1;
          }
        });
    }, 2000);
  </script>

</body>
<footer>&copy;Design & Developed By Rudam Dewmith</footer>
</html>

Credits

Rudam Dewmith
1 project • 0 followers
IoT developer crafting smart systems from idea to prototype. Started with Arduino; now building real-world hardware for over 3 years.

Comments