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
×2

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

Personal Healthcare Assistant PHA
Intermediate
  • 1,584
  • 113

Full instructions

Wearable for monitoring the health condition of patients who have suffered or are in risk of having a cardiac arrest.

OIKOS
Intermediate
  • 218
  • 6

Work in progress

OIKOS is a smart social-connected home for everyone. Our goal is to put the society in connection with homeless through IoT.

OIKOS

OIKOS NGO TEAM

Rocky Rover: Robotic Vision System PixyCam & Arduino 101
Intermediate
  • 4,034
  • 42

Full instructions

Using PixyCam and Intel Arduino 101 to drive this rover. Easy Robotic Vision system using PixyCam, rover reacts to the ball using Pan/Tilt.

Smart and Safe Cabinets
Intermediate
  • 391
  • 8

Full instructions

With 41% of poisonings happening to children under 4, more needs to be done. We use Amazon Alexa and Intel IoT to lock and unlock cabinets.

Smart and Safe Cabinets

Magic Smoke Replacement Team

Face Lock Deadbolts
Intermediate
  • 981
  • 11

Work in progress

Using 3D RealSense facial recognition to unlock deadbolt doors through ARTIK Cloud

Let There Be Smart Light!
Intermediate
  • 273
  • 4

Work in progress

A solution to identify and report city street lights that are not working and need to be repaired.

Let There Be Smart Light!

Team The Ravishers

ProjectsCommunitiesContestsLiveFree StoreBlogAdd projectSign up / Login
Respect project