-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
81 lines (76 loc) · 2.55 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-Time Data</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<h1>Real-Time Sensor Data</h1>
<canvas id="myChart" width="800" height="400"></canvas>
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [
{
label: 'Temperature (°C)',
borderColor: 'red',
data: []
},
{
label: 'Humidity (%)',
borderColor: 'blue',
data: []
},
{
label: 'LDR Value',
borderColor: 'green',
data: []
}
]
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: 'Timestamp'
}
},
y: {
title: {
display: true,
text: 'Value'
}
}
}
}
});
function fetchData() {
fetch('/data')
.then(response => response.json())
.then(data => {
const timestamp = data.timestamp;
chart.data.labels.push(timestamp);
chart.data.datasets[0].data.push(data.temp);
chart.data.datasets[1].data.push(data.hum);
chart.data.datasets[2].data.push(data.ldr);
if (chart.data.labels.length > 10) {
chart.data.labels.shift();
chart.data.datasets[0].data.shift();
chart.data.datasets[1].data.shift();
chart.data.datasets[2].data.shift();
}
chart.update();
})
.catch(error => console.error('Error fetching data:', error));
}
setInterval(fetchData, 1000);
</script>
</body>
</html>