veltrixcode commited on
Commit
800d973
·
verified ·
1 Parent(s): fe9b24f

Upload 5 files

Browse files
Files changed (4) hide show
  1. index.html +261 -15
  2. package-lock.json +1 -1
  3. package.json +1 -3
  4. server.js +91 -12
index.html CHANGED
@@ -1,20 +1,266 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
- <title>4 LED Countdown Control</title>
5
- <script src="/socket.io/socket.io.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  </head>
7
  <body>
8
- <h1>4 LED Control Panel</h1>
9
- <button onclick="send('led1')">All but Last ON</button>
10
- <button onclick="send('led2')">Countdown Mode</button>
11
- <button onclick="send('off')">Turn OFF</button>
12
- <p id="status"></p>
13
-
14
- <script>
15
- const socket = io();
16
- socket.on("update", s => document.getElementById("status").innerText = JSON.stringify(s));
17
- function send(mode){ socket.emit("toggle", {mode}); }
18
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </body>
20
- </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>IoT LED Control</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ min-height: 100vh;
18
+ display: flex;
19
+ justify-content: center;
20
+ align-items: center;
21
+ padding: 20px;
22
+ }
23
+
24
+ .container {
25
+ background: rgba(255, 255, 255, 0.95);
26
+ border-radius: 20px;
27
+ padding: 40px;
28
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
29
+ max-width: 500px;
30
+ width: 100%;
31
+ }
32
+
33
+ h1 {
34
+ text-align: center;
35
+ color: #333;
36
+ margin-bottom: 30px;
37
+ font-size: 28px;
38
+ }
39
+
40
+ .status {
41
+ text-align: center;
42
+ padding: 10px;
43
+ border-radius: 10px;
44
+ margin-bottom: 30px;
45
+ font-weight: bold;
46
+ transition: all 0.3s ease;
47
+ }
48
+
49
+ .status.connected {
50
+ background: #d4edda;
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);
62
+ gap: 20px;
63
+ margin-bottom: 30px;
64
+ }
65
+
66
+ .led-card {
67
+ background: #f8f9fa;
68
+ border-radius: 15px;
69
+ padding: 25px;
70
+ text-align: center;
71
+ transition: all 0.3s ease;
72
+ cursor: pointer;
73
+ border: 3px solid transparent;
74
+ }
75
+
76
+ .led-card:hover {
77
+ transform: translateY(-5px);
78
+ box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
79
+ }
80
+
81
+ .led-card.active {
82
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
83
+ border-color: #fff;
84
+ }
85
+
86
+ .led-icon {
87
+ font-size: 48px;
88
+ margin-bottom: 10px;
89
+ transition: all 0.3s ease;
90
+ }
91
+
92
+ .led-card.active .led-icon {
93
+ filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.8));
94
+ }
95
+
96
+ .led-name {
97
+ font-size: 18px;
98
+ font-weight: bold;
99
+ margin-bottom: 5px;
100
+ color: #333;
101
+ }
102
+
103
+ .led-card.active .led-name {
104
+ color: white;
105
+ }
106
+
107
+ .led-status {
108
+ font-size: 14px;
109
+ color: #666;
110
+ }
111
+
112
+ .led-card.active .led-status {
113
+ color: rgba(255, 255, 255, 0.9);
114
+ }
115
+
116
+ .all-off-btn {
117
+ width: 100%;
118
+ padding: 15px;
119
+ font-size: 16px;
120
+ font-weight: bold;
121
+ border: none;
122
+ border-radius: 10px;
123
+ background: #dc3545;
124
+ color: white;
125
+ cursor: pointer;
126
+ transition: all 0.3s ease;
127
+ }
128
+
129
+ .all-off-btn:hover {
130
+ background: #c82333;
131
+ transform: translateY(-2px);
132
+ box-shadow: 0 5px 15px rgba(220, 53, 69, 0.4);
133
+ }
134
+
135
+ .all-off-btn:active {
136
+ transform: translateY(0);
137
+ }
138
+ </style>
139
  </head>
140
  <body>
