This project explains how to create the Christmas Lights using Android Things, Android phone and IoT platform DeviceHive. As for the lights themselves we used a WS2812B led strip. Android Things will be running using Raspberry Pi 3, through any board with a SPI interface can be used instead. The lights will be controlled from a mobile device and can generate patterns based on sounds recorded on a mobile phone.
Android Things is well constructed and modern operating system provided by Google that allows us to use the Android API and third-party Java and Android libraries. The Raspberry Pi 3 will send the colors to the led strip via SPI (Serial Peripheral Interface ).
SPI is a synchronous serial communication interface and represents a master-slave relationship between devices. Android Things has special PeripheralManager in the API to operate with this bus.
According to the original documentation the data transfer protocol of the WS2812B uses single NZR communication mode. After the pixel power-on reset, the DIN port receive data from controller, the first LED on the strip takes first 24 bits of data and then sends every following bit to the next LED in the strip through the DO port. Each next LED in the strip receives 24 bits in the same way, so transmision should contains the data for each LED.
This transmission protocol of the strip has quite a simple behaviour, digital “1” is encoded as a long high-pulse, “0” as a short pulse on “Din”.
Sequence Chart from the original documentation where T0H takes 220ns~380ns, T0L takes 580ns~1.6µs, T1H takes 580ns~1.6µs and T1L takes 220ns~420ns
We are going to emulate the protocol to work with SPI. In our example the Raspberry Pi 3 is the master and the WS2812B is the slave.
So to emulate the SPI protocol we are using blocks of 4 bits in SPI transmission to generate one bit of WS2812B protocol. The SPI speed should be selected to fit the WS2812B timings. We use 3809523 Hz and each 4 bits (one WS2812B bit) transmission takes 1.05µs.
To build the Christmas lights we need:
Connection between the RPi3 and LED Strip
Connection between the RPi3 and LED Strip
The installation instructions for Android Things are provided in the official documentation.
Basicly, that is it from the hardware side! Just make sure that power supply is powerful enough for the LED strip and board itself. For long strips 5V should be connected directly to the power supply without going through the Raspberry Pi 3 board.
To connect device the with the Android Things OS on the board and mobile device we need a platform that will provide such connectivity. The DeviceHive platform provides all that we need.
DeviceHive turns any connected device into a part of the Internet of Things. It provides the communication layer, control software, and multi-platform libraries to bootstrap the development of smart energy, home automation, remote sensing, telemetry, remote control and monitoring software, and much more.
The DeviceHive platform provides us with support libraries. To develop client applications we can use the original DeviceHive client Java library. The DeviceHive Client Library is an open source project based on Java. It can be used to build any applications based on Java or Android. This library has full support of Android Things. Implementation of this library is really simple:
That’s it. Now we have all that we need to use DeviceHive in our project. After the DeviceHive support library was set up we created a project that contains two modules: an Android Things application and an Android mobile application.
The Things module just listens for incoming commands from the WebSocket connection established with the DeviceHive server and performs the required actions. The commands are:
Command’s parameters in JSON format
4. OFF, just turns off the led strip.
Commands look in DeviceHive admin console
The mobile module sends the commands to the Raspberry Pi 3. It has quite a simple UI that includes two screens, a login screen and the main screen. On the login screen we have to fill in three field with the Rest URL , refresh token, and device id. On the main screen we have 4 buttons to perform the commands.
After we tap on the button that performs the Audio command the smartphone starts recording sound that comes from the smartphone’s mic. Then it transforms this captured sound to a Fourier series via the Fast Fourier Transformation algorithm (FFT) and counts the beats. The FFT algorithm creates a unique series of lights that displays a single audio track for the led strip. After this preprocessing, the mobile application generates HSV colors based on this f-series . We use HSV to manipulate only the H (hue) component and keep the S (saturation) and V (value) the same so we get bright colors. At the end, the app sends the data with the color and speed information that was calculated from the BPM.
After the command is sent the things module process it.
Christmas tree with the Lights
Of course that is just a fun project which can be used for home decoration, but since it is a fully open source project, it can be adopted for some more serious work. Using Android Things as a LED controller it is possible to implement more complicated logic, connect it to other IoT platforms, and create a cluster of boards controllable from one service. We hope this project helps you to decorate your home and create more advanced light systems.
With the warmest wishes from the DeviceHive team!