-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
199 lines (168 loc) · 7.19 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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- MQTT JS -->
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<!-- Icon -->
<link rel="icon" href="https://console.hivemq.cloud/assets/05-hivemq-cloud-icon-DxQS0dON.svg">
<title>Sistem Monitoring</title>
<style>
#checkbox {
display: none;
}
.switch {
/* position: relative; */
width: 70px;
height: 70px;
background-color: rgb(99, 99, 99);
border-radius: 50%;
z-index: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid rgb(126, 126, 126);
box-shadow: 0px 0px 3px rgb(2, 2, 2) inset;
}
.switch svg {
width: 1.2em;
}
.switch svg path {
fill: rgb(48, 48, 48);
}
#checkbox:checked+.switch {
box-shadow: 0px 0px 1px rgb(151, 243, 255) inset,
0px 0px 2px rgb(151, 243, 255) inset, 0px 0px 10px rgb(151, 243, 255) inset,
0px 0px 40px rgb(151, 243, 255), 0px 0px 100px rgb(151, 243, 255),
0px 0px 5px rgb(151, 243, 255);
border: 2px solid rgb(255, 255, 255);
background-color: rgb(146, 180, 184);
}
#checkbox:checked+.switch svg {
filter: drop-shadow(0px 0px 5px rgb(151, 243, 255));
}
#checkbox:checked+.switch svg path {
fill: rgb(255, 255, 255);
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="card">
<h5 class="card-header d-flex justify-content-between">
<div>Alat 1</div>
<div id="statusAlat">loading....</div>
</h5>
<div class="card-body">
<div class="row">
<div class="col-sm-5">
<div class="card">
<div class="card-body text-center">
<h5 class="card-title">Suhu</h5>
<h2><span id="suhu">loading....</span> °C</h2>
</div>
</div>
</div>
<div class="col-sm-5">
<div class="card">
<div class="card-body text-center">
<h5 class="card-title">Kelembapan</h5>
<h2><span id="kelembapan">loading....</span> %</h2>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="card">
<div class="card-body">
<center>
<h5 class="card-title" id="statusLampu">Off</h5>
<input id="checkbox" type="checkbox" />
<label class="switch" for="checkbox">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="slider">
<path
d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V256c0 17.7 14.3 32 32 32s32-14.3 32-32V32zM143.5 120.6c13.6-11.3 15.4-31.5 4.1-45.1s-31.5-15.4-45.1-4.1C49.7 115.4 16 181.8 16 256c0 132.5 107.5 240 240 240s240-107.5 240-240c0-74.2-33.8-140.6-86.6-184.6c-13.6-11.3-33.8-9.4-45.1 4.1s-9.4 33.8 4.1 45.1c38.9 32.3 63.5 81 63.5 135.4c0 97.2-78.8 176-176 176s-176-78.8-176-176c0-54.4 24.7-103.1 63.5-135.4z">
</path>
</svg>
</label>
</center>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const clientId = "Replace-with-clientId";
const host = "Replace-with-url-mqtt";
const options = {
keepalive: 30,
clientId: clientId,
username: "username",
password: "Password",
protocolId: 'MQTT',
protocolVersion: 4,
clean: false,
reconnectPeriod: 1000,
connectTimeout: 10000,
};
// Initialize the MQTT client
const client = mqtt.connect(host, options);
// Setup the callbacks
client.on('connect', () => {
console.log('Connected');
document.getElementById("statusAlat").textContent = "Online";
});
client.on('error', (err) => {
console.error('Connection error: ', err);
});
client.on('reconnect', () => {
console.log('Reconnecting...');
document.getElementById("statusAlat").textContent = "Reconnect";
});
client.on('offline', () => {
console.log('Client is offline');
document.getElementById("statusAlat").textContent = "Offline";
});
client.on('close', () => {
console.log('Connection closed');
});
client.subscribe("device1/#", { qos: 0 });
client.on("message", (topic, data) => {
if (topic === "Replace-with-you-topic") {
document.getElementById("suhu").textContent = data;
}
if (topic === "Replace-with-you-topicn") {
document.getElementById("kelembapan").textContent = data;
}
});
// Get the checkbox element
const checkbox = document.getElementById('checkbox');
const lampu = document.getElementById('statusLampu');
// Add an event listener to the checkbox
checkbox.addEventListener('change', () => {
if (checkbox.checked) {
lampu.textContent = "On";
client.publish('Replace-with-you-topic', '1', { qos: 0, retain: false });
} else {
lampu.textContent = "Off";
client.publish('Replace-with-you-topic', '0', { qos: 0, retain: false });
}
});
</script>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct"
crossorigin="anonymous"></script>
</body>
</html>