int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 35000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = D6; //the digital pin connected to the PIR sensor's output
int ledPin = D7;
int testLed = D5;
/////////////////////////////
//SETUP
void setup(){
//Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(testLed,OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
//Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
//Serial.print(".");
delay(1000);
}
//Serial.println(" done");
//Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//delay(500);
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
//Serial.println("---");
//Serial.print("motion detected at ");
//Serial.print(millis()/1000);
// Serial.println(" sec");
//delay(50);
Spark.publish("occupied");
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW);
//digitalWrite(testLed, LOW );
//the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
//Serial.print("motion ended at "); //output
//Serial.print((millis() - pause)/1000);
//Serial.println(" sec");
//delay(50);
digitalWrite(testLed, HIGH);
Spark.publish("unoccupied"); // Unoccupied
}
}
}
Does it publish occupied
and unoccupied
in one go, or what do you mean with “publishes twice”
BTW there is no debounce (with some combinations of your flags) between the HIGH and the LOW branch for pirPin
since you do read twice, you might get HIGH and LOW in the same interation of loop()
.
Rather use if(digitalRead(pirPin)) { highBranch } else { lowBranch }
No. I would get 2 notifications instantly for each "occupied and unoccupied in two different states.
I’ll have to try that on my devices then.
At the moment I’ve just tested with a button pulling high instead of a PIR sensor, but I can’t reproduce your multi-publish
(basically your own code, just minor tweaks and uncommented the Serial.debug() statements
// https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/20
int calibrationTime = 2; // was 30
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; // was 35000
boolean lockLow = true;
boolean takeLowTime;
int pirPin = D6; //the digital pin connected to the PIR sensor's output
int ledPin = D7;
int testLed = D5;
void setup()
{
Serial.begin(115200);
pinMode(pirPin, INPUT_PULLDOWN);
pinMode(ledPin, OUTPUT);
pinMode(testLed,OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop()
{
if(digitalRead(pirPin))
{
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//delay(500);
if(lockLow)
{
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
//delay(50);
Particle.publish("occupied");
}
takeLowTime = true;
}
else
{
digitalWrite(ledPin, LOW);
//digitalWrite(testLed, LOW );
//the led visualizes the sensors output pin state
if(takeLowTime)
{
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause)
{
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
//delay(50);
digitalWrite(testLed, HIGH);
Particle.publish("unoccupied"); // Unoccupied
}
}
}