forked from KrisKasprzak/ESP32_WebPage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperMon.h
483 lines (426 loc) · 15 KB
/
SuperMon.h
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
OK, ya ready for some fun? HTML + CSS styling + javascript all in and undebuggable environment
one trick I've learned to how to debug HTML and CSS code.
get all your HTML code (from html to /html) and past it into this test site
muck with the HTML and CSS code until it's what you want
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro
No clue how to debug javascrip other that write, compile, upload, refresh, guess, repeat
I'm using class designators to set styles and id's for data updating
for example:
the CSS class .tabledata defines with the cell will look like
<td><div class="tabledata" id = "switch"></div></td>
the XML code will update the data where id = "switch"
java script then uses getElementById
document.getElementById("switch").innerHTML="Switch is OFF";
.. now you can have the class define the look AND the class update the content, but you will then need
a class for every data field that must be updated, here's what that will look like
<td><div class="switch"></div></td>
the XML code will update the data where class = "switch"
java script then uses getElementsByClassName
document.getElementsByClassName("switch")[0].style.color=text_color;
the main general sections of a web page are the following and used here
<html>
<style>
// dump CSS style stuff in here
</style>
<body>
<header>
// put header code for cute banners here
</header>
<main>
// the buld of your web page contents
</main>
<footer>
// put cute footer (c) 2021 xyz inc type thing
</footer>
</body>
<script>
// you java code between these tags
</script>
</html>
*/
// note R"KEYWORD( html page code )KEYWORD";
// again I hate strings, so char is it and this method let's us write naturally
const char PAGE_MAIN[] PROGMEM = R"=====(
<!DOCTYPE html>
<html lang="en" class="js-focus-visible">
<title>Web Page Update Demo</title>
<style>
table {
position: relative;
width:100%;
border-spacing: 0px;
}
tr {
border: 1px solid white;
font-family: "Verdana", "Arial", sans-serif;
font-size: 20px;
}
th {
height: 20px;
padding: 3px 15px;
background-color: #343a40;
color: #FFFFFF !important;
}
td {
height: 20px;
padding: 3px 15px;
}
.tabledata {
font-size: 24px;
position: relative;
padding-left: 5px;
padding-top: 5px;
height: 25px;
border-radius: 5px;
color: #FFFFFF;
line-height: 20px;
transition: all 200ms ease-in-out;
background-color: #00AA00;
}
.fanrpmslider {
width: 30%;
height: 55px;
outline: none;
height: 25px;
}
.bodytext {
font-family: "Verdana", "Arial", sans-serif;
font-size: 24px;
text-align: left;
font-weight: light;
border-radius: 5px;
display:inline;
}
.navbar {
width: 100%;
height: 50px;
margin: 0;
padding: 10px 0px;
background-color: #FFF;
color: #000000;
border-bottom: 5px solid #293578;
}
.fixed-top {
position: fixed;
top: 0;
right: 0;
left: 0;
z-index: 1030;
}
.navtitle {
float: left;
height: 50px;
font-family: "Verdana", "Arial", sans-serif;
font-size: 50px;
font-weight: bold;
line-height: 50px;
padding-left: 20px;
}
.navheading {
position: fixed;
left: 60%;
height: 50px;
font-family: "Verdana", "Arial", sans-serif;
font-size: 20px;
font-weight: bold;
line-height: 20px;
padding-right: 20px;
}
.navdata {
justify-content: flex-end;
position: fixed;
left: 70%;
height: 50px;
font-family: "Verdana", "Arial", sans-serif;
font-size: 20px;
font-weight: bold;
line-height: 20px;
padding-right: 20px;
}
.category {
font-family: "Verdana", "Arial", sans-serif;
font-weight: bold;
font-size: 32px;
line-height: 50px;
padding: 20px 10px 0px 10px;
color: #000000;
}
.heading {
font-family: "Verdana", "Arial", sans-serif;
font-weight: normal;
font-size: 28px;
text-align: left;
}
.btn {
background-color: #444444;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.foot {
font-family: "Verdana", "Arial", sans-serif;
font-size: 20px;
position: relative;
height: 30px;
text-align: center;
color: #AAAAAA;
line-height: 20px;
}
.container {
max-width: 1800px;
margin: 0 auto;
}
table tr:first-child th:first-child {
border-top-left-radius: 5px;
}
table tr:first-child th:last-child {
border-top-right-radius: 5px;
}
table tr:last-child td:first-child {
border-bottom-left-radius: 5px;
}
table tr:last-child td:last-child {
border-bottom-right-radius: 5px;
}
</style>
<body style="background-color: #efefef" onload="process()">
<header>
<div class="navbar fixed-top">
<div class="container">
<div class="navtitle">Sensor Monitor</div>
<div class="navdata" id = "date">mm/dd/yyyy</div>
<div class="navheading">DATE</div><br>
<div class="navdata" id = "time">00:00:00</div>
<div class="navheading">TIME</div>
</div>
</div>
</header>
<main class="container" style="margin-top:70px">
<div class="category">Sensor Readings</div>
<div style="border-radius: 10px !important;">
<table style="width:50%">
<colgroup>
<col span="1" style="background-color:rgb(230,230,230); width: 20%; color:#000000 ;">
<col span="1" style="background-color:rgb(200,200,200); width: 15%; color:#000000 ;">
<col span="1" style="background-color:rgb(180,180,180); width: 15%; color:#000000 ;">
</colgroup>
<col span="2"style="background-color:rgb(0,0,0); color:#FFFFFF">
<col span="2"style="background-color:rgb(0,0,0); color:#FFFFFF">
<col span="2"style="background-color:rgb(0,0,0); color:#FFFFFF">
<tr>
<th colspan="1"><div class="heading">Pin</div></th>
<th colspan="1"><div class="heading">Bits</div></th>
<th colspan="1"><div class="heading">Volts</div></th>
</tr>
<tr>
<td><div class="bodytext">Analog pin 34</div></td>
<td><div class="tabledata" id = "b0"></div></td>
<td><div class="tabledata" id = "v0"></div></td>
</tr>
<tr>
<td><div class="bodytext">Analog pin 35</div></td>
<td><div class="tabledata" id = "b1"></div></td>
<td><div class="tabledata" id = "v1"></div></td>
</tr>
<tr>
<td><div class="bodytext">Digital switch</div></td>
<td><div class="tabledata" id = "switch"></div></td>
</tr>
</table>
</div>
<br>
<div class="category">Sensor Controls</div>
<br>
<div class="bodytext">LED </div>
<button type="button" class = "btn" id = "btn0" onclick="ButtonPress0()">Toggle</button>
</div>
<br>
<div class="bodytext">Switch</div>
<button type="button" class = "btn" id = "btn1" onclick="ButtonPress1()">Toggle</button>
</div>
<br>
<br>
<div class="bodytext">Fan Speed Control (RPM: <span id="fanrpm"></span>)</div>
<br>
<input type="range" class="fanrpmslider" min="0" max="255" value = "0" width = "0%" oninput="UpdateSlider(this.value)"/>
<br>
<br>
</main>
<footer div class="foot" id = "temp" >ESP32 Web Page Creation and Data Update Demo</div></footer>
</body>
<script type = "text/javascript">
// global variable visible to all java functions
var xmlHttp=createXmlHttpObject();
// function to create XML object
function createXmlHttpObject(){
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
// function to handle button press from HTML code above
// and send a processing string back to server
// this processing string is use in the .on method
function ButtonPress0() {
var xhttp = new XMLHttpRequest();
var message;
// if you want to handle an immediate reply (like status from the ESP
// handling of the button press use this code
// since this button status from the ESP is in the main XML code
// we don't need this
// remember that if you want immediate processing feedbac you must send it
// in the ESP handling function and here
/*
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
message = this.responseText;
// update some HTML data
}
}
*/
xhttp.open("PUT", "BUTTON_0", false);
xhttp.send();
}
// function to handle button press from HTML code above
// and send a processing string back to server
// this processing string is use in the .on method
function ButtonPress1() {
var xhttp = new XMLHttpRequest();
/*
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("button1").innerHTML = this.responseText;
}
}
*/
xhttp.open("PUT", "BUTTON_1", false);
xhttp.send();
}
function UpdateSlider(value) {
var xhttp = new XMLHttpRequest();
// this time i want immediate feedback to the fan speed
// yea yea yea i realize i'm computing fan speed but the point
// is how to give immediate feedback
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// update the web based on reply from ESP
document.getElementById("fanrpm").innerHTML=this.responseText;
}
}
// this syntax is really weird the ? is a delimiter
// first arg UPDATE_SLIDER is use in the .on method
// server.on("/UPDATE_SLIDER", UpdateSlider);
// then the second arg VALUE is use in the processing function
// String t_state = server.arg("VALUE");
// then + the controls value property
xhttp.open("PUT", "UPDATE_SLIDER?VALUE="+value, true);
xhttp.send();
}
// function to handle the response from the ESP
function response(){
var message;
var barwidth;
var currentsensor;
var xmlResponse;
var xmldoc;
var dt = new Date();
var color = "#e8e8e8";
// get the xml stream
xmlResponse=xmlHttp.responseXML;
// get host date and time
document.getElementById("time").innerHTML = dt.toLocaleTimeString();
document.getElementById("date").innerHTML = dt.toLocaleDateString();
// A0
xmldoc = xmlResponse.getElementsByTagName("B0"); //bits for A0
message = xmldoc[0].firstChild.nodeValue;
if (message > 2048){
color = "#aa0000";
}
else {
color = "#0000aa";
}
barwidth = message / 40.95;
document.getElementById("b0").innerHTML=message;
document.getElementById("b0").style.width=(barwidth+"%");
// if you want to use global color set above in <style> section
// other wise uncomment and let the value dictate the color
//document.getElementById("b0").style.backgroundColor=color;
//document.getElementById("b0").style.borderRadius="5px";
xmldoc = xmlResponse.getElementsByTagName("V0"); //volts for A0
message = xmldoc[0].firstChild.nodeValue;
document.getElementById("v0").innerHTML=message;
document.getElementById("v0").style.width=(barwidth+"%");
// you can set color dynamically, maybe blue below a value, red above
document.getElementById("v0").style.backgroundColor=color;
//document.getElementById("v0").style.borderRadius="5px";
// A1
xmldoc = xmlResponse.getElementsByTagName("B1");
message = xmldoc[0].firstChild.nodeValue;
if (message > 2048){
color = "#aa0000";
}
else {
color = "#0000aa";
}
document.getElementById("b1").innerHTML=message;
width = message / 40.95;
document.getElementById("b1").style.width=(width+"%");
document.getElementById("b1").style.backgroundColor=color;
//document.getElementById("b1").style.borderRadius="5px";
xmldoc = xmlResponse.getElementsByTagName("V1");
message = xmldoc[0].firstChild.nodeValue;
document.getElementById("v1").innerHTML=message;
document.getElementById("v1").style.width=(width+"%");
document.getElementById("v1").style.backgroundColor=color;
//document.getElementById("v1").style.borderRadius="5px";
xmldoc = xmlResponse.getElementsByTagName("LED");
message = xmldoc[0].firstChild.nodeValue;
if (message == 0){
document.getElementById("btn0").innerHTML="Turn ON";
}
else{
document.getElementById("btn0").innerHTML="Turn OFF";
}
xmldoc = xmlResponse.getElementsByTagName("SWITCH");
message = xmldoc[0].firstChild.nodeValue;
document.getElementById("switch").style.backgroundColor="rgb(200,200,200)";
// update the text in the table
if (message == 0){
document.getElementById("switch").innerHTML="Switch is OFF";
document.getElementById("btn1").innerHTML="Turn ON";
document.getElementById("switch").style.color="#0000AA";
}
else {
document.getElementById("switch").innerHTML="Switch is ON";
document.getElementById("btn1").innerHTML="Turn OFF";
document.getElementById("switch").style.color="#00AA00";
}
}
// general processing code for the web page to ask for an XML steam
// this is actually why you need to keep sending data to the page to
// force this call with stuff like this
// server.on("/xml", SendXML);
// otherwise the page will not request XML from the ESP, and updates will not happen
function process(){
if(xmlHttp.readyState==0 || xmlHttp.readyState==4) {
xmlHttp.open("PUT","xml",true);
xmlHttp.onreadystatechange=response;
xmlHttp.send(null);
}
// you may have to play with this value, big pages need more porcessing time, and hence
// a longer timeout
setTimeout("process()",100);
}
</script>
</html>
)=====";