-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature.html
154 lines (135 loc) · 4.31 KB
/
temperature.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Temperature Data</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
text-align: center;
background-color: #f4f4f9;
}
h1 {
color: #333;
margin-top: 20px;
}
.data-container {
margin: 20px auto;
width: 80%;
max-width: 600px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
p {
font-size: 18px;
color: #555;
}
.chart-container {
margin: 30px auto;
width: 80%;
max-width: 600px;
}
.button-container {
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Temperature Sensor Data</h1>
<div class="data-container">
<p id="temperature_data">Loading...</p>
</div>
<div class="chart-container">
<canvas id="temperatureChart"></canvas>
</div>
<div class="button-container">
<button onclick="toggleDataUpdate()">Start/Stop Data</button>
<button onclick="resetChart()">Reset Chart</button>
</div>
<script>
let updateInterval;
let chartData = {
labels: [],
datasets: [{
label: 'Temperature (°C)',
data: [],
borderColor: 'rgb(255, 159, 64)',
tension: 0.1
}]
};
const ctx = document.getElementById('temperatureChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: chartData,
options: {
responsive: true,
animation: {
duration: 500,
easing: 'easeOutQuart'
},
scales: {
y: {
beginAtZero: false,
max: 100 // Adjust based on expected temperature range
}
}
}
});
// Function to update Temperature data
function fetchTemperatureData() {
fetch('/get_temperature_data')
.then(response => response.json())
.then(data => {
document.getElementById("temperature_data").innerText =
`Temperature: ${data.temperature} °C`;
// Add data to chart
const currentTime = new Date().toLocaleTimeString();
chartData.labels.push(currentTime);
chartData.datasets[0].data.push(data.temperature);
// Remove old data to keep chart manageable
if (chartData.labels.length > 10) {
chartData.labels.shift();
chartData.datasets[0].data.shift();
}
chart.update();
});
}
// Function to start/stop data updates
function toggleDataUpdate() {
if (updateInterval) {
clearInterval(updateInterval);
updateInterval = null;
} else {
updateInterval = setInterval(fetchTemperatureData, 2000);
}
}
// Function to reset chart
function resetChart() {
chartData.labels = [];
chartData.datasets[0].data = [];
chart.update();
}
// Start data fetching automatically on page load
toggleDataUpdate();
</script>
</body>
</html>