This is a fun project that shows how to control a smart light bulb (Yeelight Blue) that uses Bluetooth Low Energy (BLE) to change its color with Raspberry Pi! bulb recently at a local store and was intrigued by its Bluetooth-based control and relatively low price (for a 'smart' gadget at least). Because the light bulb uses Bluetooth Low Energy (which is a subset of Bluetooth 4.0) it means any BLE device can in theory control the bulb. I bought a Yeelight Blue bulb. To control the light, I used the Raspberry Pi and Bluetooth 4.0 USB Module.
What's Yeelight Blue ?
Yeelight Blue is your personal scene lighting. It enables you to create and control light using your smart phone with Bluetooth 4.0. The LED technology inside every yeelight blue can display 500 lumens high quality white light and a wide variety of 16 million colors. And each yeelight blue bulb uses 90% less power than a traditional light bulb.
• Raspberry Pi B/B+/2/3/A/A+/ZERO
1. Install Required Librarie
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
2. Download Bluez
sudo mkdir bluez cd bluez sudo wget www.kernel.org/pub/linux/bluetooth/bluez-5.31.tar.xz
3. Unzip and Compile Bluez
sudo unxz bluez-5.31.tar.xz sudo tar xvf bluez-5.31.tar cd bluez-5.31 sudo ./configure --disable-systemd sudo make sudo make install
4. Insert the USB Module and Reset
Once Bluez has been built, shut down your computer with
sudo shutdown -h
now and once its Halted, insert your Bluetooth 4.0 USB Module and then reset the Raspberry Pi so that all of the changes we have made can take effect.
5. Install Bluez's GATT tool
Manually install bluez's GATT tool. For some reason bluez does not install its GATT tool anymore as this bug notes, however an easy workaround is to manually install it by executing this command inside the bluez source directory after it has been compiled and installed:
sudo cp attrib/gatttool /usr/bin/
Confirm you can access gatttool by running 'gatttool --help' (without quotes) to see the usage information of the tool. If you see an error that gatttool can't be found, double check it has been compiled by bluez and it's in to the /usr/bin/ directory.
Now I brought up the Bluetooth USB adapter on the Pi by running the command hciconfig to find the name of the adapter (it should be hci0 assuming it's the only Bluetooth adapter connected to the Pi). Then running 'sudo hciconfig hci0 up' (without quotes) to turn on the adapter. Finally running hciconfig again should show the adapter is in the UP RUNNING state as shown below:
6. Scan BLE Devices
Now that the adapter is up I can scan for BLE devices by running the command:
sudo hcitool lescan
Information about nearby BLE devices will be displayed. Press Ctrl-C to stop the scanning process. Please remember Yeelight Blue address.
7. Download Yeelight Blue Library
git clone https://github.com/leopck/Yee-Light-Blue.git cd Yee-Light-Blue
8. Edit test.py
sudo nano test.py
On line 12, Replace current address with Yeelight Blue address.
9. Run Python Script
sudo python test.py hci0
Special thanks to Stanley.