Particle Photon PIR help

Hey yall!

So a friend and I are working on a project for an instrumentation class and had to set up a very simple PIR sensor. We currently have it writing to Thingspeak for a usable graph. Our sensor is constantly sending data about every 30 seconds though and its not very useful. Help please! We’re well above our heads on this. Here is our code, it’s honestly mostly copied and pasted from things we found online because we’re so clueless when it comes to coding. We also eventually need to get the event to trigger a light on another photon but we’ll worry about that once this is working.

int inputPin = D0;             
int ledPin = D1;               
int pirState = LOW;          
int val = 0;                    

int calibrateTime = 5000;      

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);     
}

void loop() {

  // if the sensor is calibrated
  if (calibrated()) {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
    }
}

void readTheSensor() {
    val = digitalRead(inputPin);
}

bool calibrated() {
    return millis() - calibrateTime > 0;
}

void setLED(int state) {
    digitalWrite(ledPin, state);
}

void reportTheData() {

    if (val == HIGH) {
        // the current state is no motion
        // i.e. it's just changed
        // announce this change by publishing an event
        if (pirState == LOW) {
          // we have just turned on
          Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
          // Update the current state
          pirState = HIGH;
          setLED(pirState);
        }
    } else {
        if (pirState == HIGH) {
          // we have just turned of
          // Update the current state
          Particle.publish("PhotonMotion", "Off", PRIVATE);
          pirState = LOW;
          setLED(pirState);
        }
    }
   
           
        String data = String(10);
      
         Particle.publish("thingSpeakWrite_", data, PRIVATE);
         
         delay(6000);
         
         #define publish_delay 6000
            unsigned int lastPublish = 0;

            
        
                unsigned long now = millis();
                    if ((now - lastPublish) < publish_delay) {
            return;
            }

                int value = analogRead(D0);
                Particle.publish("thingSpeakWrite_D0", "{ \"1\": \"" + String(value) + "\", \"k\": \"HS87S50T6LUAGQDS\" }", 60, PRIVATE);
                lastPublish = now;
            
    
}
https://build.particle.io/build/58e1305ea6cb8f44a20013cf

@ScruffR I would really appreciate your help

we also keep getting these errors in our logs:

If that works, then what exactly do you need help with?

The errors you’re getting do tell you what’s wrong, so mind the documented rate limits.

Well it’s sending changing data when nothing in its environment is changing. and we can’t get clean enough data to trigger the other photon like we need. We have no consistent event happening to build off of.

PIR Sources:
http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir

One thing you need to prevent retriggers of the current state is to have a second variable that holds the previous state and only when the current state is not equal to the previous state you want to do something.

like

int pirState = LOW;
int pirLastState = !pirState;
...

  if (pirState != pirLastState) {
    pirLastState = pirState;
    // do whatever needed on change of state
  }
1 Like

I was recently using a PIR sensor with a Photon to send the alerts to Ubidots.

It worked but I found the PIR sensor to be way too sensitive to be used anywhere but an inside space. Too many things to trigger it outside.

Here is my code if it’s helpful. You just need to setup a Ubidots account and add your user Token to get it working for you.

// This example is to save values with a setted data source name

#include "Ubidots/Ubidots.h"


#define TOKEN "Put your Token Here"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_NAME "PIR_Sensor_Test"

Ubidots ubidots(TOKEN);

bool value1 = LOW;
int ledPin = D7;  

void setup() {  
    Serial.begin(115200);
    ubidots.setDatasourceName(DATA_SOURCE_NAME);
    
     pinMode(D0, INPUT_PULLDOWN); //Digital Input for PIR Sensor
     pinMode(ledPin, OUTPUT);          // Sets pin as output
}
void loop() {
    
    bool value1 = LOW;
    
    if (digitalRead(D0) == HIGH) {
        digitalWrite(ledPin, HIGH);   // Sets the LED on
        value1 = HIGH;
        ubidots.add("PIR Sensor 1", value1);  // Change for your variable name
        ubidots.sendAll();
        //Particle.publish("office-motion", "OFFICE", 60, PRIVATE); //If you want to send a Webhook then you would use this line. 
        delay(60000);
    
        value1 = LOW;
        ubidots.add("PIR Sensor 1", value1);  // Change for your variable name
        ubidots.sendAll();
        digitalWrite(ledPin, LOW);    // Sets the LED off
        
        while (digitalRead(D0) == HIGH); // hang tight here until motion stops
        
    }
   
}