-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
362 lines (329 loc) · 11.2 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Firebase-IoT-Dashboard</title>
<link rel="icon" href="https://github.githubassets.com/favicons/favicon-dark.png">
<!-- update the version number as needed -->
<script defer src="/__/firebase/8.4.2/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/8.4.2/firebase-database.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>
<!-- firebase.database().ref('/path/to/ref').on('value', snapshot => { }); -->
<!------------------------Styling------------------------->
<style>
body{
height: 100vh;
font-family: sans-serif;
font-size: 100%;
text-align: center;
background-color: #d8d8d8;
box-sizing: border-box;
margin: 0;
display:grid;
}
header, footer{
/*width: 100vw;*/
background: #5cc0fa;
color: #fff;
}
header {
grid-row-start: 1;
grid-row-end: 2;
}
footer {
grid-row-start: 8;
grid-row-end: 9;
}
.panel{
color: #474747;
width: 70vw;
border: 1px solid #000;
border-radius: 30px;
background-color: #ffffff;
margin: auto;
display: grid;
grid-auto-flow: column;
grid-gap: 20px;
padding: 2vh 1.2vw;
grid-row-start: 4;
grid-row-end: 6;
}
.texts {
grid-column: 2 / span 3;
}
.panel .userPanel, .panel .mcuPanel {
border: 1px solid #000;
border-radius: 20px;
padding: 2%;
}
.panel .userPanel {
grid-row-start : 1;
grid-row-end : 3;
display: grid;
}
.panel .mcuPanel {
grid-row-start : 3;
grid-row-end : 5;
display: grid;
}
.toggle {
grid-column-start: 1;
grid-column-end: 3;
}
.slider {
grid-column-start: 3;
grid-column-end: 6;
}
.gauge {
grid-column-start: 1;
grid-column-end: 3;
}
.state {
grid-column-start: 3;
grid-column-end: 6;
}
.state #statebox {
width: 100px;
border: 1px solid #a5a5a5;
border-radius: 10px;
margin: 0 auto;
}
#statebox {
padding: 10px;
color: #fff;
background-color: #67bd5f;
}
#gaugeReading {
font-size: 75%;
text-align: center;
}
/*======Styling required for toggle switch======*/
/* The switch - the box around the slider */
.toggle .switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.toggle .switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The key */
.toggle .key {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.toggle .key:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
.toggle input:checked + .key {
background-color: #2196F3;
}
.toggle input:focus + .key {
box-shadow: 0 0 1px #2196F3;
}
.toggle input:checked + .key:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.toggle .key.round {
border-radius: 34px;
}
.toggle .key.round:before {
border-radius: 50%;
}
/*======Styling required for toggle switch======*/
.gauge #gaugeMinVal, #gaugeMaxVal {
font-size: 75%;
display: inline;
}
#connectionState {
color: #474747;
z-index: 100;
}
/*========Just for responsiveness========*/
@media only screen and (max-width: 845px) {
h1 {
margin : 0.2em;
font-size: 180%;
}
p {
margin: 0.5em;
font-size: 75%;
}
.panel {
display: block;
padding: 10px;
width: 80vw;
}
.panel .userPanel, .panel .mcuPanel {
display: block;
}
.panel .userPanel {
margin-bottom: 10px;
}
}
/*========Just for responsiveness========*/
</style>
<!------------------------Styling------------------------->
</head>
<body>
<!------------------------Page Layout Content------------------------->
<header>
<h1>IoT Firebase dashboard</h1>
</header>
<p id="connectionState">Connecting to server....!</p>
<!--First panel-->
<div class="panel 1">
<div class="userPanel">
<h3 class="texts">Control GPIOs</h3>
<!--button to toggle-->
<div class="toggle">
<h4>Toggle</h4>
<label class="switch">
<input type="checkbox" id="button">
<span class="key round"></span>
</label>
<div id='buttonval'></div>
</div>
<!--slider to adjust-->
<div class="slider">
<h4>Adjust</h4>
<input type="range" id="slidervalue" name="slidervalue" min="0" max="1023">
<div id='sliderval'></div>
</div>
</div>
<div class="mcuPanel">
<h3 class="texts">Display Data</h3>
<!--Gauge to show readings-->
<div class="gauge">
<h4>Reading</h4>
<div id="gaugeMinVal"></div>
<meter id="rangeval" value="512" min="0" max="1023"></meter>
<div id="gaugeMaxVal"></div>
<br>
<div id="gaugeReading"></div>
</div>
<div class="state">
<h4>State</h4>
<div id="statebox">True</div>
</div>
</div>
</div>
<footer>
<p>A contribution to the Robotics Society - KGEC</p>
</footer>
<!------------------------Page Layout Content------------------------->
<!------------------------Scripting------------------------->
<script>
// The output objects
const button = document.getElementById("button");
const slider = document.getElementById("slidervalue");
// Objects to display the value
const outputSlider = document.getElementById("sliderval");
const outputSwitch = document.getElementById("buttonval");
const outputGauge = document.getElementById("rangeval");
const gaugeMinVal = document.getElementById("gaugeMinVal");
const gaugeMaxVal = document.getElementById("gaugeMaxVal");
const gaugeReading = document.getElementById("gaugeReading");
const outputState = document.getElementById("statebox");
const connectionState = document.getElementById("connectionState");
/*==================Functions===================*/
function setOutputState(state_value){
outputState.innerHTML = state_value;
if (state_value)
outputState.style.backgroundColor = "#67bd5f";
else
outputState.style.backgroundColor = "#606164";
}
/*===== Firebase Methods =====*/
document.addEventListener("DOMContentLoaded", () => {
console.log(firebase);
const database = firebase.database();
// When the HTML body loads for the first time
document.body.onload = () => {
outputSlider.innerHTML = slider.value; // Display the default slider value
outputSwitch.innerHTML = button.checked ? "on" : "off"; // Display the default button value
gaugeMinVal.innerHTML = outputGauge.min;
gaugeMaxVal.innerHTML = outputGauge.max;
gaugeReading.innerHTML = outputGauge.value;
document.getElementsByClassName("panel").hidden = true;
}
// flag
var firstLoad = true;
updateView = (data) => {
// The State box
setOutputState(data.Inputs.D0);
// The Reading Gauge
outputGauge.value = data.Inputs.A0;
gaugeReading.innerHTML = outputGauge.value;
// The Toggle Switch
button.checked = data.Keys.state;
outputSwitch.innerHTML = button.checked ? "on" : "off";
// The Adjust Slider
slider.value = data.Keys.level;
outputSlider.innerHTML = slider.value;
};
updateDatabase = () => {
var data = {
level: Number(slider.value),
state: button.checked,
};
// Show the state on the page
outputSwitch.innerHTML = button.checked ? "on" : "off";
// Show the level on the page
outputSlider.innerHTML = slider.value;
console.log(data);
console.log(database.ref("/devices/Keys").update(data));
};
removeConnectingMsg = () => {
connectionState.hidden = true;
document.getElementsByClassName("panel").hidden = false;
};
// event listeners
button.addEventListener("change", (event) => updateDatabase());
slider.addEventListener("change", (event) => updateDatabase());
slider.oninput = () => {
outputSlider.innerHTML = slider.value;
}
// database event listeners
const keysRef = database.ref("/devices");
keysRef.on("value", (snap) => {
const data = snap.val();
console.log(data);
if (firstLoad) {
removeConnectingMsg();
firstLoad = false;
}
updateView(data);
});
});
/*====================*/
</script>
<!------------------------Scripting------------------------->
</body>
</html>