-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
295 lines (261 loc) · 11.1 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
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
<!DOCTYPE html>
<html lang="en">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<head>
<title>Sauvie Wind Data</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
</script>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" language="javascript">
var mqtt;
var reconnectTimeout = 10000;
// var host = "test.mosquitto.org"; //MQTT Server,
// var port = 8081;
var host = "broker.hivemq.com"; //MQTT Server,
var port = 8884;
var chart;
var windData = []; // Array of wind reports, index is hour
var latestTime = new Date(0); // Time of most recent report
function onFailure(message) {
console.log("Connection Attempt to Host " + host + "Failed");
setTimeout(MQTTconnect, reconnectTimeout);
}
// Handle subscribed MQTT Message
function onMessageArrived(msg) {
var wind_data = msg.payloadString;
console.log("dest: :" + msg.destinationName + " *** " + msg.payloadString);
if (msg.destinationName.search("wind") >= 0) {
plotWindData(wind_data);
}
}
// Convert a 0-360 degree direction to a compass point and degrees from it
function directionString(direction) {
const dir_dict = {
0: "N",
1: "NE",
2: "E",
3: "SE",
4: "S",
5: "SW",
6: "W",
7: "NW"
}
var dir_num = Number(direction);
var dir_index = Math.floor(((dir_num + 22) % 360) / 45);
var rem = (dir_num - dir_index * 45);
rem = (rem > 23) ? rem - 360 : rem;
return dir_dict[dir_index] + " " + "<small>" + (rem < 0 ? ' ' : '+') + rem + "°</small>";
}
// Hack to make degree data fit into windspeed data, North is center of chart
function northCenter(degrees) {
return ((degrees + 180) % 360) / 9;
}
function timeString(time24) {
if (time24 == 0)
return "12am";
else if (time24 == 12)
return "12pm";
else if (time24 < 12)
return time24.toString() + "am";
else
return (time24 - 12).toString() + "pm"
}
// Adds a wind speed message to the windData array then redraw the chart of all data
function plotWindData(wind_data) {
try {
wind_json = JSON.parse(wind_data);
}
catch (err) {
console.log(err.message);
}
// get the time of the wind sample
var time = new Date(wind_json.time);
// console.log("timestamp: " + time.getTime() + " latest: ", Date.now());
// The windData array gets filled with the wind messages.
// The sample hour is the index to the windData array.
var hour = time.getHours();
windData[hour] = wind_json.wind;
// keep track of the most recent time read
if (time > latestTime) {
latestTime = time;
}
console.log("winddata: " + windData);
console.log("timediff:" + time.getTime() + " " + Date.now() + " " + (Date.now() - time.getTime()));
// check for corrupted data, older than two days ago
if (time.getTime() < (Date.now() - (2 * 24 * 60 * 60 * 1000))) {
for (var i = windData[hour].length - 1; i >= 0; --i) {
windData[hour][i][1] = 177;
}
}
// strip off the 0,0 "no data" values off the end.
for (var i = windData[hour].length - 1; i >= 0; --i) {
if (windData[hour][i][0] == 0 && windData[hour][i][1] == 0) {
windData[hour].pop();
}
else {
break;
}
}
var sampleTimes = [];
var speedData = [];
var dirData = [];
var gustData = [];
var lullData = [];
var hour = latestTime.getHours();
// Scan through the windData in order by time and
// create the arrays for the chart.
for (var i = 0; i < windData.length; ++i) {
// index = the oldest sample
var index = (hour + i + 1) % windData.length;
for (var j = 0; (windData[index] !== undefined && j < windData[index].length); ++j) {
sampleTimes.push(j == 0 ? timeString(index) : "");
speedData.push(windData[index][j][0]);
dirData.push(northCenter(windData[index][j][1]));
gustData.push(windData[index][j][2]);
lullData.push(windData[index][j][3]);
}
}
// var gust = document.getElementById("max-gust");
var last = windData[hour].length - 1;
var recentWind = document.getElementById("recent-wind");
recentWind.innerHTML =
"<h2><small>" + (latestTime.getMonth() + 1) + "/" + latestTime.getDate() + " " +
latestTime.getHours() + ":" + latestTime.getMinutes().toString().padStart(2, '0') + "</small><br>" +
windData[hour][last][0] + "<small>mph</small> " + directionString(windData[hour][last][1]);
// console.log("recent: " + hour + " " + last);
// console.log("data:" + windData[hour]);
chart.data.datasets[0].data = speedData;
chart.data.datasets[1].data = dirData;
chart.data.datasets[2].data = gustData;
chart.data.datasets[3].data = lullData;
chart.data.labels = sampleTimes;
chart.update();
}
// MQTT connection
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("Connected ");
mqtt.subscribe("zimbuktu/wind/#");
mqtt.subscribe("zimbuktu/recent/#");
}
function MQTTconnect() {
console.log("connecting to " + host + " " + port);
var x = Math.floor(Math.random() * 10000);
var cname = "orderform-" + x;
mqtt = new Paho.MQTT.Client(host, port, cname);
//document.write("connecting to "+ host);
var options = {
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
useSSL: true
};
mqtt.onMessageArrived = onMessageArrived
mqtt.connect(options); //connect
}
</script>
</head>
<body>
<div class="w3-container w3-indigo">
<h1>SAUVIE ISLAND WIND</h1>
</div>
<div class="w3-container w3-indigo">
<h1>The sensor is down for the season, but will be back in the spring.</h1>
</div>
<div class="w3-panel w3-pale-blue">
<div id="recent-wind"></div>
</div>
<div class="w3-panel w3-pale-blue" style="direction: rtl; overflow-x: auto; overflow-y: hidden;">
<div style="width:3000px; height: 500px">
<canvas id="myChart" height="500" width="0"></canvas>
</div>
</div>
<div class="w3-panel w3-pale-blue">
<p> The blue line is the direction. North is at the center of the chart.
Updates occur every 5 minutes from 10AM to 9PM if it is windy. Otherwise it is only updated on the hour.
The sensor will be taken down in September for improvements and will return in May.</p>
</div>
<div class="w3-panel w3-pale-blue">
<p style="font-size:150%; text-align: center;">
If you would like make a donation to help pay for the hardware and recurring costs,
please click the donate button.</p>
<form action="https://www.paypal.com/donate" method="post" target="_top">
<input type="hidden" name="business" value="25GJHM2LDXKG2" />
<input type="hidden" name="no_recurring" value="1" />
<input type="hidden" name="item_name" value="Help cover costs for the Sauvie Wind Sensor." />
<input type="hidden" name="currency_code" value="USD" />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0"
name="submit" title="PayPal - The safer, easier way to pay online!" alt="Donate with PayPal button" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
</div>
<!-- <div class="w3-panel w3-pale-blue">
<div id="devInfo"></div>
</div>
-->
<script>
MQTTconnect();
chart = new Chart("myChart", {
type: "line",
data: {
datasets: [{
label: "Wind Speed",
pointRadius: 1,
pointBackgroundColor: "rgb(0,0,255)",
borderColor: "green",
borderWidth: 4,
fill: false,
}, {
label: "Direction (North at middle)",
pointRadius: 1,
pointBackgroundColor: "rgb(0,0,255)",
borderColor: "blue",
fill: false,
borderWidth: -1
}, {
label: "Gusts",
pointRadius: 2,
pointBackgroundColor: "rgb(255,0,0)",
borderColor: "red",
fill: false,
borderWidth: -1
}, {
label: "Lulls",
pointRadius: 3,
pointBackgroundColor: "rgb(255,0,0)",
borderColor: "black",
fill: '-1',
borderWidth: -1
},
]
},
options: {
legend: { display: true },
scales: {
xAxes: [{ ticks: { min: 40, max: 290 } }],
yAxes: [{
position: 'right',
ticks: {
fontSize: 25,
min: 0,
max: 40,
}
},
{
position: 'right',
ticks: {
fontColor: 'blue',
fontSize: 25,
min: 0, max: 8, callback: function (value) {
var x = ["S", "SW", "W", "NW", "N", "NE", "E", "SE", "S"];
return x[value % x.length];
}
}
}],
}
}
});
</script>
</body>
</html>