Digistump Forums
The Oak by Digistump => Oak Projects => Topic started by: gregerwhut on April 11, 2016, 07:27:13 am
-
hey Folks
Anyone that could help me making connection between a PIR sensor and the oak, which can send a message/notification to the phone when it detects movement / light interfernce.
I am new to all kind off coding, and have been searching the web for my exact project, but cant find any help.
Anders
-
I think you are going to have to break it down to it's component parts and do some research...
1. Don't know what PIR sensor you are using but most likely it is powered by 5V or more. The Oak is a 3.3v device so you'll need to look into how to drop the output voltage from the PIR output signal to 3.3v for whatever input pin you use on the Oak (Just to be safe). Search for circuits built around the PIR. Since you'll most likely want to pull-down the PIR output, I guess you could create a voltage divider by placing a resistor ahead of the Oak corresponding input pin. But note that there are Oak pins that should not be pulled down at boot.
2. You should be able to easily find a working example of code for the PIR in the Arduino Playground or elsewhere on the web. You'll need to adapt that to the available Oak pins. Also, Oak has no USB serial out through USB, but you can use the TX, RX pins with an appropriate 3.3v serial adapter to get serial output if needed for debug.
3. Notification to phone (not a strong point for me). Look at or for examples on how to get data to a cloud service from the Oak that can also notify to your phone. I'm not familiar with many of the them but they do exist. Thingspeak, I think can send Twitter messages. The Blynk app has a virtual LCD screen. Not sure what Particle offers in this realm. There may be others that I am even less familiar with. Microsoft Azure? IFTTT? Others apps?
Hope this at least gets you started in the right direction. Or have I given you just enough info to make it seem complicated? Just approach it step by step. You'll learn more in the end.
Get the basic circuit down with a simple example, then try to add the notification part.
There my be others here that can help with various parts of the puzzle.
-
1. I use I 5 v PIR sensor yes. I have a Adruino nano and Uno, but dont know if they are to any help for the project? I dont know anything about how to setup a circuit like you described with yje resirsor ahead of the oak and so on.
2- I dont understand this part off your reply at all.
3. I would be glad to use IFTTT, if it is possible.
Yeah It seems kind of complicated, when I aint familiar with any of the stuff.
-
I did a BING search "ESP8266 PIR" and this was the first link: http://www.esp8266-projects.com/2015/03/esp8266-pir-motion-sensor-detection.html (http://www.esp8266-projects.com/2015/03/esp8266-pir-motion-sensor-detection.html)
It should provide more helpful details. It seems to be using LUA - but the PIR hardware and voltage technical details and process - and most coding - should be otherwise the same - if you have a similar PIR unit.
-
Maybe this will help you get started...
Check to see if you PIR is 3.3v compatible. Some are. If so, you can use the simple fritzing example without the voltage divider.
NOTE: In the fritzing diagrams the PIR red wire is VCC, black wire is GND and yellow wire PIR OUT.
If your PIR is not 3.3v compatible, you can use the fritzing example with the voltage divider which uses 4.7K and 10K resistors to form that divider and drop the PIR output signal to about 3.4v assuming 5v at VIN. Check the band colors to see which resistor is which in the diagram. Do not exceed that 5V for VIN (i.e. Power to the Oak) as it will raise the voltage of the PIR output.
This all may not be necessary if the Oak pins can tolerate 5V but, I would rather be safe than sorry.
Finally, here is a very simple example. It will need a lot more code to handle the notifications and keeping motion states so that you are not flooded with continuous notifications when the PIR output remains HIGH because of motion. The example simple turns on the Oak onboard LED when motion is detected.
// 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
}
exeng OUT
-
UPDATE: The fritzing diagrams do not apply to the HC-SR501 which is what comes with the Oak Ultimate Kit. I don't have one (but probably should as it appears to be a nicely featured PIR). From reading the spec's it operates at 5V to 20V for VCC but outputs 3.3V when motion is detected. So it looks like you can connect the PIR output directly to an Oak input pin. You only need to supply it's VCC with 5V and share a common ground with the Oak.
-
okay I am unsure if i have to buy a new PIR sensor like this one.
http://www.aliexpress.com/item/Free-Shipping-HC-SR501-Adjust-IR-Pyroelectric-Infrared-PIR-Motion-Sensor-Motion-Human-Sensor-Detector-Module/32608657312.html?spm=2114.01010208.3.1.SJF2wr&ws_ab_test=searchweb201556_0,searchweb201602_2_10017_10005_10006_10034_10021_507_10022_508_10032_10020_10018_10019,searchweb201603_9&btsid=30ff976c-a5dc-48ec-a462-b0c7b376cbff (http://www.aliexpress.com/item/Free-Shipping-HC-SR501-Adjust-IR-Pyroelectric-Infrared-PIR-Motion-Sensor-Motion-Human-Sensor-Detector-Module/32608657312.html?spm=2114.01010208.3.1.SJF2wr&ws_ab_test=searchweb201556_0,searchweb201602_2_10017_10005_10006_10034_10021_507_10022_508_10032_10020_10018_10019,searchweb201603_9&btsid=30ff976c-a5dc-48ec-a462-b0c7b376cbff)
or if i am able to use the one i allready own
http://www.aliexpress.com/item/D19-Free-Shipping-Mini-Adjust-IR-Pyroelectric-Infrared-PIR-Motion-Human-Sensor-Detector-Module/1978936711.html (http://www.aliexpress.com/item/D19-Free-Shipping-Mini-Adjust-IR-Pyroelectric-Infrared-PIR-Motion-Human-Sensor-Detector-Module/1978936711.html).?
I do not have any resistors
-
gregerwhut,
The one you have looks suspiciously like the HC-SR501. Looking at the specs, it can be power by 5V and outputs 3.3V. So you should be able to use the fritzing diagram attached to this post and be able to get the example code to run.
-
Okay Super, I got that to work :). Now i need help to make a code that can be setup to send a notification, when the PIR detects movement ?? :)
-
Glad to hear that it works. I'm afraid you'll have to appeal to others or do some learning on your own. As I said in a previous post, I'm not that knowledgeable with respect to how to implement notifications to the phone. I'm only able to offer suggestions, as I did.
-
Adding DrJFM relpy here as most of the discussion has already occurred under "Oak Projects". http://digistump.com/board/index.php/topic,2187.msg10302.html#msg10302 (http://digistump.com/board/index.php/topic,2187.msg10302.html#msg10302)
-
Desperately trying to keep this in 1 thread (here):
http://digistump.com/board/index.php/topic,2187.msg10320.html#msg10320
-
gregerwhut,
In order for anyone to help you, you'll need to provide more info...
1. How are you getting the serial output from the Oak?
2. What is complaining about the python import?
3. It appears that you are trying to run an Adafruit example. Why did you change to this line in the example
ser = serial.Serial('COM4', 9600)to this?
ser = serial.Serial('/dev/cu.Bluetooth-Incoming-Port', 9600)
You realize that the Oak does not support serial out through USB and that you need to have some sort of adapter connected to TX and RX on the Oak to get serial output to display on your PC.
I'm not a Python expert, but from looking at the Adafruit example that you based this on, it somehow accesses serial output and looks for the "M" in movement to trigger the email.
So I'm guessing the way this is supposed to work is you motion detect code outputs to a serial port (on your PC) where python is installed and running and looking for serial input. Is this how you have it set up?
-
Hey again
1. I dont know how i get serial output from oak. but that is not what I actually a, trying to. I wrote that I was unsure where about what was meant with the Particle.publish()??
2. I am trying to make this work in Adruino software. I am unsure if i should do the code directly on the https://build.particle.io/build/ (https://build.particle.io/build/). In the Adruino app it makes this error ïmport¨dpes not name a type
and the same for the serial and smtplib.
3.
I changed it because it was stated here https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/python-code (https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/python-code)
.
4. I have Macbook, and are unfamiliar on how to setup Puthon, and I do not know anything off what i does?. I just need the most simple way for making this work together
-
I posted a reply here before I realised that exeng was trying to stop this thread from fragmenting.
http://digistump.com/board/index.php/topic,2187.msg10337.html#msg10337 (http://digistump.com/board/index.php/topic,2187.msg10337.html#msg10337)
Now that I see you later posts, I can see where some of the confusion is comming from. The Adafruit code example you linked is the python code that is meant to run on your computer. However, their code is meant to be used when Arduino (not an Oak) sends a message via it's USB<->Serial connection, and lets the computer know motion has been detected. That python code then triggers an email to be sent.
If your project is to use an Oak to detect motion with a PIR sensor, and then send an email to your phone to alert you of that motion, you could use IFTTT (http://IFTTT)as the final stage, instead of the python code on the computer (also removing the requirement to have a computer on all the time just for this purpose). You would create a recipe that waits for a particle event to be triggered (using the Particle.publish() command), and when that event triggers, sends an email. You can also choose other actions like sending an SMS, posting a message to twitter or facebook, etc.
Basically, using your code above as a example, you just need to add the Particle.publish() command as illustrated below, and then create a suitable IFTTT recipe to react to it.
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);
}
-
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?
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);
}
-
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?
-
I dont really understand what your question is ? :D
-
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 :)
-
For some reason I cant compile and verify the code i posted in the last message on the particle.io build. ?
-
@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.
// 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.
-
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?
-
@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 (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.
-
Looked at the datasheet again and perhaps this is a clue to the repeated motion detects...
From the datasheet:
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.
-
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.
-
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.
-
@gregerwhut,
Been looking at the specs for the PIR you say you have. Looks like repeat trigger is the set default.
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.
-
Also have a look at the datasheet that exeng linked to earlier (https://www.mpja.com/download/31227sc.pdf), 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).