RaspberryPI Zero W contains WLAN and Bluetooth with LE capability. This makes it perfect controlling BLE peripherals via the BLE-stack Bluez like FEEUs ClickBeetle (www.feeu.com/clickbeelte)
Bluez versions in the aptitude repository are pretty old. For this reason it is better to use Bluez from https://www.kernel.org/pub/linux/bluetooth/ and compile it.
First get sure bluez is uninstalled:
sudo apt-get --purge remove bluez
Choose the latest version from the repository https://www.kernel.org/pub/linux/bluetooth/:
in this case it is bluez-5.9, so we download it to our homedirectory:
cd ~; wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.9.tar.xz
we uncompress the package by
tar xvf bluez-5.9.tar.xz
We need to make sure all the necessary libraries are installed:
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
Now we are ready to compile bluez (this will take a while):
cd bluez-5.9
export LDFLAGS=-lrt
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-library -disable-systemd
make
sudo make install
sudo cp attrib/gatttool /usr/bin/
So lets bring up the bluetooth peripheral:
sudo hciconfig hci0 up
Let’s turn on our ClickBeetle BlueBeetle with the installed demo firmware and search for it via:
sudo hcitool lescan
with CTRL+C we can exit the scan process.
Now lets write a small script to control ClickBeetle. For that we will start gatttool with the BLE device address which is E2:73:E1:07:17:53 in this example:
sudo gatttool -b E2:73:E1:07:17:53 -I -t random
Entering connect followed by characteristics giving following result:
We see the characteristics we want to use to write data is having the value handle 0x0015. This information we will need to write data.
To control the RGB LED, data must be written in the style: “m,R,G,B”. Example: “m,255,0,0”
We start creating our script by:
nano clickbeetlecmd
Following script is using the gatttool to control writing data to value handle 0x0015.
#!/bin/bash
STR=m,$2,$3,$4
#HEXVAL=$(hexdump -e '"%X"' <<< $STR)
HEXVAL=$(xxd -pu <<< $STR)
echo "$HEXVAL"
gatttool -b $1 --char-write -a 0x0015 -n $HEXVAL -t random
We close nano with CTRL+w and CTRL+x.
Now we need to make clickbeetlecmd executable and copy it into the bin folder by:
sudo chmod 755 clickbeetlecmd
sudo cp clickbeetlecmd /usr/local/bin/
Great, we are ready. ClickBeetle can be now controlled by RaspberryPI Zero W by following command:
(ClickBeetle E2:73:E1:07:17:53, turning LED red maximum, LED green off, LED blue off)
sudo clickbeetlecmd E2:73:E1:07:17:53 255 0 0