Things used in this project

Hardware components:
Intel.web.720.405
Intel Edison
×1
11026 02
Jumper wires (generic)
×1
Sharp microelectronics gp2y0a41sk0f image
Proximity Sensor
×1

Schematics

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.
Cakaufayuqeax9ckocxb

Code

Team_PlutoJavaScript
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- http://iot-hackseries.s3-website-us-west-2.amazonaws.com    
 * 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")

/*
 * CONFIGURATION VARIABLES 
 * To set your AWS credentials, export them to your envPironment variables.
 * Run the following from the Edison command line:
 * export AWS_ACCESS_KEY_ID='AKID'
 * export AWS_SECRET_ACCESS_KEY='SECRET'
 */
// 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";

// CONSTANTS - PIN LOCATIONS
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!
 */

board.on("ready",function(){

	//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.
	
	proximity.on("data",function(){
		if (this.in>15){ 
			// The distance threshold is 15 inch
			flag1 = 0;
		}
		if (this.in<=15){
			if (flag1==0){
				// This means that a person entered the room.
				count = count+1;
				led.on();
				sleep.usleep(10000)
				led.off();
				sendData();
				console.log("proximity enter: ");
				console.log(" in :",this.in);
				console.log("count: ",count);
				console.log("......");
				//A delay of 1 sec is the time, a person takes to enter the room.
				sleep.sleep(1)
			}
			flag1 = 1;
		}	

	});
	//The second sensor is used to check when a person exits.
	var proximity1 = new five.Proximity({
		controller: "GP2Y0A21YK",
		pin: "A1"
	});
	proximity1.on("data",function(){
		//The distance threshold is 15 inch
		if (this.in>15){
			flag2 = 0;
		}
		if (this.in<=15){
			if (flag2==0){
				if (count>0){
					// This indicates that a person exits the room.
					count = count-1;
					led1.on();
					sleep.usleep(10000)
					led1.off();
					sendData();
				}
				console.log("proximity exit: ");
				console.log(" in :",this.in);				
				console.log("count: ",count);
				console.log("......");
				// It takes 1 sec for a person to exit the room.
				sleep.sleep(1)
			}
			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.
		sleep.usleep(50000)
	
	});
	// 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));
    }
});

Credits

Replications

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

Comments

Similar projects you might like

Automated Shower Boiler
Intermediate
  • 27
  • 1

Full instructions

Do you live in a country where you use solar energy to warm up the water? Well, how about we automate the process of the shower boiler.

Interactive Mario Mushroom Block
Intermediate
  • 499
  • 2

Work in progress

Ever want to punch a Mario block and get a mushroom to come out? Me too, so I'm building it.

IoT Intruder System With Intel® Arduino 101 & DFRobot
Intermediate
  • 180
  • 2

Protip

IoT intruder alarm system on an Intel® Arduino 101 with DFRobot LCD Keypad Shield, connected to the IoT using the IoT JumpWay MQTT Library.

PubNub & Edison - Living in your own Public IoTaho!
Intermediate
  • 2,007
  • 6

Protip

Use PubNub and the Intel Edison to turn your Arduino 101 project into a true IoT project.

IoT-Based Patient Monitoring System
Intermediate
  • 470
  • 4

Work in progress

The system monitors the patients based on sensor data and notifies the corresponding doctors/nurses or management in case of emergency.

BLE Bot 9000
Intermediate
  • 302
  • 3

Work in progress

Building robots shouldn't be hard or laborious. All you need is an Arduino 101 and a Sparkfun chassis.

ProjectsCommunitiesContestsLiveJobsBetaFree StoreBlogAdd projectSign up / Login
Respect project