No More Allowed!

IoT implementation to adhere with "Certificate of Occupancy".

IntermediateFull instructions provided5 hours341
No More Allowed!

Things used in this project

Story

Read more

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_Pluto

JavaScript
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

Ish Kumar Jain

Ish Kumar Jain

1 project • 0 followers
NYU student in Electrical Engineering
Contact
Lakshmana Sitarama Kishore Suri

Lakshmana Sitarama Kishore Suri

1 project • 0 followers
Contact
Amitesh Sah

Amitesh Sah

1 project • 0 followers
Contact
Prakhar Pandey

Prakhar Pandey

1 project • 0 followers
Contact

Comments