forked from bseybold/IoT-particle-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_website_get_variable.html
59 lines (55 loc) · 1.8 KB
/
example_website_get_variable.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
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<title>IoT Web Portal</title>
<script>
window.onload=function() {
document.getElementById('my-form').onsubmit=function() {
all_in_one = document.getElementsByName("all_in_one")[0].value;
if (all_in_one) {
pieces = all_in_one.split(",");
device_id = pieces[0];
access_key = pieces[1];
variable_name = pieces[2];
} else {
device_id = document.getElementsByName("device_id")[0].value;
access_key = document.getElementsByName("access_key")[0].value;
variable_name = document.getElementsByName("variable_name")[0].value;
}
request = 'https://api.particle.io/v1/devices/'+device_id+'/'+variable_name+'?access_token='+access_key;
console.log(request);
(async function () {
try {
const myResponse = await fetch(request);
myOutput = await myResponse.json();
output_name = myOutput['name'];
output_result = myOutput['result'];
document.getElementById('result').innerHTML = output_name + ' = ' + output_result;
console.log(JSON.stringify(myOutput));
} catch(error) {
console.log('Error ' +error);
}
})() // end of inline async function
// You must return false to prevent the default form behavior
return false;
}
}
</script>
</head>
<body>
<h1>Welcome!</h1>
<form id='my-form'>
Device ID: <input type="text" name="device_id"><br>
Access Key: <input type="text" name="access_key"><br>
Variable Name: <input type="text" name="variable_name"><br>
-- OR --<br>
All in one (id,key,variable): <input type="text" name="all_in_one"><br>
<input type="submit" value="Submit">
</form>
<br>
<br>
<div id="result"></div>
</body>
</html>