Hey this is my first post, so i have no clue how to structure this.
I’m currently working on a project, the Photon needs to read if a vibration has occurred.
I get lots of data on my dashboard, I’m reading the data has a digitalRead.
This is my c++ code from my photon.
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
// digital pin 2 has a pushbutton attached to it. Give it a name:
int vibration = D2;
int buttonState = 0; //https://build.particle.io/build/5720735d68d58fbc250001ed#flash
// the setup routine runs once when you press reset:
void setup() {
Particle.variable("fly", buttonState);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(vibration, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
buttonState = digitalRead(vibration);
//Publish to Particle log
if(buttonState == 1){
String a = (String)buttonState;
Particle.publish("Input", a);
delay(1000);
}
//delay(1000); // delay in between reads for stability
}
html/javascript code.
var howManyHits = new Array;
window.setInterval(function () {
var deviceID = "<<deviceID>>";
var accessToken = "<<accesToken>>";
var varName = "fly";
requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
$.getJSON(requestURL, function (json) {
//document.getElementById("fly").innerHTML = json.result + " hit";
document.getElementById("fly").style.fontSize = "28px";
if(json.result == "1"){
howManyHits.push(json.result);
}
document.getElementById("fly").innerHTML = howManyHits + " hits";
});
}, 1000);
I don’t get the same result as my photon, sometimes I get an result but not always.
any suggestions?
Best regards
Lasse