Raspberry Pi Zero WにBluetoothデバイスを接続する方法

Raspberry Pi Zero WにBluetoothデバイスを接続する方法

タグ
IoT学習
公開日
March 9, 2023

image
image

開発環境

Raspberry Pi Zero W

※購入時(2023/1/16)は¥3,980でした。

A&D Bluetooth内蔵 体温計 UT-201BLE

概要

Raspberry PI Zero Wを使ってBluetooth対応体温計のデータを取得する。

下準備

OSをSDカードに焼く

image

image
image

「OSを選ぶ」→「Raspberry Pi OS(other)」→「Raspberry Pi OS Lite(32-bit)」

初期設定

image
image

「歯車マーク」→「詳細な設定」

※これを設定しておくと自動的にWi-Fiに接続されて便利。

オペレーティングシステム

$ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)"
NAME="Raspbian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

起動

image

SSH接続

初期設定にてWi-Fiに接続出来る状態であればSSHコマンドにてラズパイZeroWにSSH接続可能。

ssh pi@raspberry.local
raspberry.local's password:
Linux opening 5.15.84+ #1613 Thu Jan 5 11:58:09 GMT 2023 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

キーボード入力の修正

(リブートが必要かも)

echo "set nocompatible" >> /etc/skel/.vimrc
echo "set backspace=indent,eol,start" >> /etc/skel/.vimrc
sudo echo "set nocompatible" >> /root/.vimrc
sudo echo "set backspace=indent,eol,start" >> /root/.vimrc
echo "set nocompatible" >> /home/pi/.vimrc
echo "set backspace=indent,eol,start" >> /home/pi/.vimrc

1.ペアリングデバイスの検索

bluetoothctlのコマンド一覧

piユーザー(初期)をbluetoothグループに追加

$ sudo usermod -a -G bluetooth pi
$ sudo reboot

2.繋ぎたいデバイスのBluetoothアドレスを特定

リブート後、bluetoothctlコマンドを実行

$ bluetoothctl
[bluetooth]# scan on

※scan onは自分で入力

以下のようにデバイスが列挙される。

[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:13:4A:0D Discovering: yes
[NEW] Device 6C:17:F8:88:CE:C5 6C-17-F8-88-CE-C5
image

沢山出て来たら、目当ての品を探す。

xx:xx:xx:xx:xx:xxという書かれ方をしている文字列が、各Bluetooth機器のMACアドレス

3.ペアリングする

image

pair MACアドレスで接続する

pair 64:33:DB:D8:AF:D2

Enterを押下して、Pairing successfulと表示されたらあとはconnect MACアドレスと打てば成功。

image
image

最後にtrust MACアドレスにて信頼できるデバイスとして追加しておけば次回以降この手間が省けるので便利。

今回は以上です。