Things used in this project


Semantics of Counter using PIR sensor
2 PIR sensors, Intel Edison board and Amazon AWS IoT and SNS platform combined in one project to implement a counter.


The code is well documented.
 * No More Allowed!
 * No copyright claims. Some lines of this code are taken from maraca.js which is published in Amazon AWS.
 * Link-    
 * This code demonstrates how to monitor PIR proximity sensor (PPIR sensor) data from an Edison via AWS IoT Service. 
 * Authors: Team Pluto
 * Date: 12/10/2016
//RequPired Libraries
var five = requPire("johnny-five")
var Edison = requPire("edison-io")
var awsIoT = requPire('aws-iot-device-sdk');
var sleep = requPire("sleep")

 * To set your AWS credentials, export them to your envPironment variables.
 * Run the following from the Edison command line:
// AWS IoT Variables
var mqttPort = 8883;
var rootPath = '/home/root/awscerts/';
var awsRootCACert = "root-CA.crt";
var awsClientCert = "pluto.cert.pem";
var awsClientPrivateKey = "pluto.private.key";
var topicName = "plutoTopic";
var awsClientId = "pluto";
var REGION = "us-east-1";

var LED = 3 // D3 on the Grove shield for fPirst LED
var LEDG=6 //D6 on the Grove shield for second LED

var PARTITION_KEY = "xyz"; // DO NOT CHANGE | Your partition key.
var DEVICE = "edison"; // DO NOT CHANGE | Your device type.
var DEVICE_ID = "pluto"; // CHANGE | Your device id / team name.
var THRESHOLD = 50; // OPTIONAL | The amount of change that is requPired for the accelerometer to be "shaking"
var INTERVAL = 1000; // OPTIONAL | 1000 milliseconds, or one second. 

 * Instance AWS variables for use in the application for
 * AWS IoT Certificates for secure connection.
var privateKeyPath = rootPath + awsClientPrivateKey;
var clientCertPath = rootPath + awsClientCert;
var rootCAPath = rootPath + awsRootCACert;

*Initializing Device Communication for AWS IoT
var device = awsIoT.device({
    keyPath: privateKeyPath,
    certPath: clientCertPath,
    caPath: rootCAPath,
    clientId: awsClientId,
    region: REGION
console.log("AWS IoT Device object initialized");

var flag1=0;
var flag2=0;
var count=0;

 * Initializing the Edison board with the Johnny-Five node library.

var board = new five.Board({
	io: new Edison()
 * Once the board is ready to go, run the code!


	//fPirst proximity sensor on A0 pin
	var proximity = new five.Proximity({
		controller: "GP2Y0A21YK",
		pin: "A0"


	//Two Leds are initialized

	var led = new five.Led(LED);
	var led1 = new five.Led(LEDG);

device.on('connect', function (){
      console.log("Connecting to AWS IoT....");

device.on('error', function (error) {
        console.log('error', error);
//Sensor 1 is placed outside the door entrance and sensor 2 is placed inside the door entrance.We have to give a threshold for distance value measured by the PIR sensors. 
//If the distance is less than the threshold, it means that a person is crossing the door.
//When sensor-1 crosses the threshold (from high value to low value), it indicates the arrival of a person. 
//Because it is understood that sensor-2 will trigger (high to low) almost certainly after sensor-1.
// So, we ignore the trigger the sensor for 1 sec, assuming that a person takes 
//On the other hand, when a person exits, the sensor-2 will trigger (high to low) first. 
// And thus we detect that a person exits the room.
		if (>15){ 
			// The distance threshold is 15 inch
			flag1 = 0;
		if (<=15){
			if (flag1==0){
				// This means that a person entered the room.
				count = count+1;
				console.log("proximity enter: ");
				console.log(" in :",;
				console.log("count: ",count);
				//A delay of 1 sec is the time, a person takes to enter the room.
			flag1 = 1;

	//The second sensor is used to check when a person exits.
	var proximity1 = new five.Proximity({
		controller: "GP2Y0A21YK",
		pin: "A1"
		//The distance threshold is 15 inch
		if (>15){
			flag2 = 0;
		if (<=15){
			if (flag2==0){
				if (count>0){
					// This indicates that a person exits the room.
					count = count-1;
				console.log("proximity exit: ");
				console.log(" in :",;				
				console.log("count: ",count);
				// It takes 1 sec for a person to exit the room.
			flag2 = 1;
		//The time of 50 ms is the time interval between two successive sensing cycle. 
		//It means the sensors are off for 50ms. This is done to reduce the sensitivity of the sensor.
	// sendData packages the JSON string and sends it to SNS with the Node.JS AWS SDK.
    function sendData() {
        var json = {
            default: "Enter default message here",
            count: count


        device.publish(topicName, JSON.stringify(json));
        console.log("JSON Msg Published to AWS IoT Topic. Payload:" + JSON.stringify(json));



Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback


Similar projects you might like

Eco-Thing #1  "Eco-Smart-Container V1.0"
  • 1,646
  • 12

Full instructions

An IoT device that could help keep the Sandy River Delta clean, as garbage is a very common problem.

Computer Vision Neural Network on Your Intel NUC / Linux Box
  • 385
  • 4

Work in progress

Want to turn your Intel® NUC, or any Linux box running Ubuntu into an artificially intelligent, IoT connected CCTV hub? Check this out!

Smart Garbage Monitoring System Using Arduino 101
  • 729
  • 5

Full instructions

A method in which waste management is automated. An innovative system that will help keep the city clean and healthy

Shower Regulator
  • 2,940
  • 3

Full instructions

Learn how to make a device to limit your shower time!

  • 168
  • 2

Full instructions

An Arduino controlled boat that takes temperature, accelerometer, and light data from any body of water.

Portable Environment Monitor
  • 275
  • 1

Full instructions

A handheld monitor for young scientists to get familiar with various aspects of the surrounding environment.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog