User Tools

Site Tools


oak:tutorials:pir

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
oak:tutorials:pir [2016/08/11 23:05]
brohan created
oak:tutorials:pir [2017/01/16 22:53] (current)
weaslyd [Code]
Line 1: Line 1:
 ===== Oak: PIR Motion Sensor ===== ===== Oak: PIR Motion Sensor =====
  
-======= Work in Progress ======= 
  
 {{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​pir_sketch.png?​300| {{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​pir_sketch.png?​300|
Line 7: Line 6:
  
  
-If you've been working your way though the starter tutorials to get here, you've found that there are many different kinds of sensors readily available for your Oak, each with their own benefits. The PIR sensor (Passive Infrared Sensor) works by detecting infrared (invisible radiant) energy, aka heat. These are most commonly knows as the security sensor tucked up inside the corners inside homes to detect intruders.+If you've been working your way though the starter tutorials to get here, you've found that there are many different kinds of sensors readily available for your Oak, each with their own benefits. The PIR sensor (Passive Infrared Sensor) works by detecting infrared (invisible radiant) energy, aka heat. These are most commonly knows as the security sensor tucked up inside the corners inside homes to detect intruders. Let's take this sensor one step further and incorporate it into the Internet of Things (IoT),and expand the reach of our Particle with [[https://​ifttt.com/​]].
  
  
Line 20: Line 19:
 ===== Concepts ===== ===== Concepts =====
  
-The take away from this tutorial ​is to thoroughly research ​the sensors ​you are interested ​in for their compatibility+=== Connecting to other products and apps === 
 + 
 +In other tutorials, we've been able to publish items to your Particle account. This is an easy 1st step in branching your Oak into the wiles of the internet. From here, you can expand your Oak's reach and productivity far and wide across the globe. For this exercise you'll create a free [[https://​ifttt.com/​]] 
 + ​acount by clicking the link. This is going to enable you to connect your Oak / Particle account to many other apps and websites. Go ahead and follow ​the steps below to get everything set up. In addition to your existing particle account, and your ifttt account, we will be using Gmail. If you don't have one (they are free as well) I recommend getting one, if for nothing else then using it as you experiment with your Oak. 
 + 
 +Process for ifttt: 
 + 
 +1.) Create your free ifttt account. Once you have logged ​in, from the dropdown menu at the top, select "​Create"​ You will be presented with "​IfTHISthenTHAT"​. Click on the word "​this"​ 
 + 
 +2.) Select your Channel. You will be prompted to select your trigger channel. In the search box enter "​Particle"​ and select it, then log in to your Particle account. 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step1.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step1.png?​500|}} 
 + 
 +Next choose the option "New Event Published"​. 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step2.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step2.png?​500|}} 
 + 
 +The last step in our Trigger process is to tell ifttt exactly what to listen ​for. In our program we'll be using the Event Name "​MOTION SENSOR",​ and the Events Contents of "​Motion detected"​. These names must match exactly. Finally choose which device of your you will be listening on. This is a clue that we can publish, and then listen for a number of items at once, as well as multiple Oaks. 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step3.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step3.png?​500|}} 
 + 
 + 
 +3.) We finish our ifttt setup by specifying what action to perform by clicking on the word "​that"​ that appears. For your action channel, this time choose Gmail, again follow the prompts to enable your Gmail account with ifttt. 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step4.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step4.png?​500|}} 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step5.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step5.png?​500|}} 
 + 
 +Then tell ifttt specifically what to do in Gmail when it hears the event from particle. In this case we will simply have it send an email to ourselves letting us know that motion was detected. 
 + 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step6.png?​200| 
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ifttt_step6.png?​500|}} 
 + 
 +Press Create. Doing so automatically starts ifttt listening for this specific event on Particle. Nothing will happen of course until we have our program running on the Oak. I recommend that you keep this window open (or bookmarked if you aren't going to finish this tutorial straight away), so that you can easily stop this "​recipe"​ once we have everything working. 
 + 
 + 
 +=== Research Your Sensors ===
  
 My particular HC-SR501 states that it operates with 4.5 - 20v. However taking a deeper look, the chipset'​s datasheet states it runs on 3.3v. The 5v input goes to the board'​s power regulator, which shifts it to 3.3v. There is a bypass pin that allows one to power the sensor directly with 3.3v. This is why the power jumper does not go to the bottom sensor input. See the pictures below for more detail. My particular HC-SR501 states that it operates with 4.5 - 20v. However taking a deeper look, the chipset'​s datasheet states it runs on 3.3v. The 5v input goes to the board'​s power regulator, which shifts it to 3.3v. There is a bypass pin that allows one to power the sensor directly with 3.3v. This is why the power jumper does not go to the bottom sensor input. See the pictures below for more detail.
Line 50: Line 90:
 ===== Code ===== ===== Code =====
  
-Again, if you've been doing the starter tutorials, this code should be 2nd nature to you. The lines that aren't typical from the starter tutorials have been commented.+If you've been doing the starter tutorials, this code should be 2nd nature to you. The lines that aren't typical from the starter tutorials have been commented. The onboard LED is used to signal at the device that motion was detected. 
 + 
 +The take away from this will be you creating a free ifttt.com account, and connecting it to your Particle account.
  
 <​code>​ <​code>​
Line 72: Line 114:
  
   //give the sensor some time to calibrate   //give the sensor some time to calibrate
-    ​for(int i = 0; i < calibrationTime;​ i++){ +  ​for(int i = 0; i < calibrationTime;​ i++){ 
-      delay(1000);​ +    delay(10000);
-      } +
-    delay(50);+
   }   }
 +  delay(50);
 +
 +}
  
 void loop(){ void loop(){
  
-     if(digitalRead(pirPin) == HIGH){ +  ​if(digitalRead(pirPin) == HIGH){ 
-       ​digitalWrite(ledPin,​ HIGH);  +    digitalWrite(ledPin,​ HIGH); 
-       ​if(lockLow){ ​  +    ​Particle.publish("​MOTION SENSOR",​ "​Motion detected"​);​ 
-         ​lockLow = false; ​            +    ​if(lockLow){ ​  
-         ​delay(50); +      lockLow = false; ​            
-         ​}          +      delay(1000); 
-         ​takeLowTime = true; +    }          
-       ​}+    takeLowTime = true; 
 +  }
  
-     if(digitalRead(pirPin) == LOW){        +  ​if(digitalRead(pirPin) == LOW){        
-       ​digitalWrite(ledPin,​ LOW);  ​+    digitalWrite(ledPin,​ LOW);  ​
  
-       if(takeLowTime){ +    ​if(takeLowTime){ 
-        lowIn = millis(); ​      +      lowIn = millis(); ​      
-        takeLowTime = false; ​      +      takeLowTime = false; ​      
-        }+    }
  
-       if(!lockLow && millis() - lowIn > pause){ ​  +    ​if(!lockLow && millis() - lowIn > pause){ ​  
-           ​//makes sure this block of code is only executed again after  +      //makes sure this block of code is only executed again after  
-           ​//a new motion sequence has been detected +      //a new motion sequence has been detected 
-           ​lockLow = true;                         +      lockLow = true;                         
-           ​delay(50);​ +      delay(50);​ 
-           ​+    
-       }+    
   }   }
 +
 +}
 +
 </​code>​ </​code>​
  
 +=== Results ===
  
 +Remember the ifttt.com page with our recipe? Before you flash the program to your Oak, I highly recommend that you have this recipe page open in your browser, your Particle log page, as well as your Gmail page, so that you can see everything working together.
 +
 +Flash your Oak, and you will shortly see your Particle logs showing the information that you've published, and if your recipe is active, you will see an email (or a steady stream) arrive in your Gmail inbox. To halt the emails, simply turn off your recipe on ifttt.
 +
 +== Note ==
 +
 +You will most likely need to make many adjustments to tweak this in. You can most likely adjust the sensitivity of your PIR sensor, for activity, for depth of field, as well as the timeout of the sensor. You can also adjust the timing of the reporting within the program. ​ The current set up should fire off a steady stream of emails to show you that it's working in short order, unless of course you are in a room by yourself, with the sensor pointing away from you, and you aren't moving at all.
  
 ===== Conclusion ===== ===== Conclusion =====
 +
 +Connecting your Oak to Particle, (and thereby OakTerm) gives you a number of options, the ability to view what your Oak is doing, program your Oak through the Particle IDE, and event put it into Config Mode via OakTerm. Using a system like ifttt.com quickly, and easily extends the capability of your Oak.
  
 With the various devices available, with varying voltages, it's not uncommon for manufacturers to market and build to a specific voltage only. This doesn'​t mean that it won't work, sometimes with minor modifications,​ with something else. If your up for it, do further research to see what you can do with some tweeking, at your own risk, and the risk of your equipment of course. With the various devices available, with varying voltages, it's not uncommon for manufacturers to market and build to a specific voltage only. This doesn'​t mean that it won't work, sometimes with minor modifications,​ with something else. If your up for it, do further research to see what you can do with some tweeking, at your own risk, and the risk of your equipment of course.
oak/tutorials/pir.1470981908.txt.gz · Last modified: 2016/08/11 23:05 by brohan