IKEA recently started selling “smart” lights including remotes and gateways with a pretty attractive price tag. When the popular german website heise.de published an article on connecting one of those remotes to an ESP8266, I wanted to try this myself. Instead of using an ESP, I decided to reuse a Raspberry Pi 1 model B, which is located in the bedroom already anyway. Nice side effect: the board of the remote even fits inside the case along with the Raspberry Pi itself.
The main goal was to integrate those Trådfri lights into any kind of home automation software that understands the MQTT protocol, for example OpenHAB or homebridge.
For the Raspberry Pi, any version will work. Just make sure it has pin headers. If not (like the Raspberry Pi Zero), you have to solder some on beforehand.
Before you start soldering, it is a good idea to decide which GPIO pins on the Raspberry Pi you will use. There are several websites out there showing the pinout of the Raspberry, e.g. the official Raspberry Pi documenation or Panu.it. You need one pin for 3.3V, one ground pin, and five digital output pins.
If you go for the first few pins (in the range 1 through 15, which are identical across all Raspberry Pi models), your selection could look like the following:
Note that the image shows eight pins instead of seven. The eighth pin connects to TX on the remote circuit board. But so far I have not experimented with it to figure out how it can be used.
Here’s the actual pin assignment for the image above:
Color | Function | physical pin | GPIO pin |
---|---|---|---|
red | 3.3 V | 1 | |
orange | TX | 3 | 2 |
yellow | touchlink reset | 5 | 3 |
green | color | 7 | 4 |
blue | ground | 9 | |
violet | dim - | 11 | 17 |
gray | on/off | 13 | 27 |
white | dim + | 15 | 22 |
See Appendix 1 of the Raspberry Pi documention for the difference between the numbering of physical pins and GPIO pins.
Before you can solder, you have to “carefully” extract the circuit board from the case. But expect to break most parts of it. The circuit board is right below the buttons, a rubber mat and a white plastic board. This white plastic board and the circuit board are clipped into the outer housing. Once you have circuit board, check if it still works.
Before soldering the jumper cables, you can cut them short enough to minimize the amount of excess cable inside the case. Then solder the cables onto the circuit board as pictured (remember to remove the battery when soldering):
Using the colors in the image above, make sure your red cable will really connect to the 3.3 V pin and the blue one to the ground pin. Other than that, you can freely choose the wiring. When done, check again if the circuit board still works.
After you have plugged in all jumper cables, it’s time to boot! If you haven’t already, install wiringpi:
sudo apt-get install wiringpi
While wiringpi is actually a GPIO access library written in C, it comes with a handy little tool called gpio. It allows us to manipulate the GPIO pins straight from the command line. For a start, you can list all pins and their current state using gpio readall
at any time.
First, the pins need their mode set to output, for example GPIO pin 2:
gpio -g mode 2 out
The -g
is needed to be able to pass GPIO pin numbers. Otherwise, you would have to translate the pin numbers to yet another numbering scheme, which wiringpi (and the gpio utility) use by default. Repeat the above for all other pins. A gpio readall
should now tell you that all your pins are in ‘out’ mode.
Next, you have to initially set all pins to ‘high’, e.g. for GPIO pin 2 again:
gpio -g write 2 1
Now you are finally ready to test. Use the gpio utility to set a pin to low, wait a bit, and set it back to high. For example, the following should change the color (assuming you used the same pins pictured above):
gpio -g toggle 4; sleep 0.05; gpio -g toggle 4
You should be able to do almost everything the remote control also allowed you to do, with the exception of being able to toggle the color in only one direction. Also, remember that the touchlink reset pin needs to be “pressed” for at least 10s to take effect.
Now that everything is working, the next step is to make all this controllable by a home automation system. One way to do this is to use the MQTT protocol. You find a Python script on GitHub that plugs everything together, using RPi.GPIO to access the GPIO pins and the paho library to communicate over MQTT. The script subscribes to all messages in “bedroom/lights” and pulls the GPIO pins accordingly.
All this assumes you have an MQTT broker (i.e. a server) running somewhere in your network. If you don’t, you can install mosquitto
on any Linux system in your home network. Installing it on the Raspberry Pi will also work.
Where to go from here? Many home automation systems will understand MQTT, so you can integrate it rather easily. For homebridge, there’s the mqttlightbulb plugin, which can be configured as follows:
{
"accessory": "mqttlightbulb",
"name": "Lights",
"url": "http://your.mqtt.broker:1883",
"caption": "",
"topics":
{
"getOn": "bedroom/lights/power",
"setOn": "bedroom/lights/power",
"getBrightness": "bedroom/lights/brightness",
"setBrightness": "bedroom/lights/brightness",
"getHue": "bedroom/lights/hue",
"setHue": "bedroom/lights/hue",
"getSaturation": "bedroom/lights/saturation",
"setSaturation": "bedroom/lights/saturation"
}
}
You should now be able to control the Trådfri lights from your smartphone.
As you might have noticed already, this whole solution only allows to set parameters of connected lights. You are not able to get the current state. Half a solution is to keep track of the state when changing it, as done in the pyhton script above. If you start in a known state, this works pretty well. In order to get a known state, you send the message “auto” for the topic “bedroom/lights/calibrate” (i.e. using mosquitto_pub
provided by the Linux package mosquitto-clients
), which will reset the lights to 2700 K and full brightness. But you are still able to lose sync, for example when using the wall switch or trying to apply changes too quickly. This pretty much restricts the possible use cases to those that do not require feedback of the current state. Unfortunately, home automation systems generally do not fall into this category.
A viable alternative is the Trådfri gateway, for which you have to spend an additional € 30. It connects to your local network and can be controlled using CoAP. Using this protocol, you can query the state of your lights and also set the effective brightness and color, without switching through all intermediate states “manually” (as done by the script). If you want to go for the gateway, have a look at the GitHub repository by hardillb. There you find a Java implementation that translates the CoAP messages from/to the gateway to MQTT. Alternatively, you might also be able to directly integrate the gateway. For homebridge, there is a pretty capable plugin called homebridge-tradfri-plugin.