PHYSIs Beacon 모듈 - iBeacon 예제

Beacon 모듈의 경우, 아두이노와 모듈 간의 소프트웨어 통신을 활용하여 설정합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <PHYSIs_Master.h>                  // PHYSIs_Master 라이브러리 추가
 
PHYSIs_Beacon beacon(65);                // 비콘 객체 선언 (Rx, Tx)
 
void setup() {
  Serial.begin(115200);
 
  if (beacon.begin()) {                     // 비콘 모듈 연결    
    beacon.init();                                  // 모듈 고장 초기화
    delay(1000);
    beacon.reboot();                                // 모듈 재시작
    delay(1000);
    if (beacon.isConnected()) {                     // 모듈 연결 확인      
      delay(1000);
      Serial.print("iBeacon Address : ");
      Serial.println(beacon.getAddress());          // 모듈 MAC 주소 출력
      delay(1000);
      beacon.setADInterval('5');                    // 신호 출력 간격 설정 ( '1 '~ 'F' )
      delay(1000);
      beacon.setDeviceName("P_Beacon");             // 디바이스 이름 설정
      delay(1000);
      beacon.setBeaconMode();                       // 비콘 모드 설정
      delay(1000);
      beacon.setOnlyBroadcast();                    // 단일 신호 출력 모드 설정
      delay(1000);
      beacon.setAutoSleepStatus(true);              // 오토 슬립 모드 설정
      delay(1000);
      beacon.reboot();                              // 모듈 재시작
    }
  }
}
 
void loop() {
 
}
cs

PHYSIs 메이커보드 - Beacon Scanner(도난 방지기) 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <PHYSIs_Master.h>
#include <Adafruit_NeoPixel.h>        // LED 라이브러리 추가
 
#define BUZZER_PIN    4               // 부저 연결 Pin 번호
#define LED_PIN       2               // LED Ring 연결 Pin 번호
#define LED_PIXELs_SIZE   12          // LED Ring 픽셀 수
 
// 객체 선언
PHYSIs_Beacon beacon(109); // Rx, Tx
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(LED_PIXELs_SIZE, LED_PIN, NEO_GRB + NEO_KHZ800);
 
String targetAddr = "C8FD198E4D15";          // 탐색 대상 비콘 주소값
uint32_t distColor;                          // 거리 상태 색상
int nonfindCnt = 0;                          // 검색 오류 카운트
 
long waringTime = 0;
 
void setup() {
  Serial.begin(115200);
  pixels.begin();                                      // LED 링 모듈 활성화
  pixels.setBrightness(75);
 
  if (beacon.begin()) {                                // 비콘 모듈 연결
    beacon.init();                                        // 모듈 고장 초기화
    delay(1000);
    beacon.reboot();                                      // 모듈 재시작
    delay(1000);
    if (beacon.isConnected()) {                           // 모듈 연결 확인
      beacon.setBeaconScanner();                          // 비콘 스캔 모드 설정
      beacon.beaconScanResult = &beaconScanResult;        // 비콘 검색 결과 콜백 함수 지정
    }
  }
}
 
void loop() {
  beacon.beaconDiscovery();                     // 비콘 검색
 
 
  if (nonfindCnt > 5)                           // 대상 비콘이 5회 이상 검색되지 않으면
  {
    if (millis() - waringTime > 1000) {                               // 1초 마다 부저음 출력
      digitalWrite(BUZZER_PIN, !digitalRead(BUZZER_PIN));
      waringTime = millis();
    }
  }
}
 
// 비콘 검색 결과 콜백 함수
void beaconScanResult(String addr, int rssi) {
  Serial.print(addr);
  Serial.print(", ");
  Serial.println(rssi);
  if (addr.equals(targetAddr)) {                      // 대상 비콘이 검색될 경우,
    nonfindCnt = 0;                                         // 검색 오류 카운트 초기화
    showStatusLed(rssi);                                    // 비콘 거리 상태 LED 출력
  } else {                                            // 대상 비콘이 아닌 경우,
    nonfindCnt++;                                           // 검색 오류 카운트 증가
  }
}
 
// 비콘 거리 상태 LED 출력
void showStatusLed(int rssi) {
  distColor = rssi > -50 ? pixels.Color(02550) : pixels.Color(2552550);
  for (int i = 0; i < pixels.numPixels(); i++) {
    pixels.setPixelColor(i, distColor);
  }
  pixels.show();
}
 
cs

+ Recent posts