-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
104 lines (87 loc) · 4.8 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
<!doctype html>
<html>
<head>
<title>SparkJS Library Example</title>
<script type="text/javascript" src="sparkio-client.js"></script>
</head>
<body>
<table>
<tr>
<td><label for="deviceID">Your Device ID:</label></td>
<td><input type="text" id="deviceID" style="width:300px" placeholder="Your Device ID"/></td>
</tr>
<tr>
<td><label for="deviceID">Your Access Token:</label></td>
<td><input type="text" id="accessToken" style="width:300px" placeholder="Your Access Token" c/></td>
</tr>
</table>
<div id="buttons">
<button id="buttonConnect" style="width:100px">Connect</button>
<button id="buttonGetRSSI" style="width:130px">Get WiFi Signal</button>
<button id="buttonLEDOn" style="width:100px">LED ON</button>
<button id="buttonLEDOff" style="width:100px">LED OFF</button>
</div>
<div>
<hr/>
<h4>Result</h4>
<div id="result"></div>
</div>
<script type="application/javascript">
/*global SparkAPI*/
var accessToken = "Your access token"; //Access token for your core/app
var deviceID = "Your device id"; //Device ID for your device.
var SparkDevice;
var buttonConnect = document.getElementById("buttonConnect"); //Get the object that handles the Status button in the page
buttonConnect.onclick = function() { //Define what happens on the Status button click
var inputDeviceID=document.getElementById("deviceID");
var inputAccessToken=document.getElementById("accessToken");
accessToken=inputAccessToken.value;
deviceID=inputDeviceID.value;
SparkDevice = new SparkAPI(deviceID, accessToken);
SparkDevice.getVariable("", onSparkStatus); //Get a variable with "" as the name, actually pulls status.
}; //The onSparkStaus callback will be called with the result.
var buttonGetRSSI = document.getElementById("buttonGetRSSI"); //Get the object that handles the RSSI button in the page
buttonGetRSSI.onclick = function() { //Define what happens on the RSSI button click
if(SparkDevice!==undefined) {
SparkDevice.getVariable("RSSI", onSparkRSSI); //Use SparkJS function to get RSSI from the Spark Core.
}
else {
ShowResult("Try Connecting First");
}
}; //The onSparkRSSI callback will be called with the result.
var buttonLEDOn = document.getElementById("buttonLEDOn"); //Get the object that handles the LED On button in the page
buttonLEDOn.onclick = function() { //Define what happens on the LED On button click
if(SparkDevice!==undefined) {
SparkDevice.callFunction("LED", "ON", onSparkLED); //Use the SparkJS function to call the Spark Core registered
}
else {
ShowResult("Try Connecting First");
}
}; //LED function with "ON" as the parameter string.
//The onSparkLED callback will be called with the result.
var buttonLEDOff = document.getElementById("buttonLEDOff"); //Get the object that handles the LED Off button in the page
buttonLEDOff.onclick = function() { //Define what happens on the LED Off button click
if(SparkDevice!==undefined) {
SparkDevice.callFunction("LED", "OFF", onSparkLED); //Use the SparkJS function to call the Spark Core registered
}
else {
ShowResult("Try Connecting First");
}
}; //LED function with "OFF" as the parameter string.
//The onSparkLED callback will be called with the result.
function onSparkStatus(result) { //Callback function for the status button
ShowResult("Get Status Response:" + JSON.stringify(result)); //Just dump the result object to the console. Use the JS console
} //to view result. Could do a lot of stuff but keeping it simple now.
function onSparkRSSI(result) { //Callback function for the Get RSSI button
ShowResult("Get RSSI Response:" + JSON.stringify(result)); //Just dump the result object to the console. Use the JS console
} //to view result. Could do a lot of stuff but keeping it simple now.
function onSparkLED(result) { //Callback function for the LED (ON/OFF) button
ShowResult("Function LED Response:" + JSON.stringify(result)); //Just dump the result object to the console. Use the JS consol
} //to view result. Could do a lot of stuff but keeping it simple now.
function ShowResult(resultMessage){
var responceElement=document.getElementById("result");
responceElement.innerHTML=resultMessage;
}
</script>
</body>
</html>