Don't get all data on my webpage

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

I don't quite understand this one.
What exactly do you get and what your Photon?

But one thing always holds true:
If you use a switch (or other device) that leaves your pin open (aka floating) you need to attach pull resistors which pull the pin to the opposite level as your switch closes to.
So I gather your switch closes to HIGH, hence use pinMode(vibration, INPUT_PULLDOWN).

thanks for the fast reply

it’s a simple sensor, so whenever the pins go down, it’s registrer a value.

i will try out the INPUT_PULLDOWN, but i can see the data get publish to my dashboard (https://dashboard.particle.io/user/logs).

But i have tried with php to get the data aswell, but it doesn’t get all of the data, so sometimes it gets 4/20 which is good enough.
The data shall be realtime to my webpage, so it’s important i get all of the data.

I guess the reason is that you are polling the variable asynchronous and too slow for that.
Although you have a delay(1000) in there the variable poll will only happen once per second with that code which might or might not hit the state you are looking for.

Try reducing the inteval time on the PHP side and replace your delay with this

  for(uint32_t ms = millis(); millis() - ms < 1000; Particle.process());

This will allow the cloud to access your variable thousends of times while delay() only does that once per second.

Thanks man!!!
You’re the best :slight_smile:
I got all the results now on my page and you saved my day

Have a great day

Best regards
Lasse

1 Like