Академический Документы
Профессиональный Документы
Культура Документы
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.
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>.
#include <ESP8266WiFi.h>
int in = D0;
int out = D1;
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");
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
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;
}
// 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(){
// 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);
}