본문 바로가기
실전 임베디드

Raspberry PI BLE(Bluetooth Low Energy) 설정. 3. 24. 10:24

by BABEL-II 2019. 9. 24.

1. 라즈비안 최신 버전으로 굽는다... 실시. 이것은 필수

 

2. 최신 버전으로 굽고 나면 ssh가 서비스가 안 된다. 살려주자.

    sudo update-rc.d ssh defaults
    sudo update-rc.d ssh enable

 

3. 일단 참조 사이트

http://www.elinux.org/RPi_Bluetooth_LE

RPi Bluetooth LE - eLinux.org

www.elinux.org

라즈비안 최신버전

http://downloads.raspberrypi.org/raspbian/images/raspbian-2014-01-09/2014-01-07-wheezy-raspbian.zip

버전 체크

uname -a

Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l GNU/Linux

Hardware

CSR chipset adapter http://www.amazon.co.uk/Version-Bluetooth-Adapter-Compatible-Windows/dp/B00A0CBOTE

BlueZ 설치

일단 개발 환경 설치

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev make

블루젯 설치, 아래 파일의 버전은 소스에 가 보면 다른 버전이 있다.mkdir -p work/bluepy cd work/bluepy wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xz tar xvf bluez-5.32.tar.xz

If successful, you'll now have a bluez-5.32 source code directory on disk. To build it, do:

cd bluez-5.32 ./configure --disable-systemd make sudo make install

First tests

USB 점검

lsusb

With the Plugable adapter shown above, the result will be similar to this:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. Bus 001 Device 004: ID 0a5c:21e8 Broadcom Corp.

The adapter is the one on Bus 001 Device 004. To show more information about the 'Broadcom' device, do:

sudo lsusb -v -d 0a5c:

Using hcitool

If this is working, run the hciconfig tool:

pi@raspberrypi ~ $ hciconfig hci0: Type: BR/EDR Bus: USB BD Address: 00:02:72:14:27:0E ACL MTU: 1021:8 SCO MTU: 64:1 DOWN RX bytes:2715 acl:1 sco:0 events:146 errors:0 TX bytes:2500 acl:0 sco:0 commands:133 errors:0

만약 DOWN 상태이면 UP으로 바꿔준다.

sudo hciconfig hci0 up

LE 장치 검색

sudo hcitool lescan

이러면 마구 마구 나온다.

root@raspberrypi:/home/pi# hcitool lescan
LE Scan ...
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 (unknown)
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 (unknown)
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 (unknown)
C7:53:F3:F7:83:49 Nordic_UART
C7:53:F3:F7:83:49 (unknown)

/* lecc는 할 필요 없다. -----

sudo hcitool lecc C7:53:F3:F7:83:49

*/

gatttool로 연결한다.

 

root@raspberrypi:/home/pi# gatttool -t random -b c7:53:f3:f7:83:49 -I
[c7:53:f3:f7:83:49][LE]> char-write-req 0x000e 31
Command Failed: Disconnected
[c7:53:f3:f7:83:49][LE]> connect
Attempting to connect to c7:53:f3:f7:83:49
Connection successful
[c7:53:f3:f7:83:49][LE]> char-write-req 0x000e 31
Characteristic value was written successfully
[c7:53:f3:f7:83:49][LE]> char-write-req 0x000e 32
Characteristic value was written successfully
[c7:53:f3:f7:83:49][LE]> char-read-req 2902
Error: char-read-req: command not found
[c7:53:f3:f7:83:49][LE]> char-read-uuid 2902
handle: 0x000c   value: 00 00
[c7:53:f3:f7:83:49][LE]> char-write-hnd 0x000c 0100
Error: char-write-hnd: command not found
[c7:53:f3:f7:83:49][LE]> char-write-req 0x000c 0100 <-- 노티피케이션을 살려준다.
Characteristic value was written successfully
Notification handle = 0x000b value: 31 32 33 0d 0d 0a
Notification handle = 0x000b value: 31 32 33 34 0a
Notification handle = 0x000b value: 35 36 37 38 39 30 0a
Notification handle = 0x000b value: 68 65 6c 6c 6f 0a
Notification handle = 0x000b value: 65 6b 61 20 77 6e 64 70 08 0a
[c7:53:f3:f7:83:49][LE]> exit
root@raspberrypi:/home/pi# gatttool -t random -b c7:53:f3:f7:83:49 -I
[c7:53:f3:f7:83:49][LE]> connect
Attempting to connect to c7:53:f3:f7:83:49
Connection successful
[c7:53:f3:f7:83:49][LE]> char-write-req 0x000c 0100
Characteristic value was written successfully
Notification handle = 0x000b value: 61 62 63 64 65 0a <== 디바이스 쪽 단말에서 입력한 게 노티로 날아온다.
Notification handle = 0x000b value: 31 32 33 34 35 0a
Notification handle = 0x000b value: 35 36 37 38 0a

Using Bluetooth LE from Python

You can communicate with Bluetooth LE devices from Python using the bluepy package. This is currently source code you need to build yourself; there isn't an official installer for it yet.

Fetch the source by changing directory to a suitable work area and running:

git clone https://github.com/IanHarvey/bluepy.git

This should create a directory bluepy/. If you have already fetched all the packages needed in BlueZ 5.30 Build Instructions' above, you can build the source with just:

cd bluepy/bluepy make

If successful you will have built an executable bluepy-helper.

To test this, first ensure that hcitool lecc works (see above) so you can manually create a connection, then run btle.py giving it your peripheral's MAC address, e.g.

python btle.py 00:1e:3f:22:4b:a7

This will attempt to enumerate the services and characteristics presented by the device.

There is currently no official documentation for the bluepy module; you will need to use the Python source as example code.

Using Bluetooth LE with Go (Golang)

Gatt is a Go package, which provides developers to create BLE applications for Linux and OS X.

Developers install Go language on the host machine, and cross-compile the applications for RPi.

The package accesses HCI devices directly via HCI sockets provided by BlueZ core (kernel space), so it doesn't require the BlueZ userland package.

To test the example programs (sample GATT server and clients):

Cross-compile the server example for an ARMv6 target device.

GOARCH=arm GOARM=6 GOOS=linux go build examples/server.go cp server <target device>

Start the server on the target device

sudo ./server

Cross-compile the client example (discoverer) for an ARMv6 target device.

GOARCH=arm GOARM=6 GOOS=linux go build examples/discoverer.go cp discoverer <target device>

Run the discoverer to scan surrounding peripheral devices.

sudo ./discoverer

Links: other Bluetooth Low Energy resources

 

Retrieved from "http://www.elinux.org/index.php?title=RPi_Bluetooth_LE&oldid=384651"