Rainbow Cube


Create an Internet-enabled mood lamp using Seeedstudio's Rainbow Cube and make it talk to the web via DeviceHive and Raspberry Pi. To demonstrate how Devicehive can be easily used, we’ve bought a Rainbow Cube and integrated it with DeviceHive. It takes just a couple of hours!

This guide shows the step-by-step process of configuring DeviceHive's cloud, deploying software on Rainbow Cube using Arduino, copying the agent on Raspberry Pi and running the HTML5 app to control it. Before you start, please download the .zip file with everything you need here.

Rainbow Cube

The solution consists of the following:

  • Rainbow Cube — an array of RGB LEDs, where each LED is managed by an Arduino-compatible controller. Using Rainbowduino library it’s possible to set RGB color for each LED individually. The cube can be flashed with simple Arduino firmware, which transmits commands from the DeviceHive server to the Rainbowduino library.
  • Raspberry Pi. The cube doesn’t have an Internet connection, so we cannot connect it to the DeviceHive server directly. So Raspberry Pi becomes handy and can be used as a gateway that enables the cube to talk to the cloud by having a simple agent deployed to Raspberry Pi that would translate messages between RESTful cloud service and the binary protocol used for Arduino-like devices.
  • The HTML5 application allows us to choose a custom color and light the whole Rainbow Cube. Moreover it’s possible to set RGB color to particular LEDs or to fill a custom area.
    The control flow is simple. After selecting a color, the HTML5 application sends a command to the DH server. RPi grabs that command from the DH server and sends it to the Cube.


Rainbow Cube Device

The Cube device is connected to the RPi via a serial port. If you use a USB cable, the Cube device will be available as “/dev/ttyUSB0”. But it’s also possible to make the same connection using a pair of XBee modules.
Rainbowduino firmware uses binary protocol to communicate with the RPi.
The cube device supports the following commands:

  • “fill” to fill the whole Cube with one color
  • “pixel” to set a color of a pixel
  • “pixels” to set colors of multiple pixels at once

To build firmware you have to provide two additional dependencies from the libraries folder:

  • DeviceHiveEngine
  • Rainbowduino

Unpack these dependencies into the Arduino’s “libraries” path.
The firmware can be found at ArduinoFirmware folder. Now you can build and update the firmware of the Rainbowduino board.
The next step is to configure the gateway on RPi.

RaspberryPi Gateway

Gateway is an application that grabs commands from the DH server, converts them to binary format and sends them down to the serial device.
In this case it’s just a simple C++ gateway example. The full sources can be found at the Gateway folder. There are several builds - simple_gw.RPi.tar.bz2 for RPi and simple_gw.bb.tar.bz2 for BeagleBoard. You need to download one of them and then upload it to your gateway board.
The gateway could be run with the following command line arguments:

simple_gw --serial /dev/ttyUSB0 --baudrate 9600 --server <the URL of your playground>

Once started, the gateway registers your device (if it’s connected) on the DH server and transmits REST commands to binary and vice versa.

HTML5 client

It’s easy to control Rainbow Cube with the HTML5 application, you can find it in HTMLApplication folder.
After downloading client files follow the steps below:

  1. Open js\app.js file.
  2. In the beginning of the file find config JavaScript object:
    window.config = {
    		serverUrl: '[DeviceHive_api_url]',
    		login: '[DeviceHive_login]',
    		password: '[DeviceHive_Password]',
    	deviceId: '[Device_Id]',
    		cmdTimeout: 1000 * 60 * 2, //2 minutes
    		ledCmdName: 'pixels',
    		cubeSize: 4
  3. Change the cloud URL, login, password and LED Cube device id (id from DeviceHive).
  4. Save the app.js file and open the cube.html file in any modern browser that supports cross-domain requests (IE 10, Chrome, Firefox). You can also deploy this application on a server and access it using your IPhone device.
  5. Wait until all information about the device is loaded.

After completing these steps you will see a page with a color picker and four buttons:
Choose a color for pixels using the color picker and then click on one of the four buttons at the bottom of the page. Each button sends a command, which lights pixels in the corners of the cube. The number of pixels to light matches the button’s number. For instance, if you choose red color and the third button, you will see an inner red cube with the size of three pixels.
Note that if your device is not set up properly, timeout exception will be thrown.

< Back to samples