-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaScript.js
58 lines (51 loc) · 1.76 KB
/
JavaScript.js
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
<script type="text/javascript">
// set your channel id here
var channel_id = '[ThingSpeak Channel ID]';
// set your channel's read api key here if necessary
var api_key = '[ThingSpeak Read API Key]';
// variable for the data point
var p_temp,
p_humi,
p_lux,
p_smoist,
p_ssalinity,
p_battvolt,
p_timestamp,
timestamp;
function initPage() {
loadData();
setInterval(loadData, 15000);
}
function loadData() {
p_temp = 0;
p_humi = 0;
p_lux = 0;
p_smoist = 0;
p_ssalinity = 0;
p_battvolt = 0;
p_timestamp = 0;
// get the data from thingspeak
$.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/feed/last.json?results=1&api_key=' + api_key, function(data) {
// get the data points
p_temp = parseFloat(data.field1);
p_humi = parseFloat(data.field2);
p_lux = parseFloat(data.field3);
p_smoist = parseFloat(data.field4);
p_ssalinity = parseFloat(data.field5);
p_battvolt = parseFloat(data.field6);
p_timestamp = new Date(data.created_at);
timestamp = p_timestamp.toLocaleString();
outputData();
});
}
function outputData() {
// update page
document.getElementById('temperature').innerHTML = p_temp + ' °C';
document.getElementById('humidity').innerHTML = p_humi + ' %';
document.getElementById('lx').innerHTML = p_lux + ' ㏓';
document.getElementById('smoisture').innerHTML = p_smoist + ' %';
document.getElementById('ssalinity').innerHTML = p_ssalinity + ' %';
document.getElementById('battvolt').innerHTML = (p_battvolt-3250).toFixed(2) + ' ㎷ / ' + (4200-3250) + ' ㎷ (' + (100*(p_battvolt-3250)/(4200-3250)).toFixed(0) + ' %)';
document.getElementById('timestamp').innerHTML = '<i class="material-symbols-rounded" style="font-size:1.17em";">potted_plant</i>' + " " + timestamp;
}
</script>