Author Topic: HELP PIR sensor and oak  (Read 21165 times)

gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #15 on: April 27, 2016, 01:29:12 am »
Okay. I have not really had time to look at it before now.

I keep getting this error when i try to verify the project?

Quote
sketch_apr27b:31: error: expected unqualified-id before 'else'
expected unqualified-id before 'if'

My current code:

// Simple Oak PIR example
#define PIR_Out 5  // On Oak pin 5
#define Oak_LED 1  // Onboard LED pin

void setup()
{
  pinMode(PIR_Out, INPUT);
  pinMode(Oak_LED, OUTPUT);
  digitalWrite(Oak_LED, LOW); // Turn Off onboard LED
//  Serial.begin(9600);
//  Serial.println("Warming up...");
  delay(10000); // Ten seconds
}

void loop()
{
  if (digitalRead(PIR_Out) == HIGH) {
    digitalWrite(Oak_LED, HIGH);
  }
  else {
    digitalWrite(Oak_LED, LOW);
  }
  delay(200); // Adjust delay as needed
}

  if (digitalRead(PIR_Out) == HIGH) {
    digitalWrite(Oak_LED, HIGH);
   //is the sensor output high when there is motion? if not, move the publish line to the 'else' block
   Particle.publish("motion-detected");
  }
  else {
    digitalWrite(Oak_LED, LOW);
  }

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #16 on: April 27, 2016, 09:22:00 am »
Look at your code again. You have the if / else code that does the Particle.publish() outside the loop. Did you mean to replace the code inside the loop with this?

gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #17 on: April 27, 2016, 10:11:58 am »
I dont really understand what your question is ?  :D

gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #18 on: April 27, 2016, 10:15:34 am »
Ahh okay. It has to be like this

// Simple Oak PIR example
#define PIR_Out 5  // On Oak pin 5
#define Oak_LED 1  // Onboard LED pin

void setup()
{
  pinMode(PIR_Out, INPUT);
  pinMode(Oak_LED, OUTPUT);
  digitalWrite(Oak_LED, LOW); // Turn Off onboard LED
//  Serial.begin(9600);
//  Serial.println("Warming up...");
  delay(10000); // Ten seconds
}

void loop()
{
  if (digitalRead(PIR_Out) == HIGH) {
    digitalWrite(Oak_LED, HIGH);
  }
  else {
    digitalWrite(Oak_LED, LOW);
  }
  delay(200); // Adjust delay as needed
  if (digitalRead(PIR_Out) == HIGH) {
    digitalWrite(Oak_LED, HIGH);
   //is the sensor output high when there is motion? if not, move the publish line to the 'else' block
   Particle.publish("motion-detected");
  }
  else {
    digitalWrite(Oak_LED, LOW);
}
 }
 


Now i have to find out how i use the IFTTT app to make it work :)

gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #19 on: April 27, 2016, 03:18:05 pm »
For some reason I cant compile and verify the code i posted in the last message on the particle.io build. ?


exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #20 on: April 27, 2016, 08:23:10 pm »
@gregerwhut, 
If you are trying to compile using Particle Build, I don't believe that this is supported yet for the Oak. You need to continue to use the Arduino IDE to compile and upload to the Oak.

Here is an example of using the PIR state info to publish to Particle. Verify/Upload using the Arduino IDE. I assume you have a Particle account with your Oak claimed there.

Code: [Select]
// Simple Oak PIR example with controlled Particle.publish() updates
#define PIR_Out 5  // PIR input on Oak pin 5
#define Oak_LED 1  // Onboard LED pin

#define MOTION 1
#define NO_MOTION 0
int PIRlast; // Used to keep motion state

void setup()
{
  pinMode(PIR_Out, INPUT);
  pinMode(Oak_LED, OUTPUT);
  digitalWrite(Oak_LED, LOW); // Turn Off onboard LED
  PIRlast = NO_MOTION;
// Allow time for PIR "warmup"
  delay(10000); // Ten seconds
}

void loop()
{
  if (digitalRead(PIR_Out) == HIGH) {
    digitalWrite(Oak_LED, HIGH); // Visual for motion detected
    // If the sensor output is high there is / was motion
    // To avoid flooding Particle.io with Motion updates, only publish when motion is initially detected
    if (PIRlast == NO_MOTION) {
      Particle.publish("MOTION SENSOR", "Motion detected");
      PIRlast = MOTION;
    }
  }
  else {
   // If the sensor output is low there is no motion
   // To avoid flooding Particle.io with Motion updates, only publish when no motion is initially detected
    digitalWrite(Oak_LED, LOW); // Turn of onboard LED
    if (PIRlast == MOTION) {
      Particle.publish("MOTION SENSOR", "No Motion");
      PIRlast = NO_MOTION;
    }
  }
  // Delay here to read PIR state at 1 second intervals
  delay(1000);
}

