Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

create web app that show demo dashboard for MQTT smart home devices for two homes. use…

  1. User

    create web app that show demo dashboard for MQTT smart home devices for two homes. use vanila HTML,CSS,JS no frameworks

  2. Assistant

    Certainly! Below is a simple vanilla HTML, CSS, and JS web app that simulates a dashboard for MQTT smart home devices in two homes. This is a demo: it does not connect to a real MQTT broker, but it shows how you might display device status and simulate updates.

    Features:

    • Two homes, each with a few devices (lights, thermostat, door lock, etc.)
    • Device status display
    • Simulated status updates (randomly change device states every few seconds)
    • Simple, clean layout

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>MQTT Smart Home Dashboard Demo</title>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
      <h1>MQTT Smart Home Dashboard (Demo)</h1>
      <div id="dashboard">
        <!-- Homes and devices will be rendered here -->
      </div>
      <script src="app.js"></script>
    </body>
    </html>
    

    styles.css

    body {
      font-family: Arial, sans-serif;
      background: #f4f6fa;
      margin: 0;
      padding: 0;
    }
    
    h1 {
      text-align: center;
      margin: 24px 0 16px 0;
    }
    
    #dashboard {
      display: flex;
      justify-content: center;
      gap: 32px;
      padding: 24px;
    }
    
    .home {
      background: #fff;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.08);
      padding: 24px;
      min-width: 320px;
    }
    
    .home-title {
      font-size: 1.3em;
      margin-bottom: 16px;
      color: #2a3a4a;
    }
    
    .device-list {
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    .device {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 0;
      border-bottom: 1px solid #eee;
    }
    
    .device:last-child {
      border-bottom: none;
    }
    
    .device-name {
      font-weight: 500;
    }
    
    .device-status {
      padding: 4px 12px;
      border-radius: 8px;
      font-size: 0.95em;
      min-width: 60px;
      text-align: center;
    }
    
    .status-on {
      background: #d4f8e8;
      color: #1a7f37;
    }
    
    .status-off {
      background: #ffeaea;
      color: #c0392b;
    }
    
    .status-locked {
      background: #e3e8ff;
      color: #2a3a8c;
    }
    
    .status-unlocked {
      background: #fff6e3;
      color: #b67c00;
    }
    
    .status-temp {
      background: #eaf6ff;
      color: #1b4d72;
    }
    

    app.js

    // Demo data for two homes and their devices
    const homes = [
      {
        name: "Home 1 - Main Residence",
        devices: [
          { id: "h1-light1", name: "Living Room Light", type: "light", status: "off" },
          { id: "h1-thermo", name: "Thermostat", type: "thermostat", status: 22 },
          { id: "h1-door", name: "Front Door Lock", type: "lock", status: "locked" }
        ]
      },
      {
        name: "Home 2 - Lake House",
        devices: [
          { id: "h2-light1", name: "Porch Light", type: "light", status: "on" },
          { id: "h2-thermo", name: "Thermostat", type: "thermostat", status: 19 },
          { id: "h2-door", name: "Back Door Lock", type: "lock", status: "unlocked" }
        ]
      }
    ];
    
    // Helper: get status class for device type/status
    function getStatusClass(device) {
      if (device.type === "light") {
        return device.status === "on" ? "status-on" : "status-off";
      }
      if (device.type === "lock") {
        return device.status === "locked" ? "status-locked" : "status-unlocked";
      }
      if (device.type === "thermostat") {
        return "status-temp";
      }
      return "";
    }
    
    // Helper: get status label for device
    function getStatusLabel(device) {
      if (device.type === "thermostat") {
        return device.status + "°C";
      }
      return device.status.charAt(0).toUpperCase() + device.status.slice(1);
    }
    
    // Render the dashboard
    function renderDashboard() {
      const dashboard = document.getElementById("dashboard");
      dashboard.innerHTML = "";
    
      homes.forEach(home => {
        const homeDiv = document.createElement("div");
        homeDiv.className = "home";
    
        const title = document.createElement("div");
        title.className = "home-title";
        title.textContent = home.name;
        homeDiv.appendChild(title);
    
        const deviceList = document.createElement("ul");
        deviceList.className = "device-list";
    
        home.devices.forEach(device => {
          const deviceItem = document.createElement("li");
          deviceItem.className = "device";
    
          const nameSpan = document.createElement("span");
          nameSpan.className = "device-name";
          nameSpan.textContent = device.name;
    
          const statusSpan = document.createElement("span");
          statusSpan.className = "device-status " + getStatusClass(device);
          statusSpan.textContent = getStatusLabel(device);
    
          deviceItem.appendChild(nameSpan);
          deviceItem.appendChild(statusSpan);
    
          deviceList.appendChild(deviceItem);
        });
    
        homeDiv.appendChild(deviceList);
        dashboard.appendChild(homeDiv);
      });
    }
    
    // Simulate device status updates (as if via MQTT)
    function simulateUpdates() {
      homes.forEach(home => {
        home.devices.forEach(device => {
          if (device.type === "light") {
            // 20% chance to toggle
            if (Math.random() < 0.2) {
              device.status = device.status === "on" ? "off" : "on";
            }
          } else if (device.type === "lock") {
            // 10% chance to toggle
            if (Math.random() < 0.1) {
              device.status = device.status === "locked" ? "unlocked" : "locked";
            }
          } else if (device.type === "thermostat") {
            // 30% chance to change temp by -1, 0, or +1
            if (Math.random() < 0.3) {
              let delta = Math.floor(Math.random() * 3) - 1;
              device.status = Math.max(16, Math.min(28, device.status + delta));
            }
          }
        });
      });
      renderDashboard();
    }
    
    // Initial render
    renderDashboard();
    
    // Simulate updates every 3 seconds
    setInterval(simulateUpdates, 3000);
    

    How to use

    1. Create three files: index.html, styles.css, and app.js in the same folder.
    2. Copy the code above into the respective files.
    3. Open index.html in your browser.

    You will see a dashboard for two homes, with device statuses updating every few seconds, simulating MQTT updates.


    You can expand this by adding controls, more device types, or real MQTT integration using MQTT.js in the future!