정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

아두이노 WIFI를 이용하여 웹서버와 연동하고 싶습니다. ㅜㅜ
김지수 | 2015-09-19

안녕하세요~~` ㅜㅜ

지문인식도어락을 만들어서 현재 아두이노 자체의 웹서버가 아닌 저희가 만든 웹서버와 연동을 하려고 합니다....

근데 제가.... JSON형태로 웹서버에 전송을 하고 웹서버에서 다시 JSON형태로 받아서 파싱을 통해 데이터를 받아서

지문인식 도어락이 열리지 않게 하려고 하는데 어떻게 해야 할까요??? ㅜㅜ

전송시에는 지문번호를 전송을 하고 문을 열기 위해 지문인식을 하면 웹서버에서 열어도 좋다는 데이터를 받아서 열게 하려고 합니다. ㅜ

아두이노 고수님들..... 도와주세요 ㅜㅜ

소스는 다음과 같습니다. ㅜ

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <Servo.h>

#include "FPS_GT511C3.h"
#include<SoftwareSerial.h>

// Mode definitions
#define MODE_IDLE      0
#define MODE_WELCOME   1
#define MODE_ENROLL    2
#define MODE_DEROLL    3

// Buttons definition
#define BTN_MODE      7
#define BTN_SELECT    8
#define BTN_NEXT      9

// Other things..

#define MOTION_SENSOR  A3
#define BACKLIGHT      A4 //SDA : A5번 SCL : A5번
#define DOOR           12

#define IDLE_TIME       300
#define MAX_ID      200

// Emoticons
#define CH_LOCK   1
#define CH_OPEN   2
#define CH_PERSON   3
#define CH_SELECT   4
#define CH_NEXT     5

// Emoticons
byte locked[8] = {
  0b00000,
  0b01110,
  0b10001,
  0b10001,
  0b11111,
  0b11111,
  0b11111,
  0b00000
};

byte opened[8] = {
  0b01110,
  0b10001,
  0b10001,
  0b00001,
  0b11111,
  0b11111,
  0b11111,
  0b00000
};

byte person[8] = {
  0b01110,
  0b11111,
  0b01110,
  0b00100,
  0b01110,
  0b11111,
  0b11111,
  0b11111
};

byte sel[8] = {
  0b00000,
  0b00001,
  0b00010,
  0b10010,
  0b01100,
  0b01100,
  0b00000,
  0b00000
};

byte next[8] = {
    0b00000,
  0b00000,
  0b00100,
  0b00110,
  0b11111,
  0b00110,
  0b00100,
  0b00000
};

// variables
LiquidCrystal_I2C lcd(0x27,16,2);
int curMode = MODE_WELCOME;
int idleTimer = 0;
FPS_GT511C3  fps(2, 4);
Servo locker;

// Utility functions

// open the door with emoticon
void openDoor(){
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.write(CH_OPEN);
 
 
  pinMode(6,OUTPUT);
  locker.write(90);
  lcd.print("Open.....");
  lcd.setCursor(0,0);
  lcd.write(CH_LOCK);
  lcdprint(1, "Door opened!");
  delay(3000);
  locker.write(0);
  lcd.print("Close.....");
  lcd.setCursor(0,0);
  lcd.write(CH_LOCK);
  lcdprint(1, "Door closed!");
  delay(3000);
 
  pinMode(6,INPUT);

  //lcd.print(" Open..       ");
        Serial.println("Open door");
}

// get enrolled id
int getNextId(int cur){
  int i = 0;
  for(i = cur+1; i < MAX_ID; i++){
    if( fps.CheckEnrolled(i) ) return i;
  }
  for(i = 0; i < cur; i++){
    if( fps.CheckEnrolled(i) ) return i;
  }
  return -1;
}


int getAvailableId(int cur){
  int i = 0;
  for(i = cur + 1; i < MAX_ID; i++){
    if( !fps.CheckEnrolled(i) ) return i;
  }
  for(i = 0; i < cur; i++){
    if( !fps.CheckEnrolled(i) ) return i;
  }
  return -1;
}


void checkMotion(){
  if( digitalRead(MOTION_SENSOR) == HIGH ){
  idleTimer = IDLE_TIME;
  }
}

void lcdprint(int row, char* msg){
  lcd.setCursor(0, row);
  int len = strlen(msg);
  int i = 0;
 
  lcd.print(msg);
 
  for(i=len-1;i<16;i++){
    lcd.print(" ");
  }
  lcd.setCursor(len, row);
}


// main routines
void setup(){
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.print("Make emotions");
  lcd.createChar(1, locked);
  lcd.createChar(2, opened);
  lcd.createChar(3, person);
  lcd.createChar(4, sel);
  lcd.createChar(5, next);
 
  lcd.print("LCD initialize");
  delay(2000);
  lcd.begin(16, 2);
  lcd.setCursor(0,1);
   
  lcd.print("Pin mode setting");
  delay(2000);
  pinMode(BTN_MODE, INPUT_PULLUP);
  pinMode(BTN_SELECT, INPUT_PULLUP);
  pinMode(BTN_NEXT, INPUT_PULLUP);
 
  pinMode(MOTION_SENSOR, INPUT);
 
  pinMode(BACKLIGHT, OUTPUT);
  pinMode(DOOR, OUTPUT);
 
  delay(100);

  lcd.setCursor(0,0);
  lcd.print("Finger print");
  lcd.setCursor(0,1);
  lcd.print("scanner init...");
  delay(2000);
  fps.UseSerialDebug = true;
  fps.Open();
  fps.SetLED(false);
  delay(2000); // 추가한부분

  locker.attach(6);
  pinMode(6,OUTPUT);
  locker.write(0);
  delay(3000);
  pinMode(6,INPUT);
 
  
  lcd.clear();
  lcd.print("Ready");
  delay(2000);
  digitalWrite(DOOR, HIGH);
  changeMode(MODE_IDLE);
}

void enroll(){
  int enrollCount = 0;
  enrollCount = fps.GetEnrollCount();
 
  lcd.setCursor(11, 0);
  lcd.print("(");
  lcd.print(enrollCount);
  lcd.print(")");
 
  lcd.setCursor(0,1);
  lcd.write(CH_SELECT);
  lcd.print(" to start");
  while(true){
    idleTimer--;
    if( idleTimer <= 0 ){
      changeMode(MODE_IDLE);
      return;
    }
    if( digitalRead(BTN_MODE) == HIGH ){
      changeMode(MODE_DEROLL);
      delay(100);
      return;
    }
    if( digitalRead(BTN_SELECT) == HIGH){
      break;
    }
    checkMotion();
    delay(10);
  }
 
  // enrolling
  int enrollId = -1;
  int i;
  bool used = true;
  bool bret = false;
  // find enroll id
  lcdprint(1, "Looking slot");
  for(i = 0; i < 200; i++){
    used = fps.CheckEnrolled(i);
    if( !used ){
      enrollId = i;
      break;
    }
   
  }
 
  if( enrollId == -1 ){
    lcdprint(1, "Slots full");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }
 
  fps.EnrollStart(enrollId);
  lcdprint(1, "Put finger (1/3)");
  while(fps.IsPressFinger() == false){
    if( digitalRead(BTN_MODE) == HIGH ){
      fps.Close();
      fps.Open();
      changeMode(MODE_DEROLL);
    }
  }
 
  bret = fps.CaptureFinger(true);
  if( bret == false ){
    lcdprint(1, "Capture fail!");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }
  lcdprint(1, "Remove finger");
  fps.Enroll1();
  while(fps.IsPressFinger() == true) delay(100);
  lcdprint(1, "Put finger (2/3)");
  while(fps.IsPressFinger() == false){
    if( digitalRead(BTN_MODE) == HIGH ){
      changeMode(MODE_DEROLL);     
    }
  }
 
  bret = fps.CaptureFinger(true);
  if( bret == false ){
    lcdprint(1, "Capture fail!");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }
  lcdprint(1, "Remove finger");
  fps.Enroll2();
  while(fps.IsPressFinger() == true) delay(100);
  lcdprint(1, "Put finger (3/3)");
  while(fps.IsPressFinger() == false){
    if( digitalRead(BTN_MODE) == HIGH ){
      changeMode(MODE_DEROLL);   
    }
  }
  bret = fps.CaptureFinger(true);
  if( bret == false ){
    lcdprint(1, "Capture fail!");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }
  lcdprint(1, "Remove finger");
  int iret;
  iret = fps.Enroll3();
  if( iret == 0 ){
    // 여기서 iret를 웹서버로 전송을 한다.
    lcdprint(1, "Enroll OK : ");
    lcd.write(CH_PERSON);
    lcd.print(enrollId);
  }else{
    lcdprint(1, "Enrolling failed.");
  } 
  delay(1000);
  changeMode(MODE_WELCOME);
}

void changeMode(int mode){
  lcd.clear();
  if ( mode != MODE_IDLE ){
    idleTimer = IDLE_TIME;
    lcd.backlight();
    fps.SetLED(true);
  lcd.display();
    Serial.println("Idle mode");
  }else{
    idleTimer = 0;
    lcd.noBacklight();
    fps.SetLED(false);
  lcd.noDisplay();
    Serial.print("Mode change to ");
    Serial.println(mode);
  }
  switch(mode){
    case MODE_WELCOME:
      lcd.backlight();
      lcd.setCursor(0,0);
      lcd.write(CH_LOCK);
      lcd.print(" Welcome!");
      lcd.setCursor(0,1);
      lcd.print("Put your finger");
    break;
    case MODE_ENROLL:
      lcd.setCursor(0,0);
      lcd.write(CH_PERSON);
      lcd.print(" ADD MODE");
    break;
    case MODE_DEROLL:
      lcd.setCursor(0,0);
      lcd.write(CH_PERSON);
      lcd.print(" REMOVE MODE");
    break;
  }
  curMode = mode;
  delay(100);
}

// Idle mode process
void idle(){
  lcd.noBacklight();
  if( digitalRead(BTN_MODE) == HIGH || digitalRead(MOTION_SENSOR) == HIGH){
    lcd.backlight();
    changeMode(MODE_WELCOME);
    return;
  }
  if( digitalRead(BTN_SELECT) == HIGH ){
    idleTimer = IDLE_TIME;
    openDoor();
    changeMode(MODE_IDLE);
    return;
  }
  delay(100);
}

// Welcome mode process
void welcome(){
  // change mode to the idle without no action while IDLE_TIME
  idleTimer--;
  if( idleTimer <= 0 ){
    changeMode(MODE_IDLE);
    lcd.noBacklight();
    return;
  }
 
  // if the mode button pressed
  if( digitalRead(BTN_MODE) == HIGH ){
    changeMode(MODE_ENROLL);
    return;
  }
 
  // if the select button pressed
  if( digitalRead(BTN_SELECT) == HIGH ){
    idleTimer = IDLE_TIME;
    openDoor();
    changeMode(MODE_WELCOME);
    return;
  }
 
  //if finger pressed
  if( fps.IsPressFinger() ){
    fps.CaptureFinger(false);
    fps.SetLED(false);
    int id = fps.Identify1_N();
    if( id < 200 ){
      lcd.setCursor(0,1);
      lcd.write(CH_PERSON);
      lcd.print(id);
      lcd.print(" verified!");
      openDoor();
     
      changeMode(MODE_WELCOME);
      return;
    }else{
      idleTimer = IDLE_TIME;
      lcdprint(1, "Who are you?");
      delay(2000);
      fps.SetLED(true);
      lcdprint(1, "Put your finegr");
    }
  }

  checkMotion();

  delay(10);
}

void deroll(){
  // press select button to start
  lcdprint(1, "  ");
  lcd.setCursor(0,1);
  lcd.write(CH_SELECT);
  lcd.print(" to start");
  while(true){
    idleTimer--;
    if( idleTimer <= 0 ){
      changeMode(MODE_IDLE);
      return;
    }

    if( digitalRead(BTN_MODE) == HIGH ){
      changeMode(MODE_WELCOME);
      return;
    }

    if( digitalRead(BTN_SELECT) == HIGH){
      break;
    }
          checkMotion();
    delay(10);
  }

  // if user press the button
  // first of all, get the enrolled user count
  int enrollCount = 0;
  enrollCount = fps.GetEnrollCount();

  // this mode needs to 1 or more ids
  if( enrollCount <= 0 ){
    lcdprint(1, "No user");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }

  int cur = -1;
  cur = getNextId(cur);

  if( cur == -1 ){
    lcdprint(1, "No user");
    delay(1000);
    changeMode(MODE_WELCOME);
    return;
  }

  lcdprint(0, " ");
  lcd.setCursor(0,0);
  lcd.write(CH_NEXT);
  lcd.print(" to next");
  lcdprint(1, " ");
  lcd.setCursor(0,1);
  lcd.write(CH_SELECT);
  lcd.print(" to remove ");
  lcd.write(CH_PERSON);
  lcd.print(cur);
  while(true){
    if( digitalRead(BTN_NEXT) == HIGH ){
      cur = getNextId(cur);
      lcdprint(1, " ");
      lcd.setCursor(0,1);
      lcd.write(CH_SELECT);
      lcd.print(" to remove ");
      lcd.write(CH_PERSON);
      lcd.print(cur);
    }
 
     if(digitalRead(BTN_SELECT) == HIGH ){
      bool bret = false;
      bret = fps.DeleteID(cur);
      if( bret ){
        lcdprint(1, " ");
        lcd.setCursor(0,1);
        lcd.write(CH_PERSON);
        lcd.print(cur);
        lcd.print(" Deleted!");
        delay(1000);
        changeMode(MODE_WELCOME);
        return;
      }else{
        lcdprint(1, "error..");
        delay(1000);
        changeMode(MODE_WELCOME);
        return;
    }
    if(digitalRead(BTN_MODE) == HIGH){
      changeMode(MODE_WELCOME);
      return;
    }
    checkMotion();
    delay(10);
  }
  }
  changeMode(MODE_WELCOME);
 
}

void loop(){
  switch(curMode){
    case MODE_IDLE:
      idle();
    break;
    case MODE_WELCOME:
      welcome();
    break;
    case MODE_ENROLL:
      enroll();
    break;
    case MODE_DEROLL:
      deroll();
    break;
   
  }
}

정말.... 되게만 해주시면.... 이은혜 잊지 않겠습니다. ㅜㅜㅜㅜㅜ

이전글   |    오렌지 보드 BLE 질문입니다. 2015-09-16
다음글   |    Weather Cube [PART2] 에서 쓰인 부품 2015-09-20