11
14

More than 5 years have passed since last update.

ビーコンを用いて入退室をチェックする簡単なプログラム

Last updated at Posted at 2019-03-09

はじめに

ビーコンの一種であるEddyStonまたはiBeaconの入退室を検出する処理をPythonで実現します。ビーコンの作り方は小型 Bluetooth LE モジュール AE-TYBLE16とボタン電池でビーコンを作るを参照してください。

Pythonのセットアップ

pip install setuptools
pip install beacontools
apt-get install libbluetooth-dev
pip install pybluez

ソースコード

下記をチェックアウトしてください。
https://github.com/MypaceEngine/beacontools

動作方法

exampleフォルダに移動し、下記を実行してください。

Beaconのタイプ ファイル
Eddystone(指定Namespaceのみ) sudo python3 scanner_eddystone_inout_example.py
iBeacon(指定UUIDのみ) sudo python3 scanner_ibeacon_inout_example.py
iBeaconとEddystoneの両方(全Beaconを検出) sudo python3 scanner_eddystone_ibeacon_inout_example.py

検出するBeaconの設定

検出するBeaconを指定する必要があります。Filterを除去すれば、EddyStone/iBeaconの両方とも受信できるようになります。

Eddstoneの場合

scanner_eddystone_inout_example.py
# scan for all TLM frames of beacons in the namespace "12345678901234678901"
scanner = BeaconScanner(callback, 
    device_filter=EddystoneFilter(namespace="12345678901234678901"),
    packet_filter=[EddystoneTLMFrame, EddystoneUIDFrame]
)

iBeaconの場合

scanner_ibeacon_inout_example.py
# scan for all iBeacon advertisements from beacons with the specified uuid 
scanner = BeaconScanner(callback, 
    device_filter=IBeaconFilter(uuid="e5b9e3a6-27e2-4c36-a257-7698da5fc140")
)

退室から検出までの時間を変更する方法

現在は30秒で実装されています。変更の際には下記を変更してください。

            #Wait 30s for detecting to exit beacon.
            if(time.perf_counter()-bt_list[i]>30):

動作結果

iBeaconの場合 (scanner_ibeacon_inout_example.py)

イベント 出力ログ
入室 <IN> UUID:beac0712-3456-789a-bcde-f12345678907 Major:0 Minor:0
検出中 <Listing> UUID:beac0712-3456-789a-bcde-f12345678907 Major:0 Minor:0 Timeout:30
退室 <OUT> UUID:beac0712-3456-789a-bcde-f12345678907 Major:0 Minor:0

Eddtstonの場合 (scanner_eddystone_inout_example.py)

イベント 出力ログ
入室 <IN> namespace:01020304050607080910 Instance:aabbccddeef
検出中 <Listing> namespace:01020304050607080910 Instance:aabbccddeef Timeout:30
退室 <OUT> namespace:01020304050607080910 Instance:aabbccddeef

Eddystone/iBeaconの両方の場合(scanner_eddystone_ibeacon_inout_example.py)

イベント 出力ログ
入室 <IN> Type:Eddystone Adress: cc:39:f7:20:b1:62 Namespace:01020304050607080910 Instance:aabbccddeeff
<IN> Type:iBeacon Adress: f2:0d:7e:2f:flag_ac:21 UUID:beac0712-3456-789a-bcde-f12345678902 Major:0 Minor:0
検出中 <Listing> Type:Eddystone Adress: cc:39:f7:20:b1:62 Namespace:01020304050607080910 Instance:aabbccddeeff
<Listing> Type:iBeacon Adress: f2:0d:7e:2f:flag_ac:21 UUID:beac0712-3456-789a-bcde-f12345678902 Major:0 Minor:0
退室 <OUT> Type:Eddystone Adress: cc:39:f7:20:b1:62 Namespace:01020304050607080910 Instance:aabbccddeeff
<OUT> Type:iBeacon Adress: f2:0d:7e:2f:flag_ac:21 UUID:beac0712-3456-789a-bcde-f12345678902 Major:0 Minor:0
11
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
14