141
+ <div class="container">
142
+ <h1>🔌 IoT LED Control</h1>
143
+
144
+ <div class="status disconnected" id="status">
145
+ Connecting...
146
+ </div>
147
+
148
+ <div class="led-grid">
149
+ <div class="led-card" data-led="led1" onclick="toggleLED('led1')">
150
+ <div class="led-icon">💡</div>
151
+ <div class="led-name">LED 1</div>
152
+ <div class="led-status">OFF</div>
153
+ </div>
154
+
155
+ <div class="led-card" data-led="led2" onclick="toggleLED('led2')">
156
+ <div class="led-icon">💡</div>
157
+ <div class="led-name">LED 2</div>
158
+ <div class="led-status">OFF</div>
159
+ </div>
160
+
161
+ <div class="led-card" data-led="led3" onclick="toggleLED('led3')">
162
+ <div class="led-icon">💡</div>
163
+ <div class="led-name">LED 3</div>
164
+ <div class="led-status">OFF</div>
165
+ </div>
166
+
167
+ <div class="led-card" data-led="led4" onclick="toggleLED('led4')">
168
+ <div class="led-icon">💡</div>
169
+ <div class="led-name">LED 4</div>
170
+ <div class="led-status">OFF</div>
171
+ </div>
172
+ </div>
173
+
174
+ <button class="all-off-btn" onclick="allOff()">
175
+ Turn All OFF
176
+ </button>
177
+ </div>
178
+
179
+ <script>
180
+ let ledStates = {
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
+ document.getElementById('status').textContent = 'Connected';
195
+ document.getElementById('status').className = 'status connected';
196
+ } catch (error) {
197
+ console.error('Error fetching LED states:', error);
198
+ document.getElementById('status').textContent = 'Connection Error';
199
+ document.getElementById('status').className = 'status disconnected';
200
+ }
201
+ }
202
+
203
+ // Toggle individual LED
204
+ async function toggleLED(ledName) {
205
+ const newState = !ledStates[ledName];
206
+
207
+ try {
208
+ const response = await fetch(`/api/led/${ledName}`, {
209
+ method: 'POST',
210
+ headers: {
211
+ 'Content-Type': 'application/json'
212
+ },
213
+ body: JSON.stringify({ state: newState })
214
+ });
215
+
216
+ const data = await response.json();
217
+ if (data.success) {
218
+ ledStates = data.ledStates;
219
+ updateUI();
220
+ }
221
+ } catch (error) {
222
+ console.error('Error toggling LED:', error);
223
+ }
224
+ }
225
+
226
+ // Turn all LEDs off
227
+ async function allOff() {
228
+ try {
229
+ const response = await fetch('/api/all-off', {
230
+ method: 'POST'
231
+ });
232
+
233
+ const data = await response.json();
234
+ if (data.success) {
235
+ ledStates = data.ledStates;
236
+ updateUI();
237
+ }
238
+ } catch (error) {
239
+ console.error('Error turning off all LEDs:', error);
240
+ }
241
+ }
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
+ if (ledStates[ledName]) {
250
+ card.classList.add('active');
251
+ statusText.textContent = 'ON';
252
+ } else {
253
+ card.classList.remove('active');
254
+ statusText.textContent = 'OFF';
255
+ }
256
+ });
257
+ }
258
+
259
+ // Initialize
260
+ fetchLedStates();
261
+
262
+ // Refresh states every 2 seconds as backup
263
+ setInterval(fetchLedStates, 2000);
264
+ </script>
265
  </body>
266
+ </html>
package-lock.json CHANGED
@@ -8,7 +8,7 @@
8
  "name": "bloomee-tunes",
9
  "version": "1.0.0",
10
  "dependencies": {
11
- "express": "^4.19.2",
12
  "socket.io": "^4.7.5"
13
  },
14
  "engines": {
 
8
  "name": "bloomee-tunes",
9
  "version": "1.0.0",
10
  "dependencies": {
11
+ "express": "^4.21.2",
12
  "socket.io": "^4.7.5"
13
  },
14
  "engines": {
package.json CHANGED
@@ -12,9 +12,7 @@
12
  "node": ">=18"
13
  },
14
  "dependencies": {
15
- "express": "^4.19.2",
16
  "socket.io": "^4.7.5"
17
  }
18
  }
19
-
20
-
 
12
  "node": ">=18"
13
  },
14
  "dependencies": {
15
+ "express": "^4.21.2",
16
  "socket.io": "^4.7.5"
17
  }
18
  }
 
 
server.js CHANGED
@@ -1,25 +1,104 @@
1
  const express = require("express");
