-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebase V.9 Web Application - with Buttons.html
231 lines (213 loc) · 7.15 KB
/
Firebase V.9 Web Application - with Buttons.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
<!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 v.9 modular</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
<style>
.container {
font-size: clamp(16px, 2.5vw, 25px);
margin-top: 20px;
text-align: center;
}
.temperature {
background-color: darkcyan;
color: white;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
border-radius: 10px;
}
.humidity {
background-color: steelblue;
color: white;
padding: 10px 20px;
border-radius: 10px;
}
button {
font-size: clamp(16px, 2.5vw, 25px);
padding: 10px 20px;
margin-bottom: 10px;
border-radius: 10px;
}
.onBtn {
background-color: green;
color: white;
}
.offBtn {
background-color: crimson;
color: white;
}
.onBtn:hover {
background-color: white;
color: green;
cursor: pointer;
}
.offBtn:hover {
background-color: white;
color: crimson;
cursor: pointer;
}
body {
background-color: rgb(68, 68, 68);
}
.myChart {
margin: 20px 20px;
}
.chart-container {
height: 70vh;
width: 100vw;
}
</style>
</head>
<body>
<div class="container">
<p class="temperature">อุณหภูมิ = <span id="temp"></span> องศาเซลเซียส</p>
<p class="humidity">ความชื้น = <span id="humi"></span> %</p>
<button class="onBtn" id="onBtnR1">เปิด Relay #1</button>
<button class="offBtn" id="offBtnR1">ปิด Relay #1</button><br>
<button class="onBtn" id="onBtnR2">เปิด Relay #2</button>
<button class="offBtn" id="offBtnR2">ปิด Relay #2</button>
</div>
<div class="chart-container">
<canvas id="myChart" class="myChart"></canvas>
</div>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.1/firebase-app.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
databaseURL: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
projectId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
messagingSenderId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
appId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
import {
getDatabase,
ref,
onValue,
set,
} from "https://www.gstatic.com/firebasejs/9.9.1/firebase-database.js";
const db = getDatabase();
let tempPoint, humiPoint;
const firebaseTempRef = ref(db, "xxxxx key ของอุณหภูมิ xxxxx");
onValue(firebaseTempRef, (snapshot) => {
let temp = snapshot.val();
tempPoint = temp.toPrecision(4);
document.getElementById("temp").innerHTML = tempPoint;
});
const firebaseHumiRef = ref(db, "xxxxx key ของความชื้น xxxxx");
onValue(firebaseHumiRef, (snapshot) => {
let humi = snapshot.val();
humiPoint = humi.toPrecision(4);
document.getElementById("humi").innerHTML = humiPoint;
updateChart();
});
const Relay1 = ref(db, "xxxxx key ของ Relay1 xxxxx");
const Relay2 = ref(db, "xxxxx key ของ Relay2 xxxxx");
let onBtnR1 = document.getElementById("onBtnR1");
let offBtnR1 = document.getElementById("offBtnR1");
let onBtnR2 = document.getElementById("onBtnR2");
let offBtnR2 = document.getElementById("offBtnR2");
onBtnR1.addEventListener("click", function onRelay1() {
set(Relay1, true);
});
offBtnR1.addEventListener("click", function offRelay1() {
set(Relay1, false);
});
onBtnR2.addEventListener("click", function onRelay2() {
set(Relay2, true);
});
offBtnR2.addEventListener("click", function offRelay2() {
set(Relay2, false);
});
let chartClick = document.getElementById("myChart");
function updateChart() {
myChart.data.labels.push("");
myChart.data.labels.shift();
myChart.data.datasets[0].data.push(tempPoint);
myChart.data.datasets[0].data.shift();
myChart.data.datasets[1].data.push(humiPoint);
myChart.data.datasets[1].data.shift();
myChart.update();
}
const temp = Array(20)
.fill()
.map(function rand() {
return Math.random() * 20 + 20;
});
const humi = Array(20)
.fill()
.map(function rand() {
return Math.random() * 30 + 50;
});
const ctx = document.getElementById("myChart").getContext("2d");
const myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
datasets: [
{
label: "อุณหภมิ (C)",
data: temp,
backgroundColor: "rgba(54, 162, 235, 1)",
borderColor: "rgba(54, 162, 235, 1)",
borderWidth: 3,
tension: 0.3,
},
{
label: "ความชื้น (%)",
data: humi,
backgroundColor: "rgba(255, 206, 86, 1)",
borderColor: "rgba(255, 206, 86, 1)",
borderWidth: 3,
tension: 0.3,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
grid: {
color: "rgba(255, 255, 255, 0.2)",
},
ticks: {
color: "white",
},
},
x: {
ticks: {
color: "white",
},
},
},
plugins: {
title: {
display: true,
text: "Tony Space Didactic Training",
color: "white",
},
legend: {
labels: {
color: "white",
},
},
},
responsive: true,
maintainAspectRatio: false,
},
});
</script>
</body>
</html>