-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbrowser_controller.html
More file actions
302 lines (263 loc) · 11.4 KB
/
browser_controller.html
File metadata and controls
302 lines (263 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html>
<head>
<title>ESP32-CAM Browser Controller</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f9f9f9; }
.container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.controls { margin: 20px 0; }
button { margin: 5px; padding: 10px 20px; font-size: 16px; border: none; border-radius: 4px; cursor: pointer; background: #007bff; color: white; }
button:hover { background: #0056b3; }
button:disabled { background: #ccc; cursor: not-allowed; }
.status { margin: 10px 0; padding: 10px; background: #f0f0f0; border-radius: 5px; }
.image-container { margin: 20px 0; text-align: center; }
.image-container img { max-width: 100%; border: 1px solid #ccc; border-radius: 4px; }
.error { color: red; background: #ffe6e6; border-left: 4px solid red; }
.success { color: green; background: #e6ffe6; border-left: 4px solid green; }
.response { font-family: monospace; background: #f8f8f8; padding: 10px; margin: 10px 0; border-radius: 5px; max-height: 300px; overflow-y: auto; border: 1px solid #ddd; }
.hidden { display: none; }
.visible { display: block; }
.url-input { width: 300px; padding: 8px; margin: 5px; border: 1px solid #ddd; border-radius: 4px; }
.control-group { margin: 15px 0; padding: 15px; background: #f8f9fa; border-radius: 4px; }
.control-group h3 { margin-top: 0; color: #333; }
</style>
</head>
<body>
<div class="container">
<h1>ESP32-CAM Browser Controller</h1>
<p>This page demonstrates direct browser access to the ESP32-CAM MCP server using CORS headers.</p>
<div class="control-group">
<h3>Connection Settings</h3>
<label for="esp32-url">ESP32-CAM URL:</label>
<input type="text" id="esp32-url" value="http://esp32-7c9ebdf16a10.local" class="url-input">
<button onclick="testConnection()">Test Connection</button>
</div>
<div class="control-group">
<h3>MCP Protocol</h3>
<button onclick="initialize()">Initialize MCP</button>
<button onclick="listTools()">List Tools</button>
</div>
<div class="control-group">
<h3>System Information</h3>
<button onclick="getStatus()">System Status</button>
<button onclick="getWiFiStatus()">WiFi Status</button>
</div>
<div class="control-group">
<h3>Camera Controls</h3>
<button onclick="ledOn()">LED On</button>
<button onclick="ledOff()">LED Off</button>
<button onclick="flashCamera()">Flash Camera</button>
<button onclick="captureImage('off')">Capture Image (No Flash)</button>
<button onclick="captureImage('on')">Capture Image (With Flash)</button>
</div>
<div id="status" class="status">Ready to connect...</div>
<div class="control-group">
<h3>Response Data</h3>
<button onclick="clearResponse()">Clear Response</button>
</div>
<div id="response" class="response hidden"></div>
<div id="image-container" class="image-container hidden">
<h3>Captured Image:</h3>
<img id="captured-image" alt="Captured from ESP32-CAM">
</div>
</div>
<script>
let requestId = 1;
function getESP32URL() {
const url = document.getElementById('esp32-url').value.trim();
if (!url) {
updateStatus('Please enter a valid ESP32-CAM URL', true);
return null;
}
// Ensure URL starts with http:// or https://
if (!url.startsWith('http://') && !url.startsWith('https://')) {
return 'http://' + url;
}
return url;
}
function updateStatus(message, isError = false) {
const statusDiv = document.getElementById('status');
statusDiv.textContent = message;
statusDiv.className = isError ? 'status error' : 'status success';
// Auto-clear success messages after 5 seconds
if (!isError) {
setTimeout(() => {
if (statusDiv.textContent === message) {
statusDiv.textContent = 'Ready...';
statusDiv.className = 'status';
}
}, 5000);
}
}
function showResponse(response) {
const responseDiv = document.getElementById('response');
responseDiv.textContent = JSON.stringify(response, null, 2);
responseDiv.className = 'response visible';
}
function clearResponse() {
const responseDiv = document.getElementById('response');
responseDiv.textContent = '';
responseDiv.className = 'response hidden';
updateStatus('Response cleared');
}
async function testConnection() {
const url = getESP32URL();
if (!url) {
return;
}
try {
updateStatus('Testing connection...');
const response = await fetch(url, {
method: 'OPTIONS',
headers: {
'Origin': window.location.origin
}
});
if (response.ok) {
updateStatus(`Connection successful! ESP32-CAM is reachable at ${url}`);
} else {
updateStatus(`Connection test failed: HTTP ${response.status}`, true);
}
} catch (error) {
updateStatus(`Connection test failed: ${error.message}`, true);
}
}
async function sendMCPRequest(method, params = null) {
const url = getESP32URL();
if (!url) {
return null;
}
const payload = {
jsonrpc: "2024-11-05",
id: requestId++,
method: method
};
if (params) {
payload.params = params;
}
try {
updateStatus(`Sending ${method} request...`);
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
showResponse(data);
if (data.error) {
updateStatus(`Error: ${data.error.message}`, true);
return null;
} else {
updateStatus(`${method} completed successfully`);
return data.result;
}
} catch (error) {
let errorMessage = error.message;
if (error.name === 'TypeError' && error.message.includes('fetch')) {
errorMessage = `Connection failed - check if ESP32-CAM is online and URL is correct`;
}
updateStatus(`Request failed: ${errorMessage}`, true);
console.error('Request error:', error);
return null;
}
}
async function initialize() {
const result = await sendMCPRequest('initialize', {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: {
name: "Browser MCP Client",
version: "1.0.0"
}
});
if (result) {
updateStatus(`Connected to ${result.serverInfo?.name || 'ESP32-CAM'}`);
}
}
async function listTools() {
const result = await sendMCPRequest('tools/list');
if (result) {
updateStatus(`Found ${result.tools?.length || 0} available tools`);
}
}
async function ledOn() {
const result = await sendMCPRequest('tools/call', {
name: 'led',
arguments: { state: 'on' }
});
if (result) {
updateStatus('LED turned on');
}
}
async function ledOff() {
const result = await sendMCPRequest('tools/call', {
name: 'led',
arguments: { state: 'off' }
});
if (result) {
updateStatus('LED turned off');
}
}
async function flashCamera() {
const result = await sendMCPRequest('tools/call', {
name: 'flash',
arguments: {}
});
if (result) {
updateStatus('Flash executed');
}
}
async function getStatus() {
await sendMCPRequest('tools/call', {
name: 'system_status',
arguments: {}
});
}
async function getWiFiStatus() {
await sendMCPRequest('tools/call', {
name: 'wifi_status',
arguments: {}
});
}
async function captureImage(flash = 'off') {
const result = await sendMCPRequest('tools/call', {
name: 'capture',
arguments: { flash: flash }
});
if (result && result.content && result.content.length > 0) {
// Look for the image content in the response
let imageContent = null;
let textContent = null;
for (let i = 0; i < result.content.length; i++) {
const content = result.content[i];
if (content.type === 'image') {
imageContent = content;
} else if (content.type === 'text') {
textContent = content;
}
}
if (imageContent && imageContent.data) {
const img = document.getElementById('captured-image');
img.src = `data:${imageContent.mimeType || 'image/jpeg'};base64,${imageContent.data}`;
document.getElementById('image-container').className = 'image-container visible';
const statusMsg = textContent ? textContent.text : `Image captured (${imageContent.data.length} bytes base64)`;
updateStatus(statusMsg);
} else {
updateStatus('Image capture succeeded but no image data found in response', true);
}
} else {
updateStatus('Image capture failed or returned empty response', true);
}
}
// Test CORS support on page load
window.addEventListener('load', function() {
updateStatus('Page loaded. CORS headers enable direct browser access to ESP32-CAM.');
});
</script>
</body>
</html>