-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.h
211 lines (196 loc) · 6.38 KB
/
index.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
const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>local data</title>
</head>
<style>
*{margin: 0;padding: 0;}
body{
background:#544947;
font-family:sans-serif;
}
h2{
font-size:14px;
}
.widget{
margin:100px auto;
height: 330px;
position: relative;
width: 500px;
}
.devices{
background:#ccccff;
border-radius:0 0 5px 5px;
font-family:sans-serif;
font-weight:200;
height:130px;
width:100%;
}
.sensorcluster {
display:block;
text-align: left;
margin-bottom:1px;
background-color:black
}
.sensors {
border-collapse: collapse;
width: 100%;
}
.sensors td, .sensors th {
border: 1px solid #cccccc;
background-color: #999999;
padding: 2px;
}
.weatherdata{
text-align: right;
}
.sensorname{
text-align: right;
font-weight: bold;
}
#devicename {
text-align: center;
color:white;
background-color: #000000;
font-size: 14px;
}
#sensorheader {
text-align: center;
color:white;
font-size: 14px;
display:none;
}
</style>
<body>
<div class="widget">
<div id='devicename'>Your Device</div>
<div class="devices" id="devices">
</div>
<div id='sensorheader'>Sensors</div>
<table class="sensors">
<tbody id="sensors"></tbody>
</table>
</div>
<script>
setInterval(showPinValues, 7000);
function updateWeatherDisplay() {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let txt = this.responseText;
let firstLine = txt.split("|")[0];
//console.log(firstLine);
let weatherLines = firstLine.split("!");
let sensorsDiv = document.getElementById("sensors");
let firstSensorDone = false;
let sensorCursor = 0;
for(weatherLine of weatherLines){
if(weatherLine.indexOf("*") > -1) {
console.log(weatherLine);
let weatherData = weatherLine.split("*");
//temperatureValue*pressureValue*humidityValue*gasValue*sensorType*deviceFeatureId"*sensorName; //using delimited data instead of JSON to keep things simple
let temperature = weatherData[0];
let pressure = weatherData[1];
let humidity = weatherData[2];
let sensorName = weatherData[6];
let potentialWeatherDisplay = "";
let parentDiv = "";
let weHadData = false;
parentDiv += "<tr id='sensor" + sensorCursor + "'>";
if(firstSensorDone) {
potentialWeatherDisplay += "<td class='sensorname'>" + sensorName + "</td>";
} else {
potentialWeatherDisplay += "<td></td>";
}
if(temperature != "NULL" && !isNaN(temperature) && temperature != "NaN" && temperature != "") {
potentialWeatherDisplay += "<td class='weatherdata'>" + (parseFloat(temperature) * 1.8 + 32).toFixed(2) + "° F" + "</td>";
//document.getElementById("temperature").innerHTML = (parseFloat(temperature) * 1.8 + 32).toFixed(2) + "° F";
weHadData = true;
}
if(pressure != "NULL" && !isNaN(pressure) && pressure != "NaN" && pressure != "") {
potentialWeatherDisplay += "<td class='weatherdata'>" + parseFloat(pressure).toFixed(2) + "mm Hg" + "</td>";
//document.getElementById("pressure").innerHTML = parseFloat(pressure).toFixed(2) + "mm Hg";
weHadData = true;
}
if(humidity != "NULL" && !isNaN(humidity) && humidity != "NaN" && humidity != "") {
potentialWeatherDisplay += "<td class='weatherdata'>" + parseFloat(humidity).toFixed(2) + "% rel" + "</td>";
//document.getElementById("humidity").innerHTML = parseFloat(humidity).toFixed(2) + "% rel";
weHadData = true;
}
parentDiv += "</tr>";
if(weHadData) {
document.getElementById('sensorheader').style.display = 'block';
let particularSensorDiv = document.getElementById('sensor' + sensorCursor);
if(!particularSensorDiv) {
sensorsDiv.innerHTML += parentDiv;
}
particularSensorDiv = document.getElementById('sensor' + sensorCursor);
particularSensorDiv.innerHTML = potentialWeatherDisplay;
}
}
firstSensorDone = true;
sensorCursor++;
}
}
};
xhttp.open("GET", "/weatherdata", true);
xhttp.send();
}
function updateLocalValues(id, value) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
}
console.log(value);
let onValue = "0";
if(value) {
onValue = "1";
}
xhttp.open("GET", "writeLocalData?id=" + id + "&on=" + onValue, true);
xhttp.send();
}
function showPinValues(){
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let txt = this.responseText;
let arr = JSON.parse(txt);
let deviceName = arr.device;
let pins = arr.pins;
if(pins.length> 0) {
document.getElementById("devices").innerHTML = "";
}
document.getElementById("devicename").innerHTML = deviceName;
let pinCursor = 0;
//console.log(arr);
for(let obj of pins) {
//console.log(obj);
let checked = "";
let i2cString = "";
let id = obj["id"];
let pin = id;
let friendlyName = obj["name"];
if(id.indexOf(".")>-1) {
let pinInfo = id.split(".");
let i2c = pinInfo[0];
pin = pinInfo[1];
i2cString = " via I2C: " + i2c;
}
if(obj["value"] == '1') {
checked = "checked";
}
document.getElementById("devices").innerHTML += " <input onchange='updateLocalValues(this.name, this.checked)' " + checked + " type='checkbox' name=\"" + id + "\" value='1' > <b>" + friendlyName + "</b>, pin " + pin + " " + i2cString + "<br/>";
pinCursor++;
}
}
};
xhttp.open("GET", "readLocalData", true); //Handle readADC server on ESP8266
xhttp.send();
setTimeout(function(){
updateWeatherDisplay();
}, Math.random()* 10000); //i make this a little non-deterministic so the DHT sensors won't get stuck in a pattern where they are read to frequently
}
</script>
</body>
</html>
)=====";