![見出し画像](https://assets.st-note.com/production/uploads/images/172326238/rectangle_large_type_2_bf7a9a48478f8c799b290b67f6ddc479.jpeg?width=1200)
ELECOMのBluetoothシャッターリモコンをラズパイで使う
概要
DAISOのシャッターリモコンをラズパイのスイッチとして使うための情報はWeb上に多数あり、自分も実践したことがありました。
今回、ある目的のために同様のリモコンが必要になったのですが、ボタンが1つだけのシンプルなものがよかったので、探したところELECOMのものが見つかり、不安を感じつつ試してみたところ、ちゃんと使えたので共有します。
Bluetooth自撮りリモコン P-SRBBK
購入したのはELECOMの「Bluetooth自撮りリモコン P-SRBBK」。
ELECOM Direct Shopで858円です。
https://shop.elecom.co.jp/item/4953103305977.html
![](https://assets.st-note.com/img/1738239493-xNuk1GDhXecfJYw6BbHKTCzU.jpg?width=1200)
![](https://assets.st-note.com/img/1738239532-lChzkQ8egPoNSJ39EdncMZ6v.jpg?width=1200)
円形のミニマルなデザインかつ小型のところに惹かれました。
ペアリング
通常のBluetoothデバイスなので、手順については多数紹介され尽くしているので、ここでは省略します。
ペアリングの時に必要となるデバイスIDは 0C:FC:83:xx:xx:xx になりますので参考にしてください。
デバイス名は「BTselfie E」です。
Pythonから使う
evdevライブラリを使うので、インストールします。
$ pip install evdev
最初に動作確認をしたところ、ボタンを1回押しただけなのに、なぜか Press -> Release -> Press -> Release と2回検知される現象が発生。
詳細にイベントを調べたところ、ボタンを押下した時に 28 (KEY_ENTER) と 115 (KEY_VOLUMEUP) という2つのイベントを送信していることがわかりました。
なので判定ロジックではひとつのイベント(28)だけを処理対象とすることで対応。
import evdev
from evdev import InputDevice, ecodes
import time
class ButtonMonitor:
def __init__(self):
self.device = None
self.device_path = None
def find_bluetooth_button(self):
# 保存されたパスがあればそれを試す
if self.device_path:
try:
device = InputDevice(self.device_path)
if "BTselfie" in device.name:
return device
except:
self.device_path = None
# 新規検索
devices = [InputDevice(path) for path in evdev.list_devices()]
for device in devices:
if "BTselfie" in device.name:
self.device_path = device.path
return device
return None
def monitor(self):
while True:
if not self.device:
self.device = self.find_bluetooth_button()
if not self.device:
print("BTselfie device not found")
time.sleep(1)
continue
print(f"Found device: {self.device.name}")
try:
for event in self.device.read_loop():
if event.type == ecodes.EV_KEY and event.code == 28:
if event.value == 1:
print("Button pressed")
elif event.value == 0:
print("Button released")
except OSError as e:
if e.errno == 19:
print("Device disconnected")
self.device = None
continue
if __name__ == "__main__":
monitor = ButtonMonitor()
monitor.monitor()
動作テスト
動作させるために管理者権限を付加する説明が多いのですが、ユーザー権限でもinputグループに属することで動作可能です。
$ sudo usermod -a -G input $USER
上記コマンドで所属グループを変更したら、ログアウト/ログインして有効にしてください。
idコマンドでinputグループが表示されていることを確認。
$ id
uid=1000(ryo) gid=1000(ryo) groups=1000(ryo),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),102(input),106(netdev),114(bluetooth),117(lpadmin),120(scanner)
あとは普通にプログラムを起動すればOKです。
いいなと思ったら応援しよう!
![R-Y-O](https://assets.st-note.com/production/uploads/images/55212934/profile_7a6381aeff3ebe51b3fd0d85ee2c2849.png?width=600&crop=1:1,smart)