Building poor man’s smart lights

Vladimir Atanasov
Vlad’s corner
Published in
6 min readJan 10, 2017
The finished “product”

This is a ghetto version of the great Phillips Hue smart lights. This project was put together in one Sunday afternoon, so there is a ton of room for improvement.

The lights communicate with an Android phone via Bluetooth, so you can control them only on close proximity. The reason I chose Bluetooth is that I had a spare module (HC-06) lying around. For controlling the RGB strip, I used a cheap LED strip driver module, that plays very nicely with the Arduino. For the micro controller I used an Arduino Uno, but any other Arduino compatible board should work just fine. The whole thing should cost you less than €40 (~$42), considering you have to buy everything. As expected, the most expensive part is the Arduino board itself (phone not included, doh).

Ok, let’s put the thing together. The software IDE’s we are going to need are Arduino IDE and Android Studio. Both are free and brilliant. Go ahead and download them. I won’t go into detail on how to set up your environment, there are plenty of tutorials online.

For the wiring, we won’t need anything special, if you use the breakout boards, you won’t need any other components. Start by wiring the LED strip driver. Connect the RGB strip wires to the corresponding pins on the board, they should be labeled with R G B +

RGB LED strip driver

My breakout board has very convenient terminal block connectors, so I just had to screw the cables. Then we have to connect the driver to our micro controller. Connect the VCC to Arduino’s 3.3v pin, GND to Arduino’s Ground, DIN (data) to 3, CIN (clock) to 2.

Once this is done, you will have to power up the whole thing. For driving a 5m long LED strip, you will have to supply 12V, please check how many Watts the RGB strip draws, so you can pick the appropriate power supply. I did connect the 12V to the LED Strip driver and then extended the connection to the Arduino, this way, I can power both from one source. The recommended input voltage for the Arduino is 7-12V, so we are good to go there.

I did connect the 12V to the LED String driver and then extended the connection to the Arduino, this way, I can power both from one source. The recommended input voltage for the Arduino is 7-12V, so we are good to go there.

The Bluetooth connection is as follows: VCC to 5V on the Arduino, GND to GND, TX to pin 10, RX to pin 11.

Once we are done with the wiring, we can move to the software.

For controlling the lights, we will use this library. Download it, unzip and import to your Arduino libraries. Include the library into you schema and instantiate the diver

#include "RGBdriver.h"RGBdriver Driver(2, 3); // CLK, DIN

Next we have to include the library that will take care of the Bluetooth for us.

#include <SoftwareSerial.h>SoftwareSerial BT(10, 11); // TX, RX

Next, in the setup() method, we have to initialise Bluetooth

void setup()
{
BT.begin(9600);
Serial.begin(9600);
}

Before we continue, we should change the default settings on our bluetooth module. We don’t want our neighbours connecting and trolling us during the night.

To do so, we have to send some AT commands to the Bluetooth module. The easiest way to do so, is to temporarily listen for serial data and write it to the Bluetooth module. Then we will use the IDE’s built in Serial Monitor

void loop()
{
if (BT.available())
Serial.write(BT.read());
if (Serial.available())
BT.write(Serial.read());
}

Upload the sketch and open the Serial Monitor if everything was successful. You can find the Serial Monitor under Tools.

Ok, time to reprogram our Bluetooth module. The following commands are for the HC-06 (JY-MCU), you can double-check if you have a different board.

To change the name, send

AT+NAMEmy fancy light

Now you can discover your Bluetooth device under the name “my fancy light”, how cool is that!

Next we want to change the default pin (Mirai anyone?). To do so, send the following command

AT+PIN1234

You would like to use a stronger pin, but other than that we are done! You can now remove the code from within the main loop()

Next we are going to start listening for incoming commands via the Bluetooth, parse them and do some work

void loop() {  readBT();
}
void readBT() {
if (BT.available()) {
char colors[BT.available()] = {};
int i = 0;

while (BT.available()) {
char c = BT.read();
colors[i] = c;
i++;
}
Serial.println(colors); char *tok;
int j = 0;
tok = strtok(colors, ",");
while (tok != NULL) {
if (j == 0) {
Serial.print("Red: ");
redVal = atoi(tok);
} else if (j == 1) {
Serial.print("Green: ");
grnVal = atoi(tok);
} else if (j == 2) {
Serial.print("Blue: ");
bluVal = atoi(tok);
}
Serial.println(atoi(tok)); tok = strtok (NULL, ",");
j++;
}
Driver.begin();
Driver.SetColor(redVal, grnVal, bluVal);
Driver.end();
}
}

The code reads everything available on the Bluetooth port, assuming it’s the RGB values from our phone in the format “255,255,255”, splits the values into tokens and parses each token into an integer. Lastly we send the int values to our RGB Driver. Easy!

Compile and upload the sketch. If you didn’t encounter any errors, we can grab a coffee and move to the next step.

After we had our coffee it’s time to build the awesome mobile app.

First open Android Studio and create a new Android project.

Since I’m lazy, I will use a library to handle the Bluetooth for me, but I strongly suggest going through the official docs. The library I’m going to use, is actually pretty awesome and hides a lot of complexity from me. Open your build.gradle file and add it as a dependency

compile 'com.akexorcist:bluetoothspp:1.0.0'

Next we have to request Bluetooth permissions for our app. To do so, open the AndroidManifest.xml and add the following lines above the application node

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

While there, add this line in the application node

<activity android:name="app.akexorcist.bluetoothspp.DeviceList" />

It’s time to roll up our sleeves and start coding. Open your main activity, if you have created a template activity, it will be named MainActivity.java.

First we have to initialise and construct the BluetoothSPP instance.

BluetoothSPP bt;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = new BluetoothSPP(this);

if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
}
bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
@Override
public void onDeviceConnected(String name, String address) {
Toast.makeText(MainActivity.this, String.format("Connected to %s", name), Toast.LENGTH_SHORT).show();
}

@Override
public void onDeviceDisconnected() {

}

@Override
public void onDeviceConnectionFailed() {

}
});
}
@Override
protected void onStart() {
super.onStart();

if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}

}
@Override
protected void onDestroy() {
super.onDestroy();

bt.stopService();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE && data != null) {
if (resultCode == Activity.RESULT_OK) {
bt.connect(data);
}

} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
// setup();
} else {
// Do something if user doesn't choose any device (Pressed back)
}
}

}

Great! Now we can send and receive data from our device.

To set a color to our very cool RGB lights, we can simply send the color in an RGB format, like that

bt.send("255,0,0", false);

And there, we have our very own fancy connected lights.

You can find the source code in GitHub. In the project, you will find a color picker and a crossfade color mode, that I shamelessly copied from Arduino’s blog

There is a ton of room for improvement. The android app needs a lot of refinement, I want to have feedback on what color is currently set. The Arduino module should use interrupts, not poll for commands as this causes a lag, when the crossfade is on. I would love to hear your comments on the project, as this is my first “connected” device, all recommendations and critiques are welcome.

Tune in next time, for a tutorial on how to build a mars rover from salvaged DVD parts.

--

--