This code will publish to Particle the state of the PIR. That is, whether motion was detected or not. It is somewhat throttled so that when in a motion detected state you don't flood particle with updates. It will only publish to Particle one message each time motion is initially detected.  That is, if the motion is detected and continuous, you will only get one message to Particle. The PIR will have to return to a no motion detected state before another motion detected message can be sent.



gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #21 on: April 29, 2016, 04:04:11 am »
Okay. I see I will use adruino IDE.

When I got to Particle Dashboard I can see when the motion is detected or not. But it does go on and off.. even thouh the should be no motion

Is it correctly setup with the IfTTT?


exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #22 on: April 29, 2016, 08:20:33 am »
@gregerwhut,
Not sure why your PIR is going on and off without motion. Are you absolutely sure there is nothing moving within the range of the PIR (perhaps you)? I've had mine go off when sitting off to the side any making even the slightest motion. Don't know if this is like the one you have but there are various settings that can be changed on the HC-SR501.
See HC-SR501 Datasheet here: https://www.mpja.com/download/31227sc.pdf

I don't have that model PIR and mine has no settings to play with, so I can't debug your PIR's behavior. I did notice in the datasheet "Instructions for Use" that the HC-SR501 takes a minute to initialize and warm up. Don't know if this has anything to do with you observation but perhaps you should change the warm up delay to 1 minute. That is, delay(60000). You are going to have to debug this on your own. I thought you had this working before?

With respect to IFTTT, I don't have any experience using it but looking at what you posted, I would think you need to match the actual event and data strings literally.
So the event name is "MOTION SENSOR" and the data for motion detected is "Motion detected". Again, I haven't used IFTTT.  Good luck.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #23 on: April 29, 2016, 08:34:53 am »
Looked at the datasheet again and perhaps this is a clue to the repeated motion detects...
From the datasheet:
Quote
Triggered in two ways: (jumper selectable) ◦ non-repeatable trigger: the sensor output high, the delay time is over, the output is automatically changed from high level to low level; ◦ repeatable trigger: the sensor output high, the delay period, if there is human activity in its sensing range, the output will always remain high until the people left after the delay will be high level goes low (sensor module detects a time delay period will be automatically extended every human activity, and the starting point for the delay time to the last event of the time).

Try the "repeatable trigger" setting on the PIR if you have the ability to change it. Also look at the delay setting for your PIR.
« Last Edit: April 29, 2016, 10:05:15 am by exeng »

gregerwhut

  • Newbie
  • *
  • Posts: 13
Re: HELP PIR sensor and oak
« Reply #24 on: April 29, 2016, 01:24:44 pm »
I have the HC-SR-501 yes.

I had it working once yes, but cant actaully remeber if it was on the Adruino Uno that I have It probally was. But it should work on the oak. . I dont know if I have burned it off or something like that?

The delay setting is factory --> to the left.

I can see how to change the setting to make it a "repeatable trigger". There aren't any buttons like the delay and sensitivity adjusters.





exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #25 on: April 29, 2016, 02:54:49 pm »
It's obviously working because you are getting motion detected notifications to Particle. I would expect there to be a jumper on the board to select repeatable and non-repeatable settings.  The documentation seems rather poor in this area and I don't have that model to play with and understand what may be going on.

The way mine works is when motion is detected the output pin remains HIGH as long as there is motion and for about 3 additional seconds after motion stops. It has an internal LED motion indicator so it's easy to see what state it's in.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: HELP PIR sensor and oak
« Reply #26 on: April 29, 2016, 05:37:43 pm »
@gregerwhut,
Been looking at the specs for the PIR you say you have. Looks like repeat trigger is the set default.
Quote
Trigger: L: unrepeatable trigger, H: Repeat  Trigger (default)

I believe that is the preferred setting, so I would try to increase the delay setting to see if this improves the behavior. Again, I don't have this PIR so you'll have to do the experimenting.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: HELP PIR sensor and oak
« Reply #27 on: May 01, 2016, 12:17:49 am »
Also have a look at the datasheet that exeng linked to earlier, as it shows the back of that sensor module, and where the jumper for the single or repeatable trigger.  It is possible your motion sensor is triggering multiple times as PIR (passive infrared) sensors work on heat movement... so if you have heat moving through the space, they can false trigger. PIR sensors also need 30-60 seconds after powering on to try and self-calibrate to the ambient environment, in order to try and prevent those false triggers.

For the IFTTT configuration, exeng is also correct - it needs to be the same as you are seeing on the particle dashboard. Otherwise IFTTT will have no idea what you are telling it to do. So, from your screenshot of your log, your event name (if) should be "MOTION SENSOR" (minus the double quotes) and the event content (is) should be "Motion detected" (again, minus the double quotes).