-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.html
279 lines (128 loc) · 4.74 KB
/
battery.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Voltage and Gradient (Volts per Hour)</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: lightblue;
}
#container {
text-align: center;
}
#plotly-chart {
display: inline-block;
}
</style>
</head>
<body>
<div id="container">
<div id="plotly-chart"></div>
</div>
<script>
// Function to fetch data from ThingSpeak API
async function fetchData() {
const CHANNEL_ID = '2026685';
const READ_API_KEY = 'ONB2BAY33QO2YUAU';
const num_entries = 10000;
const api_url = `https://api.thingspeak.com/channels/${CHANNEL_ID}/feeds.json?api_key=${READ_API_KEY}&results=${num_entries}`;
const response = await fetch(api_url);
if (response.ok) {
const data = await response.json();
return data.feeds;
} else {
console.error(`Error: ${response.status} - ${response.statusText}`);
return null;
}
}
// Function to create and display the Plotly chart
async function createChart() {
const data = await fetchData();
if (data) {
const time = data.map(entry => new Date(entry.created_at));
const voltage = data.map(entry => parseFloat(entry.field7));
const windowSize = 10;
const voltageSmoothed = movingAverage(voltage, windowSize);
const timeSeconds = time.map(t => (t - time[0]) / 1000);
const dv_dtSmoothed = numericGradient(voltageSmoothed, timeSeconds);
const voltsPerHour = numericRound(numericMultiply(dv_dtSmoothed, 3600), 3);
const finalGradient = voltsPerHour[voltsPerHour.length - 1];
const trace1 = {
x: time,
y: voltageSmoothed,
mode: 'lines',
name: 'Smoothed Voltage'
};
const trace2 = {
x: time,
y: voltsPerHour,
mode: 'lines',
name: 'Smoothed Gradient (Volts per Hour)',
yaxis: 'y2'
};
const layout = {
title: 'Smoothed Voltage and Gradient (Volts per Hour)',
xaxis: { title: 'Time' },
yaxis: { title: 'Smoothed Voltage' },
yaxis2: { title: 'Volts per Hour', overlaying: 'y', side: 'right' },
hovermode: 'x unified',
height: 800,
width: 1200,
font: { size: 16 }
};
const annotation = {
x: time[time.length - 1],
y: Math.min(...voltsPerHour),
text: `Final Gradient: ${finalGradient.toFixed(3)} Volts/hour`,
showarrow: true,
arrowhead: 7,
ax: 0,
ay: 40,
xref: 'x',
yref: 'y',
xshift: 10,
yshift: 0
};
Plotly.newPlot('plotly-chart', [trace1, trace2], layout);
Plotly.addTraces('plotly-chart', [annotation]);
}
}
// Function to calculate moving average
function movingAverage(values, windowSize) {
const movingAverageArray = [];
for (let i = 0; i < values.length - windowSize + 1; i++) {
const window = values.slice(i, i + windowSize);
const average = window.reduce((sum, value) => sum + value, 0) / windowSize;
movingAverageArray.push(average);
}
return movingAverageArray;
}
// Function to calculate numeric gradient
function numericGradient(y, x) {
const gradientArray = [];
for (let i = 0; i < y.length - 1; i++) {
const gradient = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
gradientArray.push(gradient);
}
return gradientArray;
}
// Function to perform numeric rounding
function numericRound(array, decimals) {
return array.map(value => parseFloat(value.toFixed(decimals)));
}
// Function to perform numeric multiplication
function numericMultiply(array, factor) {
return array.map(value => value * factor);
}
// Create the chart when the page loads
document.addEventListener('DOMContentLoaded', createChart);
</script>
</body>
</html>