-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
131 lines (112 loc) · 4.63 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wind Simulator Control</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
color: #333;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 15px 30px;
margin: 10px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
button:active {
background-color: #004494;
}
.sensor-data {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<h1>Wind Simulator Control</h1>
<button id="connectButton">Connect to Device</button>
<button id="toggleButton">Toggle MSS</button>
<div class="sensor-data" id="sensorData">
<p>Current: <span id="current">N/A</span> A</p>
<p>PT1 ADC: <span id="pt1adc">N/A</span></p>
<p>PT2 ADC: <span id="pt2adc">N/A</span></p>
<p>Tach Speed: <span id="tachspeed">N/A</span></p>
<p>Temperature: <span id="temperature">N/A</span> °C</p>
<p>Humidity: <span id="humidity">N/A</span> %</p>
</div>
<script>
const SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const TOGGLE_MSS_CHARACTERISTIC_UUID = "d4c3d8a6-3c4a-11ec-8d3d-0242ac130004";
const SENSOR_DATA_CHARACTERISTIC_UUID = "d4c3d8a6-3c4a-11ec-8d3d-0242ac130005";
let toggleCharacteristic;
let sensorDataCharacteristic;
async function connectToDevice() {
try {
console.log('Requesting Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: [SERVICE_UUID] }]
});
console.log('Connecting to GATT Server...');
const server = await device.gatt.connect();
console.log('Getting Service...');
const service = await server.getPrimaryService(SERVICE_UUID);
console.log('Getting Characteristics...');
toggleCharacteristic = await service.getCharacteristic(TOGGLE_MSS_CHARACTERISTIC_UUID);
sensorDataCharacteristic = await service.getCharacteristic(SENSOR_DATA_CHARACTERISTIC_UUID);
console.log('Connected to device');
// Start notifications for sensor data
sensorDataCharacteristic.startNotifications();
sensorDataCharacteristic.addEventListener('characteristicvaluechanged', handleSensorData);
} catch (error) {
console.error('Error:', error);
}
}
async function toggleMSS() {
if (!toggleCharacteristic) {
console.error('Not connected to device');
return;
}
try {
console.log('Toggling MSS...');
const value = new Uint8Array([1]); // Value to write (can be anything)
await toggleCharacteristic.writeValue(value);
console.log('MSS toggled');
} catch (error) {
console.error('Error:', error);
}
}
function handleSensorData(event) {
const value = new TextDecoder().decode(event.target.value);
const [current, pt1adc, pt2adc, tachspeed, temperature, humidity] = value.split(',');
document.getElementById('current').textContent = current;
document.getElementById('pt1adc').textContent = pt1adc;
document.getElementById('pt2adc').textContent = pt2adc;
document.getElementById('tachspeed').textContent = tachspeed;
document.getElementById('temperature').textContent = temperature;
document.getElementById('humidity').textContent = humidity;
}
document.getElementById('connectButton').addEventListener('click', connectToDevice);
document.getElementById('toggleButton').addEventListener('click', toggleMSS);
</script>
</body>
</html>