Introduction: Remotely Monitored Motion Detector With Wia
For this tutorial, we will be creating a motion detector that can be monitored from anywhere in the world using Wia (https://www.wia.io). Wia is a cloud platform that enables makers to bring their ideas to life without having to worry about building a real-time infrastructure, managing servers or designing databases.
You will need a Wia account to do this tutorial. You can create one here https://www.wia.io/signup
Step 1: Parts
Here's a list of the required parts and where you can find them on Amazon (UK).
Raspberry Pi 2 Model B (any version should do). http://www.amazon.co.uk/Raspberry-Model-Desktop-Qu...
PIR Motion Alarm Detection module. http://www.amazon.co.uk/Motion-Detection-module-Ra...
MicroSD Card. http://www.amazon.co.uk/Samsung-Memory-MicroSDHC-U...
Ethernet Cable.
Micro USB power cable.
Step 2: Setup Your Raspberry Pi
Go to https://www.raspberrypi.org/downloads/raspbian/ to download the latest version of Raspbian. Here's the installation guide on how to prep your MicroSD card and install an image https://www.raspberrypi.org/documentation/installa...
Now we will connect the PIR to your Raspberry Pi.
- Connect VCC to GPIO port 2.
- Connect GND to GPIO port 6.
- Connect OUT to GPIO port 12.
See the diagram above showing the different ports.
Now connect the Ethernet cable and power supply to your Raspberry Pi.
Step 3: Install Node.js on Your Raspberry Pi
The instructions for this are copied from here http://blog.wia.io/installing-node-js-v4-0-0-on-a-...
Download Node.js source
Raspberry Pi Model A, B, B+ and Compute Module
wget https://nodejs.org/dist/v4.0.0/node-v4.0.0-linux-armv6l.tar.gz tar -xvf node-v4.0.0-linux-armv6l.tar.gz cd node-v4.0.0-linux-armv6l
Raspberry Pi 2 Model B
wget https://nodejs.org/dist/v4.0.0/node-v4.0.0-linux-armv7l.tar.gz tar -xvf node-v4.0.0-linux-armv7l.tar.gz cd node-v4.0.0-linux-armv7l
Copy to /usr/local
sudo cp -R * /usr/local/
Step 4: Connect to Wia and Publish an Event
Create a device in the Wia dashboard (https://dashboard.wia.io). Make a note of the device token. You can find it in the device's settings.
Create your project
cd ~ mkdir wia-motion-detector npm init
Note: Just press 'y' to everything in npm init.
Add packages
npm install --save wia npm install --save onoff
Create a Device client
touch index.js nano index.js
var wia = require('wia')('token');
Watch PIR for motion
var Gpio = require('onoff').Gpio, pir = new Gpio(18, 'in'); pir.watch(function (err, value) { if (err) { throw err; } wia.events.publish({name: "motionDetected"}); });
Run!
node index.js
To check your events, go to the Events tab for your device.