-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
171 lines (150 loc) · 4.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
<!doctype html>
<html>
<head>
<title>RasPi</title>
<script src ="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#dataTable {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#dataTable td, #dataTable th {
border: 1px solid #ddd;
text-align:center;
padding: 8px;
}
#dataTable tr:nth-child(even){text_align:center;background-color: #f2f2f2;}
#dataTable tr:hover {text-align:center; background-color: #ddd;}
#dataTable th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div style="text-align:center;"><br>Real Time Data Logging with Graphs on RasPi</div>
<div class="chart-container" position: relative; height:350px; width:100%">
<canvas id="Chart" width="400" height="400"></canvas>
</div> <div>
<input id="inputFileNameToSaveAs"></input>
<button onclick="saveTextAsFile()">Save Text to File</button>
<table id="dataTable">
<tr><th>Date-Time</th><th>Left RPM</th><th>Right RPM</th><th>UltraSonic
Distance</th></tr>
</table> </div> <br> <br>
<script type="text/javascript">
var valuesUS = [];
var timeStamp = [];
var entry="";
function showGraph() {
for (i = 0; i < arguments.length; i++) {
valuesUS.push(arguments[i]);
}
var ctx = document.getElementById("Chart").getContext('2d');
var Chart2 = new Chart(ctx, {
type: 'line',
data: {
labels: timeStamp, //Bottom Labeling
datasets: [{
label: "Distance",
fill: false, //Try with true
backgroundColor: 'rgba( 243, 156, 18 , 1)', //Dot marker color
borderColor: 'rgba( 243, 156, 18 , 1)', //Graph Line Color
data: valuesUS,
}],
},
options: {
title: {
display: true,
text: "UltraSonic"
},
maintainAspectRatio: false,
elements: {
line: {
tension: 0.5 //Smoothening (Curved) of data lines
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
//On Page load show graphs
window.onload = function() {
console.log(new Date().toLocaleTimeString());
showGraph(0,0,0,0);
};
//Read This tutorial https://circuits4you.com/2018/02/04/esp8266-ajax-update-part-of-web-page-
without-refreshing/
setInterval(function() {
// Call a function repetatively with 5Second interval
getData();
}, 500); //500mSeconds update rate
function saveTextAsFile() {
var textToSave = entry;
var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event) {
document.body.removeChild(event.target);
}
function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//Push the data in array
var date = new Date().toLocaleDateString();
var time = new Date().toLocaleTimeString();
var filedata = String(this.responseText);
var s1 = filedata.indexOf('#');
var s2 = filedata.indexOf('%');
var l = filedata.length;
var IR1 = filedata.substring(0,s1);
var IR2 = filedata.substring(s1+1,s2);
var US = filedata.substring(s2+1,l);
valuesUS.push(US);
timeStamp.push(date+" "+time);
showGraph(); //Update Graphs
//Update Data Table
var table = document.getElementById("dataTable");
var row = table.insertRow(1); //Add after headings
entry = entry+"TIME:"+time+" "+"IR1:"+IR1+" "+"IR2:"+IR2+" "+"US:"+US+"\r\n";
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = date+" "+time;
cell2.innerHTML = IR1;
cell3.innerHTML = IR2;
cell4.innerHTML = US;
}
};
xhttp.open("GET", "readValue", true); //Handle readADC server on ESP8266
xhttp.send();
}
</script>
</body>
</html>