Skip to content

Instantly share code, notes, and snippets.

@dyadica
Last active March 8, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyadica/b8a85a272103fd32baed7f07c6f13770 to your computer and use it in GitHub Desktop.
Save dyadica/b8a85a272103fd32baed7f07c6f13770 to your computer and use it in GitHub Desktop.
Particle.variable() example using an ADXL335
Particle.variable() example using an ADXL335
<!DOCTYPE HTML>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<body>
<span id="accX"></span><br>
<span id="accY"></span><br>
<span id="accZ"></span><br>
<button id="connectbutton" onclick="start()">Read Accelerometer</button>
<script type="text/javascript">
function start(objButton) {
document.getElementById("accX").innerHTML = "Waiting for data...";
document.getElementById("accY").innerHTML = "Waiting for data...";
document.getElementById("accZ").innerHTML = "Waiting for data...";
var deviceID = "";
var accessToken = "";
var xName = "AccX";
var yName = "AccY";
var zName = "AccZ";
requestURL_X = "https://api.spark.io/v1/devices/" + deviceID + "/" + xName + "/?access_token=" + accessToken;
requestURL_Y = "https://api.spark.io/v1/devices/" + deviceID + "/" + yName + "/?access_token=" + accessToken;
requestURL_Z = "https://api.spark.io/v1/devices/" + deviceID + "/" + zName + "/?access_token=" + accessToken;
$.getJSON(requestURL_X, function(json) {
document.getElementById("accX").innerHTML = json.result + "&deg;";
document.getElementById("accX").style.fontSize = "28px";
});
$.getJSON(requestURL_Y, function(json) {
document.getElementById("accY").innerHTML = json.result + "&deg;";
document.getElementById("accY").style.fontSize = "28px";
});
$.getJSON(requestURL_Z, function(json) {
document.getElementById("accZ").innerHTML = json.result + "&deg;";
document.getElementById("accZ").style.fontSize = "28px";
});
}
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<body>
<span id="accX"></span><br>
<span id="accY"></span><br>
<span id="accZ"></span><br>
<script type="text/javascript">
window.setInterval(function()
{
var deviceID = "";
var accessToken = "";
var xName = "AccX";
var yName = "AccY";
var zName = "AccZ";
requestURL_X = "https://api.spark.io/v1/devices/" + deviceID + "/" + xName + "/?access_token=" + accessToken;
requestURL_Y = "https://api.spark.io/v1/devices/" + deviceID + "/" + yName + "/?access_token=" + accessToken;
requestURL_Z = "https://api.spark.io/v1/devices/" + deviceID + "/" + zName + "/?access_token=" + accessToken;
$.getJSON(requestURL_X, function(json) {
document.getElementById("accX").innerHTML = json.result + "&deg;";
document.getElementById("accX").style.fontSize = "28px";
});
$.getJSON(requestURL_Y, function(json) {
document.getElementById("accY").innerHTML = json.result + "&deg;";
document.getElementById("accY").style.fontSize = "28px";
});
$.getJSON(requestURL_Z, function(json) {
document.getElementById("accZ").innerHTML = json.result + "&deg;";
document.getElementById("accZ").style.fontSize = "28px";
});
}, 100);
</script>
</body>
</html>
// Initialise the accelerometer
// properties
// these are then avaliable via:
// https://api.particle.io/v1/devices/your_device_id/AccY?access_token=your_access_token
int accX = 0;
int accY = 0;
int accZ = 0;
// The scale of photon read
int minVal = 0;
int maxVal = 4095;
int led = D7;
// Initialise the program
void setup()
{
// Initialise the variables.
Particle.variable("AccX", accX);
Particle.variable("AccY", accY);
Particle.variable("AccZ", accZ);
// Initialise the pin modes.
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
// The main loop
void loop()
{
// Update each of the variables
// by performing a read of pin.
int x = analogRead(A0);
int y = analogRead(A1);
int z = analogRead(A2);
//convert read values to degrees -90 to 90
// Needed for atan2
int xAng = map(x, minVal, maxVal, -90, 90);
int yAng = map(y, minVal, maxVal, -90, 90);
int zAng = map(z, minVal, maxVal, -90, 90);
// Caculate 360deg values like so: atan2(-yAng, -zAng)
// atan2 outputs the value of -π to π (radians)
// We are then converting the radians to degrees
accX = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
accY = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
accZ = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
// Pause to limit volume of sent
// data
delay(100);
}
Particle Variables Example Using an ADXL335
You can see a JSON output of your Particle.variable() calls by going to:
https://api.particle.io/v1/devices/your-device-ID-goes-here/your-variable-name-goes-here?access_token=your-access-token-goes-here
Be sure to replace your-device-ID-goes-here with your actual device ID, your-variable-name-goes-here with your actual variable name and your-access-token-goes-here with your actual access token.
For this code example your-variable-name-goes-here can be replaced with the following values:
AccX - For the Accelerometer X value
AccY - For the Accelerometer Y value
AccZ - For the Accelerometer Z value
AccA - For all of the Accelerometer values
To view variables via controlled button-press load ADXL335_HTML_1.html into your browser.
To view variables that automatically update load ADXL335_HTML_2.html into your browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment