หลายท่านคงเคยได้ยินเกี่ยวกับการเก็บสถิติของผู้เข้า-ออกภายในโรงงาน บริษัทและออฟฟิศที่ผ่านเข้า-ออกภายในแต่ละวันโดยการนับจากเจ้าหน้าที่ การเซ็นต์ชื่อและการสแกนบัตร บทความนี้จะเกี่ยวกับการเก็บสถิติของจำนวนผู้เข้า-ออก โดยเซนเซอร์อินฟราเรด Infrared ( IR ) ซึ่งมีหลากหลายแบบแต่สำหรับบทความนี้เลือก Sensor Infrared E18-D80NK เพราะมีความเหมาะสมกับการใช้งานในข้างต้นที่กล่าวมา
รูปภาพที่ 1 เปรียบเทียบคุณสมบัติ Sensor แบบต่างๆ
อุปกรณ์ที่ต้องจัดเตรียม มีดังนี้
1. IR Sensor ( E18-D80NK) 2 ตัว
2. Breadboard
3. NodeMCU8266
4. Jumper Wire
5. จอ OLED I2C 128x64
6. แหล่งจ่ายไฟ 5V ในที่นี่ผมเลือกใช้ AX-WIFI ( มีจอOLED พร้อมในบอร์ด )
7. Resistor 100 ohm
การใช้งานจอOLED //www.praphas.com/forum/index.php?topic=312.0
แนะนำ Sensor Infrared ( E18-D80NK )
เซนเซอร์ตัวนี้ใช้แสง Infrared ในการค้นหาวัตถุที่อยู่ด้านหน้าของ Sensor โดยการส่งสัญญาณออกและรับสัญญาณที่สะท้อนกลับมา สามารถวัดระยะได้ในช่วง 6–80 ซม. มุมองศาในการวัด +- 15 องศา
โดยมีคุณสมบัติดังนี้
Use Power Supply : DC 5V
Maximum load current : 100mA
Output current : DC / SCR / Relay Control 100mA/5V supply
Current consumption : DC <25mA
Response time : <2ms
Angle : more than 15°
Effective distance Adjustable from 3–80CM
Working environment temperature : -25 °C ~ 55 °C
Size : 17mm x 45mm
Brown : power supply positive 4.5–5V
Black : TTL output for connection to microcontroller
Blue : GND
หลักการทำงานของ Sensor E18-D80NK
สำหรับหลักการทำงานคือ ตัวIR (Infrared) จะเปล่งแสงอินฟราเรดออกมาอย่างต่อเนื่อง หากว่ามีวัตถุผ่านทางช่องที่อยู่ของเซนเซอร์ด้านหน้าอุปกรณ์ (ซึ่งอยู่ในระนาบเดียวกัน) ตัววัตถุจะสะท้อนแสงไปในเซนเซอร์เข้ากับชุดกระจกภายในภายเซนเซอร์ จากนั้นแสงที่ถูกวัตถุสะท้อนออกมานั้น จะถูกส่งไปยังตัวตรวจจับทางด้านล่างของเซนเซอร์ จากนั้นถ้าตรวจสอบพบวัตถุแสงไฟ LED ด้านหลังของ Sensor Infrared E18-D80NK จะติดขึ้นเป็นสีแดง
รูปภาพที่ 2 Sensor Infrared E18-D80NK
การติดตั้ง Sensor E18-D80NK เข้ากับประตู
1.ติดตั้ง Sensor E18-D80NK ลักษณะวางคู่กัน ระยะห่างประมาน 5–7 เซนติเมตร(ขึ้นกับขนาดของขอบประตูด้วย)
2.ติดตั้ง Sensor E18-D80NK ให้สูงประมาณระดับเอวของมนุษย์ เพราะเป็นช่วงที่ใหญ่ของร่างกายทำให้ Sensor สามารถตรวจจับได้แม่นยำมากขึ้น
3.ติดตั้ง Sensor E18-D80NK ด้วยฐานที่มั่นคงแข็งแรงเพื่อป้องกันการสั่นไหวของ Sensor เพราะอาจจะทำให้เกิดการรวน
ส่วนของ Library ที่ต้องดาวน์โหลดเพิ่มเติมครับ
1. Microgear
2. Adafruit_SSD1306
3. Adafruit_GFX
4. SPI
5. Wire
วิธีติดตั้ง Arduino IDE และ Library
//netpie.gitbooks.io/nodemcu-esp8266-on-netpie/content/chapter1.html
//arduinoprojects.in.th/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87library-%E0%B8%9A%E0%B8%99-arduino/
ส่วนของ Code ใน Arduino IDE นะครับ
//ArduinoIDE#include <ESP8266WiFi.h>
#include <MicroGear.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET (-1)Adafruit_SSD1306 OLED(OLED_RESET);
const char* ssid = "gotjieiei";
const char* password = "eieigotji";#define APPID "xxxxxxxxxxxx" //APP ID NETPIE
#define KEY "xxxxxxxxxxxx" //KEY NETPIE
#define SECRET "xxxxxxxxxxxx" //SECRET NETPIE
#define ALIAS "xxxxx" //ชื่อที่ใช้ติดต่อ
#define FEEDID "xxxxx" //FEEDID
#define INTERVAL 285000
#define T_INCREMENT 200
#define T_RECONNECT 100000
WiFiClient client;
String notifi ;
int ir_right_pin = D3;
int ir_left_pin = D4;
int ir_right_state = 0;
int ir_left_state = 0;
int in_counter;
int out_counter;
int timer = 0;
char str[20];
unsigned long tm;
int notifiin =0;
int notifiout =0;
int state =0;
int Inroom=0;
MicroGear microgear(client);// when the other thing send a msg to this board
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
Serial.print("Incoming message --> ");
msg[msglen] = '\0';
char strState[msglen];
for (int i = 0; i < msglen; i++)
{
strState[i] = (char)msg[i];
Serial.print((char)msg[i]);
}
Serial.println();
String stateStr = String(strState).substring(0, msglen);
//สั่งreset value in-out
if (stateStr == "reset") // ถ้า netpie ส่งข้อความมาว่า reset
{
in_counter=0;
out_counter=0;
notifiin=0;
notifiout=0;
Inroom=0;
miss=0;
state=0;
}
}
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
microgear.setAlias(ALIAS);
}
void setup(void) {
OLED.begin(SSD1306_SWITCHCAPVCC,0x3c);
Serial.begin(115200);
microgear.on(MESSAGE,onMsghandler);
microgear.on(CONNECTED,onConnected);
pinMode( ir_right_pin, INPUT);
pinMode( ir_left_pin, INPUT);
pinMode(D5,INPUT);
Serial.println("Starting...");
if (WiFi.begin(ssid, password)) {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
microgear.init(KEY,SECRET,ALIAS);
delay(500);
microgear.connect(APPID);
}
}
void loop(void) {
ir_right_state = digitalRead( ir_right_pin );
ir_left_state = digitalRead( ir_left_pin );
Inroom = in_counter-out_counter;
//*******************Reset State ==0*********************
if((ir_right_state == 1) && (ir_left_state == 1))
{
state=0;
}
//*************State == 0 (ค่าเริ่มต้น)******************
if(state==0)
{
if((ir_right_state == 0) && (ir_left_state == 1))
{
state=1; //State Count In
}
if((ir_right_state == 1) && (ir_left_state == 0))
{
state=2; //State Count Out
}
}
//***************************************************************
//********************Count In***********************************
if(state==1)
{
if(ir_left_state == 0 && ir_right_state == 0)
{
state=3;
}
}
if(state==3)
{
if(ir_left_state == 0 && ir_right_state == 1)
{
in_counter++;
Inroom = in_counter-out_counter;
//OLED Display for in
OLED.clearDisplay();
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.setTextSize(2);
OLED.println(" Hi");
OLED.print("Inroom :");
OLED.println(Inroom);
OLED.display();
microgear.publish("/countin", in_counter);
microgear.publish("/Inroom", Inroom);
state=0;
}
}
//************************End Count In***************************//***********************Count Out*******************************
if(state==2)
{
if(ir_left_state == 0 && ir_right_state == 0)
{
state=4;
}
}
if(state==4)
{
if(ir_left_state == 1 && ir_right_state == 0){
out_counter++;
Inroom = in_counter-out_counter;
if(Inroom<0){
Inroom=0;
in_counter=0;
out_counter=0;
}
//OLED Display for out
OLED.clearDisplay();
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.setTextSize(2);
OLED.println(" Bye");
OLED.print("Inroom :");
OLED.println(Inroom);
OLED.display();
microgear.publish("/countout", out_counter);
microgear.publish("/Inroom", Inroom);
state = 0;
}
}
//**********************End Count Out******************************
//************************Notification*****************************
if(Inroom==1 && out_counter==0 && in_counter==1){
notifiin++;
if(notifiin==1){
notifi = "Frist People In Room";
microgear.publish("/@push/owner",notifi);
}
}
if(in_counter && out_counter !=0 && Inroom==0){
notifiout++;
if(notifiout==1){
notifi = "Not People In Room";
microgear.publish("/@push/owner",notifi);
notifiin=0;
}
}
//******************************************************************
//*************************Microgear********************************
if (microgear.connected()) {
microgear.loop();
if (timer >= INTERVAL) {
String data = "{\"in_counter\":";
data += in_counter ;
data += ", \"out_counter\":";
data += out_counter ;
data += ", \"Inroom\":";
data += Inroom ;
data += "}";
if (isnan(in_counter) || isnan(out_counter) ) {
Serial.println("Failed to read from IR sensor!");
}else{
sprintf(str,"%d,%d,%d",in_counter,out_counter,Inroom);
//Serial.println(str);
microgear.publish("/inout",str);
Serial.print("Sending-->");
Serial.println((char*)data.c_str());
microgear.writeFeed(FEEDID,data); //YOUR FEED ID,API KEY
Inroom = in_counter-out_counter;
if(Inroom<=-1){
Inroom=0;
in_counter=0;
out_counter=0;
}
//OLED Display room
OLED.clearDisplay();
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.setTextSize(2);
OLED.println(" DACRU");
OLED.print("Inroom :");
OLED.println(Inroom);
OLED.display();
}
timer = 0;
}
else timer += T_INCREMENT;
}
else {
Serial.println("Connection Lost");
delay(1000);
if (timer >= T_RECONNECT) {
Serial.println("Reconnect");
microgear.connect(APPID);
timer = 0;
}
else timer += T_INCREMENT;
}
delay(10);
}
ส่วนการตั้งค่าใน NETPIE
รูปภาพที่ 4 หน้าต่างของ FEEDS
รูปภาพที่ 5 หน้าต่างของ Freeboard บน NETPIE
รูปภาพที่ 6 หน้าต่าง Code ของ Microgear ใน Freeboard
รูปภาพที่ 7 หน้าต่าง Code ของ NETPIE Feed ในการดึงค่าจาก Feed
รูปภาพที่ 8 หน้าต่าง Code ใน Gauge In
รูปภาพที่ 9 หน้าต่าง Code ใน Gauge Out
รูปภาพที่ 10 หน้าต่าง Code ใน Gauge Inroom
รูปภาพที่ 11 หน้าต่าง FeedView
รูปภาพที่ 12 หน้าต่าง Code ปุ่ม Reset
ส่วนรายละเอียดการใช้งาน NETPIE เพิ่มเติมกดลิ้งด้านล่างนี้เลยครับ
//drive.google.com/drive/u/0/folders/0B9jvOTVzGjXJdFZfV3ZvcjhsUjg
Reference Links
//netpie.gitbooks.io/nodemcu-esp8266-on-netpie/content/chapter1.html
//drive.google.com/drive/u/0/folders/0B9jvOTVzGjXJdFZfV3ZvcjhsUjg
//netpie.gitbooks.io/nodemcu-esp8266-on-netpie/content/lab-7.html
//www.ett.co.th/productSensor/E18-D80NK/Manual_IR-Sensor%20Switch%20E18.pdf
***ข้อควรระวัง : การติดตั้ง Sensor ควรติดตั้งในระยะห่าง-ระดับความสูงที่เหมาะสมกับขนาดของประตูแต่ละแบบที่ติดตั้งและลักษณะของการใช้งานเพราะจะมีผลต่อการนับจำนวนการเข้าออก