File size: 9,647 Bytes
fec6368
 
 
 
 
 
ceef3cc
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceef3cc
 
 
 
 
 
 
 
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceef3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6bf683
fec6368
 
 
 
 
 
ceef3cc
 
 
 
 
fec6368
 
ceef3cc
 
fec6368
 
 
ceef3cc
 
fec6368
 
 
 
 
ceef3cc
 
fec6368
 
 
 
ceef3cc
 
 
 
 
 
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceef3cc
 
 
 
 
 
 
 
fec6368
 
 
 
 
 
 
ceef3cc
fec6368
 
 
 
 
 
 
 
ceef3cc
 
fec6368
 
ceef3cc
 
 
 
 
 
 
 
 
 
 
 
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceef3cc
 
 
 
 
 
 
 
fec6368
 
 
 
 
ceef3cc
 
fec6368
 
 
 
 
 
 
 
 
 
 
ceef3cc
fec6368
 
 
 
 
 
 
ceef3cc
fec6368
ceef3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
fec6368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="Permissions-Policy" content="bluetooth=*">
	<title>ReachyMini Controller</title>
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}

		body {
			font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			min-height: 100vh;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 20px;
		}

		.container {
			background: white;
			border-radius: 20px;
			padding: 40px;
			box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
			max-width: 500px;
			width: 100%;
		}

		h1 {
			color: #333;
			margin-bottom: 10px;
			font-size: 28px;
		}

		.status {
			padding: 12px;
			border-radius: 8px;
			margin: 20px 0;
			font-weight: 500;
			text-align: center;
		}

		.status.disconnected {
			background: #fee;
			color: #c33;
		}

		.status.connected {
			background: #efe;
			color: #3c3;
		}

		.status.connecting {
			background: #ffeaa7;
			color: #d63031;
		}

		.connect-btn {
			width: 100%;
			padding: 15px;
			background: #667eea;
			color: white;
			border: none;
			border-radius: 10px;
			font-size: 16px;
			font-weight: 600;
			cursor: pointer;
			transition: all 0.3s;
			margin-bottom: 30px;
		}

		.connect-btn:hover {
			background: #5568d3;
			transform: translateY(-2px);
			box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
		}

		.connect-btn:disabled {
			background: #ccc;
			cursor: not-allowed;
			transform: none;
		}

		.commands {
			display: grid;
			grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
			gap: 15px;
		}

		.command-btn {
			padding: 20px;
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			color: white;
			border: none;
			border-radius: 10px;
			font-size: 14px;
			font-weight: 600;
			cursor: pointer;
			transition: all 0.3s;
			text-transform: uppercase;
			letter-spacing: 0.5px;
		}

		.command-btn:hover:not(:disabled) {
			transform: translateY(-3px);
			box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
		}

		.command-btn:active:not(:disabled) {
			transform: translateY(-1px);
		}

		.command-btn:disabled {
			opacity: 0.5;
			cursor: not-allowed;
			transform: none;
		}

		.command-btn.danger {
			background: linear-gradient(135deg, #ee5a6f 0%, #f29263 100%);
		}

		.command-btn.warning {
			background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
		}

		.log {
			margin-top: 30px;
			padding: 15px;
			background: #f8f9fa;
			border-radius: 8px;
			max-height: 200px;
			overflow-y: auto;
			font-size: 12px;
			font-family: 'Courier New', monospace;
		}

		.log-entry {
			padding: 5px 0;
			border-bottom: 1px solid #e9ecef;
		}

		.log-entry:last-child {
			border-bottom: none;
		}

		.log-entry.error {
			color: #c33;
		}

		.log-entry.success {
			color: #3c3;
		}

		.response-box {
			margin-top: 20px;
			padding: 15px;
			background: #e7f3ff;
			border-left: 4px solid #2196F3;
			border-radius: 4px;
			font-size: 14px;
			font-family: 'Courier New', monospace;
			display: none;
		}

		.response-box.show {
			display: block;
		}

		.note {
			margin-top: 20px;
			padding: 15px;
			background: #fff3cd;
			border-left: 4px solid #ffc107;
			border-radius: 4px;
			font-size: 14px;
			color: #856404;
		}
	</style>
</head>

<body>
	<div class="container">
		<h1>🤖 ReachyMini Controller</h1>

		<div id="status" class="status disconnected">
			Disconnected
		</div>

		<button id="connectBtn" class="connect-btn">
			Connect to ReachyMini
		</button>

		<div class="commands">
			<button class="command-btn" data-command="PING" disabled>Ping</button>
			<button class="command-btn" data-command="STATUS" disabled>Status</button>
			<button class="command-btn" data-command="CMD_HOTSPOT" disabled>Hotspot</button>
			<button class="command-btn danger" data-command="CMD_RESTART_DAEMON" disabled>Restart Daemon</button>
			<button class="command-btn warning" data-command="CMD_SOFTWARE_RESET" disabled>Software Reset</button>
		</div>

		<div id="responseBox" class="response-box"></div>

		<div class="log" id="log"></div>

		<div class="note">
			<strong>Note:</strong> This requires HTTPS and a Chromium-based browser with Web Bluetooth enabled. Access
			from your phone's hotspot for best results.
		</div>
	</div>

	<script>
		let device = null;
		let commandCharacteristic = null;
		let responseCharacteristic = null;
		const statusEl = document.getElementById('status');
		const connectBtn = document.getElementById('connectBtn');
		const commandBtns = document.querySelectorAll('.command-btn');
		const logEl = document.getElementById('log');
		const responseBox = document.getElementById('responseBox');

		// Correct UUIDs from your server
		const SERVICE_UUID = '12345678-1234-5678-1234-56789abcdef0';
		const COMMAND_CHAR_UUID = '12345678-1234-5678-1234-56789abcdef1';
		const RESPONSE_CHAR_UUID = '12345678-1234-5678-1234-56789abcdef2';

		// Check if Web Bluetooth is supported
		if (!navigator.bluetooth) {
			updateStatus('Web Bluetooth not supported', 'disconnected');
			addLog('ERROR: Web Bluetooth API not available in this browser', 'error');
			connectBtn.disabled = true;
		}

		function addLog(message, type = '') {
			const entry = document.createElement('div');
			entry.className = `log-entry ${type}`;
			const timestamp = new Date().toLocaleTimeString();
			entry.textContent = `[${timestamp}] ${message}`;
			logEl.appendChild(entry);
			logEl.scrollTop = logEl.scrollHeight;
		}

		function updateStatus(message, state) {
			statusEl.textContent = message;
			statusEl.className = `status ${state}`;
		}

		function showResponse(message) {
			responseBox.textContent = `Response: ${message}`;
			responseBox.classList.add('show');
			setTimeout(() => {
				responseBox.classList.remove('show');
			}, 5000);
		}

		async function connectToDevice() {
			try {
				updateStatus('Scanning for devices...', 'connecting');
				addLog('Requesting Bluetooth device...');

				device = await navigator.bluetooth.requestDevice({
					filters: [{ name: 'ReachyMini' }],
					optionalServices: [SERVICE_UUID]
				});

				addLog(`Found device: ${device.name}`);
				updateStatus('Connecting...', 'connecting');

				const server = await device.gatt.connect();
				addLog('Connected to GATT server');

				// Get the service
				const service = await server.getPrimaryService(SERVICE_UUID);
				addLog('Got service');

				// Get the command characteristic (write)
				commandCharacteristic = await service.getCharacteristic(COMMAND_CHAR_UUID);
				addLog('Got command characteristic');

				// Get the response characteristic (read/notify)
				responseCharacteristic = await service.getCharacteristic(RESPONSE_CHAR_UUID);
				addLog('Got response characteristic');

				// Enable notifications for responses
				await responseCharacteristic.startNotifications();
				responseCharacteristic.addEventListener('characteristicvaluechanged', handleResponse);
				addLog('Notifications enabled for responses');

				updateStatus('Connected to ReachyMini', 'connected');
				addLog('Successfully connected!', 'success');

				connectBtn.textContent = 'Disconnect';
				commandBtns.forEach(btn => btn.disabled = false);

				device.addEventListener('gattserverdisconnected', onDisconnected);

			} catch (error) {
				addLog(`Connection failed: ${error.message}`, 'error');
				updateStatus('Connection failed', 'disconnected');
				console.error('Connection error:', error);
			}
		}

		function handleResponse(event) {
			const value = event.target.value;
			const decoder = new TextDecoder('utf-8');
			const response = decoder.decode(value);
			addLog(`Response: ${response}`, 'success');
			showResponse(response);
		}

		function onDisconnected() {
			updateStatus('Disconnected', 'disconnected');
			addLog('Device disconnected', 'error');
			connectBtn.textContent = 'Connect to ReachyMini';
			commandBtns.forEach(btn => btn.disabled = true);
			commandCharacteristic = null;
			responseCharacteristic = null;
			device = null;
		}

		async function disconnect() {
			if (device && device.gatt.connected) {
				device.gatt.disconnect();
				addLog('Manually disconnected');
			}
		}

		async function sendCommand(command) {
			if (!commandCharacteristic) {
				addLog('Not connected to device', 'error');
				return;
			}

			try {
				const encoder = new TextEncoder();
				const data = encoder.encode(command);
				await commandCharacteristic.writeValue(data);
				addLog(`Sent command: ${command}`, 'success');

				// Try to read the response
				try {
					const value = await responseCharacteristic.readValue();
					const decoder = new TextDecoder('utf-8');
					const response = decoder.decode(value);
					if (response) {
						addLog(`Response: ${response}`, 'success');
						showResponse(response);
					}
				} catch (readError) {
					// Response will come via notification
					addLog('Waiting for response notification...', '');
				}
			} catch (error) {
				addLog(`Failed to send command: ${error.message}`, 'error');
				console.error('Send error:', error);
			}
		}

		connectBtn.addEventListener('click', async () => {
			if (device && device.gatt.connected) {
				await disconnect();
			} else {
				await connectToDevice();
			}
		});

		commandBtns.forEach(btn => {
			btn.addEventListener('click', () => {
				const command = btn.dataset.command;
				sendCommand(command);
			});
		});

		addLog('Ready. Click "Connect to ReachyMini" to start.');
	</script>
</body>

</html>