2
  const http = require("http");
3
- const socketIo = require("socket.io");
4
 
5
  const app = express();
6
  const server = http.createServer(app);
7
- const io = socketIo(server);
8
 
9
- let ledMode = { mode: "off" }; // "off", "led1", "led2"
 
 
 
 
 
 
10
 
 
 
 
 
11
  app.use(express.static("."));
12
- app.get("/status", (req,res)=> res.json(ledMode));
13
-
14
- io.on("connection", socket => {
15
- console.log("Client connected");
16
- socket.emit("update", ledMode);
17
- socket.on("toggle", data => {
18
- ledMode = { mode: data.mode };
19
- io.emit("update", ledMode);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  });
 
 
 
21
  });
22
 
23
  const PORT = process.env.PORT || 3000;
24
  const HOST = "0.0.0.0";
25
- server.listen(PORT, HOST, ()=> console.log(`Server running at http://${HOST}:${PORT}`));
 
 
 
 
 
1
  const express = require("express");
2
  const http = require("http");
 
3
 
4
  const app = express();
5
  const server = http.createServer(app);
 
6
 
7
+ // Store LED states
8
+ let ledStates = {
9
+ led1: false,
10
+ led2: false,
11
+ led3: false,
12
+ led4: false
13
+ };
14
 
15
+ // Store SSE clients (ESP8266 devices)
16
+ let sseClients = [];
17
+
18
+ app.use(express.json());
19
  app.use(express.static("."));
20
+
21
+ // SSE endpoint for ESP8266
22
+ app.get("/events", (req, res) => {
23
+ res.setHeader("Content-Type", "text/event-stream");
24
+ res.setHeader("Cache-Control", "no-cache");
25
+ res.setHeader("Connection", "keep-alive");
26
+ res.setHeader("Access-Control-Allow-Origin", "*");
27
+
28
+ // Send current state immediately
29
+ Object.keys(ledStates).forEach(led => {
30
+ res.write(`data: ${led}:${ledStates[led] ? 1 : 0}\n\n`);
31
+ });
32
+
33
+ // Add client to list
34
+ sseClients.push(res);
35
+ console.log(`ESP8266 connected. Total clients: ${sseClients.length}`);
36
+
37
+ // Send keepalive every 30 seconds
38
+ const keepaliveInterval = setInterval(() => {
39
+ res.write(`:keepalive\n\n`);
40
+ }, 30000);
41
+
42
+ // Remove client on disconnect
43
+ req.on("close", () => {
44
+ clearInterval(keepaliveInterval);
45
+ sseClients = sseClients.filter(client => client !== res);
46
+ console.log(`ESP8266 disconnected. Total clients: ${sseClients.length}`);
47
+ });
48
+ });
49
+
50
+ // Get current LED states (for frontend)
51
+ 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;
58
+ const { state } = req.body;
59
+
60
+ if (!ledStates.hasOwnProperty(ledName)) {
61
+ return res.status(400).json({ error: "Invalid LED name" });
62
+ }
63
+
64
+ ledStates[ledName] = state;
65
+
66
+ // Broadcast to all ESP8266 devices via SSE
67
+ const message = `data: ${ledName}:${state ? 1 : 0}\n\n`;
68
+ sseClients.forEach(client => {
69
+ try {
70
+ client.write(message);
71
+ } catch (err) {
72
+ console.error("Error sending to client:", err.message);
73
+ }
74
+ });
75
+
76
+ console.log(`${ledName} turned ${state ? "ON" : "OFF"}`);
77
+ res.json({ success: true, ledStates });
78
+ });
79
+
80
+ // Turn all LEDs off
81
+ app.post("/api/all-off", (req, res) => {
82
+ Object.keys(ledStates).forEach(led => {
83
+ ledStates[led] = false;
84
+ const message = `data: ${led}:0\n\n`;
85
+ sseClients.forEach(client => {
86
+ try {
87
+ client.write(message);
88
+ } catch (err) {
89
+ console.error("Error sending to client:", err.message);
90
+ }
91
+ });
92
  });
93
+
94
+ console.log("All LEDs turned OFF");
95
+ res.json({ success: true, ledStates });
96
  });
97
 
98
  const PORT = process.env.PORT || 3000;
99
  const HOST = "0.0.0.0";
100
+ 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
+ });