Hello, this is @dz_ this and Ohira Kazumi of oak fan.
I investigated how to handle Bluetooth so that the Raspberry Pi can use Bluetooth to acquire data from the sensor device.
I started this trial with Raspberry Pi Zero, but I couldn't install the necessary libraries due to lack of memory, so I decided to proceed with Raspberry Pi 2 Model B.
The Raspberry Pi used this time does not have Bluetooth, so use a USB adapter.
It seems that PLANEX products are often used. When I went to a certain electric shop, I didn't have PLANEX, so I bought the cheapest PRINCETON one (laughs). It works without any problems.
-PLANEX BT-Micro4 -PRINCETON PTM-UBT7
Use SensorTag CC2650 equipped with 10 types of sensors. It can be connected via Bluetooth, and it seems to be easy to handle because there are sample code and user information here and there.
I'm still studying, but it seems that the following mechanism will be used to handle Bluetooth on Linux.
GATT (Generic Attribute Profile) --A standardized framework for data transfer and storage based on the Attribute Protocol (ATT) --Reference: Bluetooth Story (2) | Wireless ・ Oto | Silex Technology, Inc.
As you will learn a lot about difficult things, first, refer to RPi Bluetooth LE --eLinux.org from the command line. I tried to check the operation.
#Check the connected USB device
$ lsusb
...
Bus 001 Device 011: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
...
#Check the status of the Bluetooth adapter
$ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: XX:XX:XX:XX:XX:XX ACL MTU: 310:10 SCO MTU: 64:8
UP RUNNING
RX bytes:610 acl:0 sco:0 events:36 errors:0
TX bytes:942 acl:0 sco:0 commands:36 errors:0
#If above`DOWN`Is displayed, enable it with the following command
$ sudo hciconfig hci0 up
#Scan nearby Bluetooth devices
$ sudo hcitool lescan
LE Scan ...
XX:XX:XX:XX:XX:XX (unknown)
XX:XX:XX:XX:XX:XX CC2650 SensorTag
...
SensorTag was displayed in the scan result. It seems that you can recognize it safely.
Now, let's try using Bluetooth programmatically.
As for the language, I will try using Python, which is a major language in the Raspberry Pi area, after reviewing it. As a library for handling Bluetooth, I will introduce pybluez
.
pybluez
Please refer to the following for the installation itself. Since gattlib
is also required to handle BLE compatible devices with pybluez
, install it as well.
#Install pyBluez dependent packages
$ sudo apt-get install python-dev libbluetooth3-dev
#Install pyBluez
$ sudo pip install pybluez
#Install gattlib dependent packages
$ sudo apt-get install libglib2.0 libboost-python-dev libboost-thread-dev
#Install gattlib required when using BLE
$ sudo pip install gattlib
Now let's look for SensorTag in python.
SensorTag supports BLE, so you can scan with this sample code.
Also, to get the data, this sample code will be helpful.
As you may have already noticed, pybluez actually uses gattlib via pybluez when dealing with BLE compatible devices, so the documentation for gattlib itself is also helpful.
In GATT, data is acquired and set using an address (?) Called a "handle". The GATT table of SensorTag is in "Sensors and Services" of Disassemble | Simplelink Sensor Tag --TI Please refer to it as it is posted.
Now you can get your favorite sensor data! ??
(Actually, the acquired data requires conversion of LSB, MSB, etc.)
It was a pity that I couldn't get it to work on my favorite Raspberry Pi Zero, but now I'm ready to use Bluetooth from my Raspberry Pi 2!
I will continue to move forward!
The following is about troubleshooting.
pybluez
Installation troubleshootingPython.h
is not foundIf you get the error fatal error: Python.h: No such file or directory
, you need to install python-dev
.
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno
-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-se
curity -fPIC -I./port3 -I/usr/include/python2.7 -c bluez/btmodule.c -o build/temp.linux-arm
v6l-2.7/bluez/btmodule.o
In file included from bluez/btmodule.c:20:0:
bluez/btmodule.h:4:20: fatal error: Python.h: No such file or directory
#include "Python.h"
^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-4nXTDu
/pybluez/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\
r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-fWWiRf-record/install-record.txt
--single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-
4nXTDu/pybluez/
$ sudo apt-get install python-dev
bluetooth / Bluetooth.h
is not foundFatal error: bluetooth / bluetooth.h: No such file or directory as below If you get the error
, you need to install libbluetooth3-dev
.
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno
-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-se
curity -fPIC -I./port3 -I/usr/include/python2.7 -c bluez/btmodule.c -o build/temp.linux-arm
v6l-2.7/bluez/btmodule.o
In file included from bluez/btmodule.c:20:0:
bluez/btmodule.h:5:33: fatal error: bluetooth/bluetooth.h: No such file or directory
#include <bluetooth/bluetooth.h>
^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-OWwZct
/pybluez/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\
r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-_zZYsg-record/install-record.txt
--single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-
OWwZct/pybluez/
$ sudo apt-get install libbluetooth3-dev
gattlib
Installation troubleshootingboost / python / list.php
is not foundIf you get the error fatal error: boost / python / list.hpp: No such file or directory
, you need to install libboost-python-dev
.
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno
-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-se
curity -fPIC -DVERSION="5.25" -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2
.0/include -Isrc/bluez -I/usr/include/python2.7 -c src/gattservices.cpp -o build/temp.linux
-armv6l-2.7/src/gattservices.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not
for C++
In file included from src/gattservices.cpp:12:0:
src/gattlib.h:11:33: fatal error: boost/python/list.hpp: No such file or directory
#include <boost/python/list.hpp>
^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-lIdNTN
/gattlib/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\
r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-h_pTpQ-record/install-record.txt
--single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-
lIdNTN/gattlib/
$ sudo apt-get install libboost-python-dev
If you get the error No package'glib-2.0' found
, as shown below, you need to install libglib2.0
.
$ sudo pip install gattlib
Collecting gattlib
Downloading gattlib-0.20150805.tar.gz (1.7MB)
100% |████████████████████████████████| 1.7MB 86kB/s
Complete output from command python setup.py egg_info:
Package glib-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `glib-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'glib-2.0' found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-ix7ocM/gattlib/setup.py", line 12, in <module>
"pkg-config --cflags glib-2.0".split()).decode('utf-8')
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['pkg-config', '--cflags', 'glib-2.0']' returne
d non-zero exit status 1
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ix7ocM/gattli
b/
$ sudo apt-get install libglib2.0
-lboost_thread
is not foundIf you get the error cannot find -lboost_thread
, as shown below, you need to install libboost-thread-dev
.
c++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -
DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-s
trong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protecto
r-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/src/gattservices.o bu
ild/temp.linux-armv7l-2.7/src/beacon.o build/temp.linux-armv7l-2.7/src/bindings.o build/tem
p.linux-armv7l-2.7/src/gattlib.o build/temp.linux-armv7l-2.7/src/bluez/lib/uuid.o build/tem
p.linux-armv7l-2.7/src/bluez/attrib/gatt.o build/temp.linux-armv7l-2.7/src/bluez/attrib/gat
trib.o build/temp.linux-armv7l-2.7/src/bluez/attrib/utils.o build/temp.linux-armv7l-2.7/src
/bluez/attrib/att.o build/temp.linux-armv7l-2.7/src/bluez/src/shared/crypto.o build/temp.li
nux-armv7l-2.7/src/bluez/src/log.o build/temp.linux-armv7l-2.7/src/bluez/btio/btio.o -lglib
-2.0 -lboost_python -lboost_thread -lbluetooth -o build/lib.linux-armv7l-2.7/gattlib.so
/usr/bin/ld: cannot find -lboost_thread
collect2: error: ld returned 1 exit status
error: command 'c++' failed with exit status 1
$ sudo apt-get install libboost-thread-dev
At the time of this error, the prompt does not come back easily, and if you think that it has returned, this error is displayed.
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-s
trict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-secu
rity -fPIC -DVERSION="5.25" -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0
/include -Isrc/bluez -I/usr/include/python2.7 -c src/bindings.cpp -o build/temp.linux-armv6
l-2.7/src/bindings.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not f
or C++
arm-linux-gnueabihf-gcc: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.9/README.Bugs> for instructions.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 4
This seems to be out of memory. The Raspberry Pi Zero I was trying out didn't allow me to install gattlib anymore, but with the Raspberry Pi 2 Model B I was able to install gattlib without this error.
Recommended Posts