Controlling smart lights in home with your phone

Creative endeavours of an engineer

light bulbs with a book case in the background

Controlling smart lights in home with your phone

My wife recently bought a new set of living room lights. When they arrived, I noticed a nice Bluetooth icon on the box. As an engineer, I thrive on automation but I haven’t had the change to play with it in the past. Of course, I had to see how smart home lights actually work. Of course you can control the lights with a phone or tablet that has Bluetooth support. More specifically, BLE support which stands for Bluetooth Low Energy.

The packaging of the lights also featured a QR code which leads you to download an iOS or Android version of the application. You can use this application to turn the lights on and off, control the brightness, and even the temperature of the lights ranging from warm yellow to cold white light.

I could simply buy a remote control from the vendor, but then this would be a very short blog post. Instead, I decided to see how the connection works and if I can control the lights without the application.

Let’s set a goal here:

My goal is to use the phone to connect to the lights and switch them on and off without the bundled app.

Let’s get to work. First, let’s see how the bundled app works.

Paulmann Home

As I mentioned, the lights are controlled via Bluetooth. Not only that, but Paulmann (lights vendor) also has an official Paulmann Home application that you can use to automated your home with their products. It’s a simple home automation application that allows you to recreate a virtual version of your home with the rooms and devices in your room. You can also use it to create “sequences”. A predefined set of settings that you apply to multiple devices in your home.

I set up the application set the administrator of my home and added the light. Turn on the light so the phone can connect to it.

Paulmann Home App - display status of lights

When the device successfully connects, you can control it using your phone.

Paulmann Home App - detailed display of lights including brightness and light temperature

You can also change the Bluetooth connection settings and define the PIN used to access the device and a type of connection

Paulmann Home App - setting the password for the smart lights in home

In the type of connection you can select “1 user and <=6 devices” and “multiple users and >6 lamps”. What I’ve learned from going through the documentation is that in the first case, the phone or tablet keeps an open connection with the light while in the latter, each time you switch the light on or off, the devices reconnect to the light. The former is of course faster while the second allows you to have multiple remotes or devices controlling the lights.

Well, if the app can do it, I can do it myself using a Bluetooth connection. But how does it work?

Controlling the smart home lighting

I’ve never worked with Bluetooth and smart home lights before this project. So, how do you connect to a Bluetooth device? Is there something that I can use from my phone?

After some digging, I found this very nice application called nRF Connect. It’s made by Nordic Semiconductors, one of the companies producing Bluetooth chips. It allows me to scan for devices, connect to and pair them, and read and write attributes. Let’s try it.

Scan for the smart lights

First, open the application and switch to the Scanner tab. Wait a few seconds and the app will initiate a bluetooth scanner and list devices that you can connect to. You will also see the quality of signal of each device and latency which gives you a rough estimate of the range of the device.

Lamp-WC is my Paulmann light and it’s obvious I’m sitting right bellow the light.

nRF Connect App - connecting to the lights

Let’s connect.

A new tab opens with the MAC address of the device and a list of characteristics with descriptors. Characteristics are properties of the light (such as a name and current brightness) while descriptors are textual descriptions of characteristics. Both together are called attributes but I won’t go into details because you can read about Bluetooth protocol and different types of attributes.

nRF Connect App - displaying characteristics

Here is an example of the Switch attribute that you will use to control the light. As you can see, the characteristic has an UUID (identifier), properties and a value. Value is currently 0x01 (which means 1 in hexadecimal) and its code for on. Clearly, this attribute is marked as “READ, WRITE” which means you can both read the state of the switch and change the state of the switch to 1 or 0 (on or off respectively). Bellow the characteristic you can find a descriptor attribute with value “Switch” which means that you can turn the light on and off by writing to the characteristic.

Clicking the “download” arrow next to the characteristic will open a new window to write a value to the device. But if you try it, the device will disconnect you.

Why is that? Simple. You need to authenticate to the device to be able to control it.

Authenticating with our device

Quick browse through the descriptor reveals a characteristic called “Password”.

nRF Connect App - finding password characteristic for the smart lights

Let’s assume the Password is actually the PIN that we set in the Paulmann Home application.

I set my PIN to “1111” in the Paulmann Home application. Now we have to send this password to the device.

Here, we’re going to assume that the device is expecting a PIN in char format. This means that we’re not sending the number 1 which would be 0x01 in hexadecimal but character 1 in ASCII. Let’s find the code for this character.

finding hex codes for ASCII characters
ASCII table from Wikipedia. Rightmost column is ASCII character.

Hexadecimal code for character “1” is “31”. Ok, let’s send this PIN to the device. Click the upload icon at the characteristic and a new window opens.

We’re going to add 4 values, each value if 0x31 which is code for character “1”.

nRF Connect App - sending hex values as password to the smart lights

If you sent the correct PIN and the device accepted it, the value will show the PIN in the Password characteristic. If the value is empty, the PIN is incorrect. After 3 incorrect attempts, the device disconnects and you have to reconnect.

Let there be light

Ok, so we successfully authenticated. Let’s flip the switch.

Find the attribute Switch by going through all the descriptors and write value 0x00 for off and 0x01 for on.

nRF Connect App - sending 0x00 to the smart lights in home to turn them off

It works. I’m controlling the smart lights at home. It’s just a lot more complicated then pressing a light switch.

gif of turning the smart lights in home on and off

Conclusion

Our goal was to control our smart lights at home via Bluetooth without relying on the bundled app provided by the manufacturer. First, we analyzed the application to see how it works and studied the documentation. Then, we used nRF Connect app to connect to the lights and authenticate, read and write attributes.

Using the nRF Connect, we analyzed what attributes the device (lights) supports and what can we change. We used the Switch attribute tu turn the lights on and off.

In the next article, I’ll try a Linux machine to control the lights. This is the next step in my overarching goal to better home automation.

 

No Comments

Add your comment