Вы находитесь на странице: 1из 8

Enter & Exit automated room system

By Bader Al-mazmumi and Arcturus12

This project is called bidirectional visitor counter. It does two things. First, it counts for
you how many person in your room, second it turns your rooms' light on/off automatically
when nobody is in the room.

It contains two sensors, one to detect who enters the room and the other to detect who
leaves the room, and 7 segment display to show the total number of people in that room.

How it works:
When you entered the room, for example, the left sensor will detect you first, which means
you entered the room, so we add one to the total number. Similarly, when you leave the
room, the right sensor will detect you first which means youre leaving the room so we
subtract one from the total number. This project has some issues such as if two persons in
row or besides each other entered the room they will be counted as one person but
hopefully it will be improved day by day.

What you need:

1- NodeMCU Lua Wi-Fi IOT.


2- Jumper wires.
3- 2 x IR Obstacle Sensor module.
4- 7-segment display.

Procedure:

First, you should prepare your computer so it can communicate with NodeMCU
module using the Arduino IDE:
1- Install the latest version of Arduino IDE <Arduino 1.8.3>.
https://www.arduino.cc/en/Main/Software

2- Start the Arduino IDE, Go to File --> Preference, and in the Additional Boards
Manager URLs field, enter the following link:

http://arduino.esp8266.com/stable/package_esp8266com_index.json
and press OK.

3- Go to Tools --> Boards --> Boards Manager. Search for esp8266 package and
then select the version you want < in my case its 2.3.0> and then hit install.

4- Go to Tools --> Boards and chose your device from the list < NodeMCU 1.0 (12E
Module).

5- Now your NodeMCU module is ready to code using the Arduino IDE.
Second, make an IFTTT applet that receive a request from NodeMCU module and
send request to your smart light that you have installed in your room to turn it
off/on.
1- Go to IFTTT website < IFTTT.com >.
2- Create a new applet to turn the light on.
3- In the This part: search for Webhooks service and chose Receive a web
request.
4- Name this event any name you want <Example: turnTheLightOn>. You should
remember it because well use it later.
5- In the That part: chose any service that turn your light on. < For Tp-link
devices you can see our tutorials that can help you to control tp-link devices
from ifttt.
6- Create a new applet to turn the light off and name it whatever you want
<Example: turnTheLightOff>.

Third, set up the circuit:

Bader ( Arcturus Enterprises )

Bader ( Arcturus Enterprises )

Bader ( Arcturus Enterprises )

Connect the components as shown in the picture.

Fourth, coding part:


1- Copy and paste the following sketch into your Arduino IDE.

#include <ESP8266WiFi.h>

int in = D0;
int out = D1;

int LedG = D5;


int LedF = D4;
int LedA = D3;
int LedB = D2;
int LedE = D6;
int LedD = D7;
int LedC = D8;

int personCounter;
boolean if1 = false;
boolean if2 = false;
boolean lighton = false;

void setup(){
Serial.begin(9600);
pinMode(in,INPUT);
pinMode(out,INPUT);
pinMode(LedA,OUTPUT);
pinMode(LedA,OUTPUT);
pinMode(LedB,OUTPUT);
pinMode(LedC,OUTPUT);
pinMode(LedD,OUTPUT);
pinMode(LedE,OUTPUT);
pinMode(LedF,OUTPUT);
pinMode(LedG,OUTPUT);

WiFi.begin("SSID", "PASS");

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop(){
if(!digitalRead(in)){
if(!if2){
if1=true;
}else{
personCounter--;
delay(500);
if1=false;
if2=false;
}
}
if(!digitalRead(out)){
if(!if1){
if2=true;
}else{
personCounter++;
delay(500);
if1=false;
if2=false;
}
}

switch(personCounter){
case -1:
personCounter=0;
zero();
case 0:
zero();
break;
case 1:
one();
break;
case 2:
tow();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
case 10:
one();
personCounter = 1;
break;
}

//Arcturus-12's final modifications

if(personCounter <= 0 && lighton == true){


turnOffTheLight();
lighton = false;

}else if(personCounter>0 && lighton == false){


turnOnTheLight();
lighton = true;
}

//by using a system where "lighton" is checked before changing state,


unnecessary POST requests are avoided
//end of modifications

void turnOnTheLight(){
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect("maker.ifttt.com", httpPort)) {
Serial.println("connection failed");
return;
}

// We now create a URI for the request


String url = "/trigger/EVENT_NAME/with/key/YOUR_EVENTS_KEY";
Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server


client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " +
"maker.ifttt.com" + "\r\n" + "Connection: close\r\n\r\n");

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

void turnOffTheLight(){

// Use WiFiClient class to create TCP connections


WiFiClient client;
const int httpPort = 80;
if (!client.connect("maker.ifttt.com", httpPort)) {
Serial.println("connection failed");
return;
}

// We now create a URI for the request


String url = "/trigger/EVENT_NAME/with/key/YOUR_EVENTS__KEY";
Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server


client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " +
"maker.ifttt.com" + "\r\n" + "Connection: close\r\n\r\n");

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

}
void zero(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, HIGH);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, LOW);
}

void one(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, LOW);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, LOW);
digitalWrite(LedE, LOW);
digitalWrite(LedF, LOW);
digitalWrite(LedG, LOW);
}

void tow(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, LOW);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, HIGH);
digitalWrite(LedF, LOW);
digitalWrite(LedG, HIGH);
}

void three(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, LOW);
digitalWrite(LedF, LOW);
digitalWrite(LedG, HIGH);
}

void four(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, LOW);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, LOW);
digitalWrite(LedE, LOW);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, HIGH);
}
void five(){
digitalWrite(LedB, LOW);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, LOW);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, HIGH);
}
void six(){
digitalWrite(LedB, LOW);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, HIGH);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, HIGH);
}
void seven(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, LOW);
digitalWrite(LedE, LOW);
digitalWrite(LedF, LOW);
digitalWrite(LedG, LOW);
}
void eight(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, HIGH);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, HIGH);
}
void nine(){
digitalWrite(LedB, HIGH);
digitalWrite(LedA, HIGH);
digitalWrite(LedC, HIGH);
digitalWrite(LedD, HIGH);
digitalWrite(LedE, LOW);
digitalWrite(LedF, HIGH);
digitalWrite(LedG, HIGH);
}

2- Finally, there are four things in the code should be replaced.


First, go to http://maker.ifttt.com and then click on the sitting icon in
upper right corner.
Copy the last part of the URL after the word use/ which is
YOUR_EVENTS_KEY.
Replace the YOUR_EVENTS_KEY in the code and replace it with the
key you just found.
Replace SSID by your WiFis name.
Replace PASS by your WiFis pass.
Replace EVENT_NAME by your events name that you chose
previously in the second part in this tutorial.

Вам также может понравиться