This is another sensor for the home automation system. PIR sensors are pretty simple, this project is nearly identical to the connected door switch I made earlier.
I bought a generic PIR detector off of Amazon. First thing you want to do is verify the pinout of the 3-pin connector. My pinout was printed on the silkscreen beneath the fresnel lens assembly.
Connect Vcc to 5V, Gnd to Ground, and Out to digital pin 2. Also, connect out to ground with a 10k pulldown resistor. That's it for the wiring - there's two trimpots that control the sensitivity of the PIR and the length of time that it signals high. I set both pots towards the middle, giving me a roughly 5min signal time and a decent amount of sensitivity. You may need to adjust the sensitivity a bit.
It's a pretty simple sketch - it basically reads pin digital pin 2 and changes the value of the BLE characteristic to 1 if motion is detected, or 0 if motion is not. Upload the sketch from the repository to your Arduino/Genuino 101 and away we go!
Go to your device dashboard and add another device by clicking on "Connect another device..."
Search for PIR Motion Detector and add it to your dashboard.
Click on the gear for your new device and generate a Token. Copy down the Device ID and Token for the upload script.
Either download the zip or clone the github repository with:
git clone https://github.com/zanycadence/motion_sensor.git
At a command line, enter the directory and then:
Install the dependencies by executing:
and change the motionBearer and motionId to the Token and ID values generated earlier
//device info var motionBearer = "Bearer INSERT_DEVICE_TOKEN_HERE"; var motionId = "INSERT_DEVICE_ID_HERE";
Note that the dustBearer variable should start with "Bearer " and then your device token.
The script itself is pretty basic - it connects to the 101 and subscribes to the notifications for the motion sensor. When the state of the sensor changes, it connects to the ARTIK Cloud and logs the data. Pretty simple, but having data on motion opens up the possibility of creating powerful and flexible rules that will work with my other connected devices.
Last but not least, here's some sample data I generated of my motion sensing in my office. Works pretty well!