-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHumidity_Temperature-Google_Charts.html
96 lines (88 loc) · 2.88 KB
/
Humidity_Temperature-Google_Charts.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Humidity and Temperature - Google Charts</title>
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>
<script src="https://blockly.webduino.io/lib/runtime.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<style>
#show {
font-size: 30px;
line-height: 40px;
}
#show b {
display: inline-block;
font-size: 14px;
vertical-align: top;
margin-top: -10px;
}
</style>
</head>
<body>
<div id="show">
<h2>溫度:<span id="temperature">0</span> ℃</h2>
<h2>濕度:<span id="humidity">0</span> %</h2>
<div id="chart_div" style="width: 100%; height: 400px;"></div>
</div>
<script>
var dht;
var chart_div = document.getElementById("chart_div");
boardReady({device: 'wa8w'}, board => {
board.systemReset();
board.samplingInterval = 20;
dht = getDht(board, 11);
var areachart = {
areachart: false,
origin: [["時間", "溫度 (℃)", "濕度 (%)"]]
};
google.load("visualization", "1", {
packages: ["corechart"],
callback: () => areachart.areachart = true
});
dht.read(evt => {
document.getElementById("temperature").innerHTML = dht.temperature;
document.getElementById("humidity").innerHTML = dht.humidity;
var time = new Date();
var ts = time.getSeconds();
var tm = time.getMinutes();
var th = time.getHours();
var a = [];
if (areachart.areachart) {
chart_div.style.display = "block";
a[0] = th + ":" + tm + ":" + ts;
a[1] = dht.temperature;
a[2] = dht.humidity;
areachart.origin.push(a);
drawAreaChart(areachart.origin);
}
if (areachart.gauge) {
chart_div.style.display = "none";
areachart.origin1 = [["Label", "Value"], ["humidity", humidity]];
areachart.origin2 = [["Label", "Value"], ["temperature", temperature]];
drawGuage(areachart.origin1, areachart.origin2);
}
}, 1000);
});
function drawAreaChart(d) {
if (!Array.isArray(d)) return;
var titleTextStyle = {
fontName: "微軟正黑體",
bold: true,
italic: false
};
var data = google.visualization.arrayToDataTable(d);
var options = {
title: "",
hAxis: {title: "時間", titleTextStyle: titleTextStyle},
vAxis: {title: "溫濕度", minValue: 0, titleTextStyle: titleTextStyle},
chartArea: {top: 50, left: 50, width: "70%", height: "70%"},
colors: ['#f00', '#00f']
};
var code = new google.visualization.AreaChart(chart_div);
return code.draw(data, options);
}
</script>
</body>