Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- index.html +188 -16
- server.js +35 -5
index.html
CHANGED
|
@@ -44,6 +44,22 @@
|
|
| 44 |
margin-bottom: 30px;
|
| 45 |
font-weight: bold;
|
| 46 |
transition: all 0.3s ease;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
.status.connected {
|
|
@@ -51,11 +67,29 @@
|
|
| 51 |
color: #155724;
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
.status.disconnected {
|
| 55 |
background: #f8d7da;
|
| 56 |
color: #721c24;
|
| 57 |
}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
.led-grid {
|
| 60 |
display: grid;
|
| 61 |
grid-template-columns: repeat(2, 1fr);
|
|
@@ -83,6 +117,11 @@
|
|
| 83 |
border-color: #fff;
|
| 84 |
}
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
.led-icon {
|
| 87 |
font-size: 48px;
|
| 88 |
margin-bottom: 10px;
|
|
@@ -113,6 +152,55 @@
|
|
| 113 |
color: rgba(255, 255, 255, 0.9);
|
| 114 |
}
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
.all-off-btn {
|
| 117 |
width: 100%;
|
| 118 |
padding: 15px;
|
|
@@ -141,8 +229,17 @@
|
|
| 141 |
<div class="container">
|
| 142 |
<h1>🔌 IoT LED Control</h1>
|
| 143 |
|
| 144 |
-
<div class="status
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
</div>
|
| 147 |
|
| 148 |
<div class="led-grid">
|
|
@@ -181,27 +278,80 @@
|
|
| 181 |
led1: false,
|
| 182 |
led2: false,
|
| 183 |
led3: false,
|
| 184 |
-
led4: false
|
|
|
|
| 185 |
};
|
| 186 |
|
| 187 |
-
// Fetch initial LED states
|
| 188 |
async function fetchLedStates() {
|
| 189 |
try {
|
| 190 |
const response = await fetch('/api/leds');
|
| 191 |
const data = await response.json();
|
| 192 |
ledStates = data;
|
| 193 |
updateUI();
|
| 194 |
-
|
| 195 |
-
document.getElementById('status').className = 'status connected';
|
| 196 |
} catch (error) {
|
| 197 |
console.error('Error fetching LED states:', error);
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
// Toggle individual LED
|
| 204 |
async function toggleLED(ledName) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
const newState = !ledStates[ledName];
|
| 206 |
|
| 207 |
try {
|
|
@@ -242,16 +392,35 @@
|
|
| 242 |
|
| 243 |
// Update UI based on LED states
|
| 244 |
function updateUI() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
Object.keys(ledStates).forEach(ledName => {
|
|
|
|
|
|
|
| 246 |
const card = document.querySelector(`[data-led="${ledName}"]`);
|
| 247 |
const statusText = card.querySelector('.led-status');
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
} else {
|
| 253 |
-
card.classList.remove('
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
}
|
| 256 |
});
|
| 257 |
}
|
|
@@ -259,8 +428,11 @@
|
|
| 259 |
// Initialize
|
| 260 |
fetchLedStates();
|
| 261 |
|
| 262 |
-
//
|
| 263 |
-
setInterval(
|
|
|
|
|
|
|
|
|
|
| 264 |
</script>
|
| 265 |
</body>
|
| 266 |
-
</html>
|
|
|
|
| 44 |
margin-bottom: 30px;
|
| 45 |
font-weight: bold;
|
| 46 |
transition: all 0.3s ease;
|
| 47 |
+
display: flex;
|
| 48 |
+
align-items: center;
|
| 49 |
+
justify-content: center;
|
| 50 |
+
gap: 10px;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
.status-indicator {
|
| 54 |
+
width: 12px;
|
| 55 |
+
height: 12px;
|
| 56 |
+
border-radius: 50%;
|
| 57 |
+
animation: pulse 2s infinite;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
@keyframes pulse {
|
| 61 |
+
0%, 100% { opacity: 1; }
|
| 62 |
+
50% { opacity: 0.5; }
|
| 63 |
}
|
| 64 |
|
| 65 |
.status.connected {
|
|
|
|
| 67 |
color: #155724;
|
| 68 |
}
|
| 69 |
|
| 70 |
+
.status.connected .status-indicator {
|
| 71 |
+
background: #28a745;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
.status.disconnected {
|
| 75 |
background: #f8d7da;
|
| 76 |
color: #721c24;
|
| 77 |
}
|
| 78 |
|
| 79 |
+
.status.disconnected .status-indicator {
|
| 80 |
+
background: #dc3545;
|
| 81 |
+
animation: none;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.status.connecting {
|
| 85 |
+
background: #fff3cd;
|
| 86 |
+
color: #856404;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.status.connecting .status-indicator {
|
| 90 |
+
background: #ffc107;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
.led-grid {
|
| 94 |
display: grid;
|
| 95 |
grid-template-columns: repeat(2, 1fr);
|
|
|
|
| 117 |
border-color: #fff;
|
| 118 |
}
|
| 119 |
|
| 120 |
+
.led-card.disabled {
|
| 121 |
+
opacity: 0.5;
|
| 122 |
+
cursor: not-allowed;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
.led-icon {
|
| 126 |
font-size: 48px;
|
| 127 |
margin-bottom: 10px;
|
|
|
|
| 152 |
color: rgba(255, 255, 255, 0.9);
|
| 153 |
}
|
| 154 |
|
| 155 |
+
.chase-section {
|
| 156 |
+
margin-bottom: 20px;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.chase-card {
|
| 160 |
+
background: #fff3cd;
|
| 161 |
+
border-radius: 15px;
|
| 162 |
+
padding: 25px;
|
| 163 |
+
text-align: center;
|
| 164 |
+
cursor: pointer;
|
| 165 |
+
border: 3px solid transparent;
|
| 166 |
+
transition: all 0.3s ease;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
.chase-card:hover {
|
| 170 |
+
transform: translateY(-5px);
|
| 171 |
+
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
.chase-card.active {
|
| 175 |
+
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
|
| 176 |
+
border-color: #fff;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.chase-icon {
|
| 180 |
+
font-size: 48px;
|
| 181 |
+
margin-bottom: 10px;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
.chase-name {
|
| 185 |
+
font-size: 20px;
|
| 186 |
+
font-weight: bold;
|
| 187 |
+
margin-bottom: 5px;
|
| 188 |
+
color: #856404;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
.chase-card.active .chase-name {
|
| 192 |
+
color: white;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
.chase-description {
|
| 196 |
+
font-size: 12px;
|
| 197 |
+
color: #856404;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
.chase-card.active .chase-description {
|
| 201 |
+
color: rgba(255, 255, 255, 0.9);
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
.all-off-btn {
|
| 205 |
width: 100%;
|
| 206 |
padding: 15px;
|
|
|
|
| 229 |
<div class="container">
|
| 230 |
<h1>🔌 IoT LED Control</h1>
|
| 231 |
|
| 232 |
+
<div class="status connecting" id="status">
|
| 233 |
+
<div class="status-indicator"></div>
|
| 234 |
+
<span id="status-text">Connecting...</span>
|
| 235 |
+
</div>
|
| 236 |
+
|
| 237 |
+
<div class="chase-section">
|
| 238 |
+
<div class="chase-card" id="chase-card" onclick="toggleChase()">
|
| 239 |
+
<div class="chase-icon">🌀</div>
|
| 240 |
+
<div class="chase-name">Chase Mode</div>
|
| 241 |
+
<div class="chase-description">LED1 → LED2 → LED3 (500ms)</div>
|
| 242 |
+
</div>
|
| 243 |
</div>
|
| 244 |
|
| 245 |
<div class="led-grid">
|
|
|
|
| 278 |
led1: false,
|
| 279 |
led2: false,
|
| 280 |
led3: false,
|
| 281 |
+
led4: false,
|
| 282 |
+
chase: false
|
| 283 |
};
|
| 284 |
|
| 285 |
+
// Fetch initial LED states and connection status
|
| 286 |
async function fetchLedStates() {
|
| 287 |
try {
|
| 288 |
const response = await fetch('/api/leds');
|
| 289 |
const data = await response.json();
|
| 290 |
ledStates = data;
|
| 291 |
updateUI();
|
| 292 |
+
checkConnectionStatus();
|
|
|
|
| 293 |
} catch (error) {
|
| 294 |
console.error('Error fetching LED states:', error);
|
| 295 |
+
updateConnectionStatus(false);
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// Check ESP8266 connection status
|
| 300 |
+
async function checkConnectionStatus() {
|
| 301 |
+
try {
|
| 302 |
+
const response = await fetch('/api/status');
|
| 303 |
+
const data = await response.json();
|
| 304 |
+
updateConnectionStatus(data.connected);
|
| 305 |
+
} catch (error) {
|
| 306 |
+
console.error('Error checking connection status:', error);
|
| 307 |
+
updateConnectionStatus(false);
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
// Update connection status display
|
| 312 |
+
function updateConnectionStatus(connected) {
|
| 313 |
+
const statusDiv = document.getElementById('status');
|
| 314 |
+
const statusText = document.getElementById('status-text');
|
| 315 |
+
|
| 316 |
+
if (connected) {
|
| 317 |
+
statusDiv.className = 'status connected';
|
| 318 |
+
statusText.textContent = 'ESP8266 Connected';
|
| 319 |
+
} else {
|
| 320 |
+
statusDiv.className = 'status disconnected';
|
| 321 |
+
statusText.textContent = 'ESP8266 Disconnected';
|
| 322 |
+
}
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
// Toggle chase mode
|
| 326 |
+
async function toggleChase() {
|
| 327 |
+
const newState = !ledStates.chase;
|
| 328 |
+
|
| 329 |
+
try {
|
| 330 |
+
const response = await fetch('/api/led/chase', {
|
| 331 |
+
method: 'POST',
|
| 332 |
+
headers: {
|
| 333 |
+
'Content-Type': 'application/json'
|
| 334 |
+
},
|
| 335 |
+
body: JSON.stringify({ state: newState })
|
| 336 |
+
});
|
| 337 |
+
|
| 338 |
+
const data = await response.json();
|
| 339 |
+
if (data.success) {
|
| 340 |
+
ledStates = data.ledStates;
|
| 341 |
+
updateUI();
|
| 342 |
+
}
|
| 343 |
+
} catch (error) {
|
| 344 |
+
console.error('Error toggling chase:', error);
|
| 345 |
}
|
| 346 |
}
|
| 347 |
|
| 348 |
// Toggle individual LED
|
| 349 |
async function toggleLED(ledName) {
|
| 350 |
+
// Prevent toggling LED1-3 when chase is active
|
| 351 |
+
if (ledStates.chase && (ledName === 'led1' || ledName === 'led2' || ledName === 'led3')) {
|
| 352 |
+
return;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
const newState = !ledStates[ledName];
|
| 356 |
|
| 357 |
try {
|
|
|
|
| 392 |
|
| 393 |
// Update UI based on LED states
|
| 394 |
function updateUI() {
|
| 395 |
+
// Update chase card
|
| 396 |
+
const chaseCard = document.getElementById('chase-card');
|
| 397 |
+
if (ledStates.chase) {
|
| 398 |
+
chaseCard.classList.add('active');
|
| 399 |
+
} else {
|
| 400 |
+
chaseCard.classList.remove('active');
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
// Update LED cards
|
| 404 |
Object.keys(ledStates).forEach(ledName => {
|
| 405 |
+
if (ledName === 'chase') return;
|
| 406 |
+
|
| 407 |
const card = document.querySelector(`[data-led="${ledName}"]`);
|
| 408 |
const statusText = card.querySelector('.led-status');
|
| 409 |
|
| 410 |
+
// Disable LED1-3 cards when chase is active
|
| 411 |
+
if (ledStates.chase && (ledName === 'led1' || ledName === 'led2' || ledName === 'led3')) {
|
| 412 |
+
card.classList.add('disabled');
|
| 413 |
+
statusText.textContent = 'CHASE MODE';
|
| 414 |
} else {
|
| 415 |
+
card.classList.remove('disabled');
|
| 416 |
+
|
| 417 |
+
if (ledStates[ledName]) {
|
| 418 |
+
card.classList.add('active');
|
| 419 |
+
statusText.textContent = 'ON';
|
| 420 |
+
} else {
|
| 421 |
+
card.classList.remove('active');
|
| 422 |
+
statusText.textContent = 'OFF';
|
| 423 |
+
}
|
| 424 |
}
|
| 425 |
});
|
| 426 |
}
|
|
|
|
| 428 |
// Initialize
|
| 429 |
fetchLedStates();
|
| 430 |
|
| 431 |
+
// Check connection status every 3 seconds
|
| 432 |
+
setInterval(checkConnectionStatus, 3000);
|
| 433 |
+
|
| 434 |
+
// Refresh states every 5 seconds as backup
|
| 435 |
+
setInterval(fetchLedStates, 5000);
|
| 436 |
</script>
|
| 437 |
</body>
|
| 438 |
+
</html>
|
server.js
CHANGED
|
@@ -9,7 +9,8 @@ let ledStates = {
|
|
| 9 |
led1: false,
|
| 10 |
led2: false,
|
| 11 |
led3: false,
|
| 12 |
-
led4: false
|
|
|
|
| 13 |
};
|
| 14 |
|
| 15 |
// Store SSE clients (ESP8266 devices)
|
|
@@ -27,7 +28,7 @@ app.get("/events", (req, res) => {
|
|
| 27 |
|
| 28 |
// Send current state immediately
|
| 29 |
Object.keys(ledStates).forEach(led => {
|
| 30 |
-
res.write(`
|
| 31 |
});
|
| 32 |
|
| 33 |
// Add client to list
|
|
@@ -52,6 +53,14 @@ app.get("/api/leds", (req, res) => {
|
|
| 52 |
res.json(ledStates);
|
| 53 |
});
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
// Toggle individual LED (for frontend)
|
| 56 |
app.post("/api/led/:ledName", (req, res) => {
|
| 57 |
const { ledName } = req.params;
|
|
@@ -63,8 +72,15 @@ app.post("/api/led/:ledName", (req, res) => {
|
|
| 63 |
|
| 64 |
ledStates[ledName] = state;
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
// Broadcast to all ESP8266 devices via SSE
|
| 67 |
-
const message = `
|
| 68 |
sseClients.forEach(client => {
|
| 69 |
try {
|
| 70 |
client.write(message);
|
|
@@ -73,6 +89,20 @@ app.post("/api/led/:ledName", (req, res) => {
|
|
| 73 |
}
|
| 74 |
});
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
console.log(`${ledName} turned ${state ? "ON" : "OFF"}`);
|
| 77 |
res.json({ success: true, ledStates });
|
| 78 |
});
|
|
@@ -81,7 +111,7 @@ app.post("/api/led/:ledName", (req, res) => {
|
|
| 81 |
app.post("/api/all-off", (req, res) => {
|
| 82 |
Object.keys(ledStates).forEach(led => {
|
| 83 |
ledStates[led] = false;
|
| 84 |
-
const message = `
|
| 85 |
sseClients.forEach(client => {
|
| 86 |
try {
|
| 87 |
client.write(message);
|
|
@@ -101,4 +131,4 @@ server.listen(PORT, HOST, () => {
|
|
| 101 |
console.log(`Server running at http://${HOST}:${PORT}`);
|
| 102 |
console.log("SSE endpoint: /events");
|
| 103 |
console.log("API endpoint: /api/leds");
|
| 104 |
-
});
|
|
|
|
| 9 |
led1: false,
|
| 10 |
led2: false,
|
| 11 |
led3: false,
|
| 12 |
+
led4: false,
|
| 13 |
+
chase: false
|
| 14 |
};
|
| 15 |
|
| 16 |
// Store SSE clients (ESP8266 devices)
|
|
|
|
| 28 |
|
| 29 |
// Send current state immediately
|
| 30 |
Object.keys(ledStates).forEach(led => {
|
| 31 |
+
res.write(` ${led}:${ledStates[led] ? 1 : 0}\n\n`);
|
| 32 |
});
|
| 33 |
|
| 34 |
// Add client to list
|
|
|
|
| 53 |
res.json(ledStates);
|
| 54 |
});
|
| 55 |
|
| 56 |
+
// Get connection status
|
| 57 |
+
app.get("/api/status", (req, res) => {
|
| 58 |
+
res.json({
|
| 59 |
+
connected: sseClients.length > 0,
|
| 60 |
+
clientCount: sseClients.length
|
| 61 |
+
});
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
// Toggle individual LED (for frontend)
|
| 65 |
app.post("/api/led/:ledName", (req, res) => {
|
| 66 |
const { ledName } = req.params;
|
|
|
|
| 72 |
|
| 73 |
ledStates[ledName] = state;
|
| 74 |
|
| 75 |
+
// If turning on chase, turn off individual LEDs 1-3
|
| 76 |
+
if (ledName === "chase" && state) {
|
| 77 |
+
ledStates.led1 = false;
|
| 78 |
+
ledStates.led2 = false;
|
| 79 |
+
ledStates.led3 = false;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
// Broadcast to all ESP8266 devices via SSE
|
| 83 |
+
const message = ` ${ledName}:${state ? 1 : 0}\n\n`;
|
| 84 |
sseClients.forEach(client => {
|
| 85 |
try {
|
| 86 |
client.write(message);
|
|
|
|
| 89 |
}
|
| 90 |
});
|
| 91 |
|
| 92 |
+
// If chase was turned on, also send LED1-3 off commands
|
| 93 |
+
if (ledName === "chase" && state) {
|
| 94 |
+
["led1", "led2", "led3"].forEach(led => {
|
| 95 |
+
const msg = ` ${led}:0\n\n`;
|
| 96 |
+
sseClients.forEach(client => {
|
| 97 |
+
try {
|
| 98 |
+
client.write(msg);
|
| 99 |
+
} catch (err) {
|
| 100 |
+
console.error("Error sending to client:", err.message);
|
| 101 |
+
}
|
| 102 |
+
});
|
| 103 |
+
});
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
console.log(`${ledName} turned ${state ? "ON" : "OFF"}`);
|
| 107 |
res.json({ success: true, ledStates });
|
| 108 |
});
|
|
|
|
| 111 |
app.post("/api/all-off", (req, res) => {
|
| 112 |
Object.keys(ledStates).forEach(led => {
|
| 113 |
ledStates[led] = false;
|
| 114 |
+
const message = ` ${led}:0\n\n`;
|
| 115 |
sseClients.forEach(client => {
|
| 116 |
try {
|
| 117 |
client.write(message);
|
|
|
|
| 131 |
console.log(`Server running at http://${HOST}:${PORT}`);
|
| 132 |
console.log("SSE endpoint: /events");
|
| 133 |
console.log("API endpoint: /api/leds");
|
| 134 |
+
});
|