Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harrisonhjones/bc983aec2e7010046ab9f383cf08c807 to your computer and use it in GitHub Desktop.
Save harrisonhjones/bc983aec2e7010046ab9f383cf08c807 to your computer and use it in GitHub Desktop.
<!DOCTYPE>
<html>
<body>
<center>
<h1>Call Function</h1>
Control the LED --->
<button onclick="callFunction('on');" value="Do it!">Turn the LED on.</button>
<button onclick="callFunction('off');" value="Do it!">Turn the LED off.</button>
<h1>Get Variable</h1>
Variable Value: <span id="varValue">Not Yet Loaded. Click this button ---> </span><button onclick="getVariable()"/>Update</button>
<h1>Logout</h1>
Click this button to logout ---> <button onclick="logout()">Logout</button>
<h1>Log</h1>
<div id="log" style="border: 1px black solid; width: 50%"> </div>
</center>
<!--- We are using JQuery for some function below -->
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
// If the current user has not set their device ID prompt them for it.
if(!localStorage.getItem("deviceID"))
{
localStorage.deviceID = prompt("Please enter your device ID", "");
}
if(!localStorage.deviceID)
alert("You did not enter a valid device ID. Shame!")
// If the current user has not set their access token prompt them for it.
if(!localStorage.getItem("accessToken"))
{
localStorage.accessToken = prompt("Please enter your Access Token", "");
}
if(!localStorage.accessToken)
alert("You did not enter a valid Access Token. Shame!")
// A little function for calling a Particle function on a device. Does not notify the user if there was an error
function callFunction(arg)
{
var data = {};
data.arg = arg;
$.post( "https://api.particle.io/v1/devices/" + localStorage.deviceID + "/led?access_token=" + localStorage.accessToken, data, function( data ) {
log("Function 'led' called with arg '" + arg + "'");
});
}
// A little function for getting a Particle variable on a device. Does not notify the user if there was an error
function getVariable()
{
$.get( "https://api.particle.io/v1/devices/" + localStorage.deviceID + "/analogvalue?access_token=" + localStorage.accessToken, function( data ) {
console.log(data);
$( "#varValue" ).html( data.result );
log("Variable 'analogvalue' retrieved. Value = '" + data.result + "'");
});
}
// A simple logout function. Unsets the user's deviceID and accessToken stored in local storage
function logout()
{
localStorage.removeItem("deviceID");
localStorage.removeItem("accessToken");
log("User logged out!");
}
// A very simple message logger function
function log(message)
{
$( "#log" ).html(new Date() + " - " + message + "<br/>" + $( "#log" ).html())
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment