블루이노

스마트홈 도어락 응용예제

ZEROWIN.TECH 2020. 12. 30. 18:41
728x90

 

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

#define PIN_DOOR_LOCK 31

// receive event
#define PIN_PIR_DETOR   3
#define PIN_DOOR_OPEN   2

#define RBG_R 23 
#define RBG_G 35 
#define RBG_B 36 

#define PIN_DOOR_LOCK 31

LiquidCrystal_I2C lcd(0x27, 16, 2);

void printLCD(int col, int row , char *str) {
    for(int i=0 ; i < strlen(str) ; i++){
      lcd.setCursor(col+i , row);
      lcd.print(str[i]);
    }
}

int bell[] = {660,660,660,550,550,550,440,440};
int melody[] = {262,294,330,349,392,440,494,523};

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);  //Bluetooth Module

  lcd.init();
  lcd.backlight();

  printLCD(0, 0, "DOOR-LOCK");
  printLCD(0, 1, "CLOSE"); 

  pinMode(PIN_DOOR_LOCK, OUTPUT);

  digitalWrite(PIN_DOOR_LOCK, false);

}

int loop_count = 0;
int bell_pos = -1;
int melody_pos = -1;

int doorbell_timeout = -1;
int doorlock_timeout = -1;
int detect_timeout = -1;

#define R_MAXNUM 32 // 64
char rData[R_MAXNUM] = { 0x00, };
int rPos = 0;

void doorOpen()
{
        // sound melody
        melody_pos = 0;
        
        // doorlock open
        digitalWrite(PIN_DOOR_LOCK, true);
        doorlock_timeout = 40;

  printLCD(0, 0, "DOOR-LOCK       ");
  printLCD(0, 1, "OPEN            "); 

      Serial.println("OPEN");
      Serial1.println("OPEN");

}

void doorClose()
{
    printLCD(0, 0, "DOOR-LOCK       ");
    printLCD(0, 1, "CLOSE           "); 

    digitalWrite(PIN_DOOR_LOCK, false);
	
      Serial.println("CLOSE");
      Serial1.println("CLOSE");	
}

void loop()
{
   bool pir_state = digitalRead(PIN_PIR_DETOR);
   bool door_state = digitalRead(PIN_DOOR_OPEN);
   //Serial.print("door_state="); Serial.print(door_state);
   //Serial.print(", pir_state="); Serial.println(pir_state);

   if(doorbell_timeout == -1)
   {
     if(door_state) { 
      digitalWrite(RBG_G, HIGH);
      bell_pos = 0;
  
      printLCD(0, 0, "INPUT PASSWORD  ");
      printLCD(0, 1, "****            "); 
  
      Serial.println("DOORBELL");
      Serial1.println("DOORBELL");

      doorbell_timeout = 20;
     }
     else {
      digitalWrite(RBG_G, LOW);     
     }   
   }
   else {
    doorbell_timeout -= 1;
   }

   if(detect_timeout == -1)
   {
     if(pir_state) { 
      Serial.println("DETECT");
      Serial1.println("DETECT");
      
      detect_timeout = 40;
      
     }
     else {
     }
   }
   else {
      detect_timeout -= 1;
      if( detect_timeout % 2 == 0)
      {
        digitalWrite(RBG_R, HIGH);
      }
      else {
        digitalWrite(RBG_R, LOW);
      }
   }
   
   if(loop_count % 10 == 0) 
   {
	  Serial.println("");
      Serial1.println("");
   }

   if(loop_count % 2 == 0) 
   {
      if(0 <= bell_pos) { // bell sound
        tone(11, bell[bell_pos],500);
        bell_pos += 1;
        if(7 < bell_pos) bell_pos = -1;
      }

      if(0 <= melody_pos) { // melody sound
        tone(11, melody[melody_pos],500);
        melody_pos += 1;
        if(7 < melody_pos) melody_pos = -1;
      }

      if(0 <= doorlock_timeout)
      {
        doorlock_timeout -= 1;
        if(doorlock_timeout == 0)
        {
          doorClose();
        }
      }
   }

   if(0 < Serial.available())
   {
      String str = Serial.readString();
      Serial.println(str);

      // 패스워드가 맞음
      if(str.equals("1004") == true) {

        doorOpen();
        
      }
   }

   while(0 < Serial1.available()) {
      char ch = Serial1.read();
      rData[rPos] = ch;
      rPos += 1;
      // Serial.print(ch); // 문자열을 출력 
    
      if(ch == '\n')
      {
        Serial.println(rData); // 문자열을 출력 

        // 패스워드가 맞음
        if(memcmp(rData, "1004", 4) == 0) {
          doorOpen();
        }        

        rPos = 0;
        memset(rData, 0x00, R_MAXNUM);        
      }
   }
   
   delay(100);
   
   loop_count += 1;

   
}

시리얼모니터 디버그화면

블루투스 통신 (블루투스 터미널)

 

play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=ko&gl=US

 

Serial Bluetooth Terminal - Google Play 앱

'Serial Bluetooth Terminal' is a line-oriented terminal / console app for microcontrollers, arduinos and other devices with a serial / UART interface connected with a bluetooth to serial converter to your android device. This app supports different bluetoo

play.google.com

스마트폰앱 앱인벤터2

도어벨을 누르면 비밀번호를 입력합니다.

비밀번호가 맞으면 문이 열립니다.

사람 움직임을 감지합니다.

 

MMB_Smarthome_v01_2_door.zip
1.66MB

'블루이노' 카테고리의 다른 글

SMART-HOME GAS-DETECT  (0) 2020.12.31
AppInventor BluetoothLE 최신버전 적용  (0) 2020.12.11