IKEA Trådfri Hack with Gateway (video #145 )
In video #140 we hacked the new IKEA Tradfri Smart lighting system using the remote control as a basis. And, as I said already in video #144: It worked, but had some major disadvantages. Today, we will use an IKEA Gateway and do it right!
- We will eliminate the problems of the current hack
- We will install and pair the gateway with two different bulbs
- We will learn how the system communicates
- We will switch the lights using node-red. Like that, we can use MQTT or many other tools to switch the lights
If you are interested
- We will switch the lights with a normal Linux command
- We will install a Python wrapper. You can use this wrapper as a basis for your own programs
- We will see the CoAP protocol , which was introduced in the last video, in action
- At the end, as promised, I will reveal a “hot” secret about the IKEA bulbs.
The major problem with the last hack was, that the remote control only toggled the on and off switch. So, we were never sure, if the bulb was really on or off. This works ok if you switch by hand, because you just press the button again, if something went wrong. For home automation systems, however, we need a concept with clear commands like “off” or “on”. And it would be nice, if we could read the actual state back to check, if everything went right.
Ikea sells a Gateway for its lighting system, which promises to be able to do exactly that. You get it together with a Smartphone app for around 30 dollars.
IKEA Architecture
First, pair your Remote Control with the Gateway and with all your bulbs.
Now we have to find the IP address of the gateway. I usually use Advanced IP scanner to do so. Fortunately, the MAC address is also printed on the Gateway. So, we just have to search for it in our IP scanner..
IP scanner
Tutorial about installing a CoAP client to our Raspberry Pi (and much other stuff):
https://learn.pimoroni.com/tutorial/sandyj/controlling-ikea-tradfri-lights-from-your-pi
Peter Scargill’s script to install Node-Red, Mosquito, and many other goodies:
Consider also watching video #126 and #144 first:
Then, you start your browser and navigate to node-red. In the palette, you find a contribution called node-red-contrib-Tradfri. Just install this contribution and you are done. A CoAP client is installed together with this contribution.
Here is the flow. Just copy the whole flow into your clipboard and insert it from there in your Node-Red installation:
[{“id”:”7a41aec5.1ad98″,”type”:”tradfri-out”,”z”:”faf1d11c.29964″,”name”:”By msg.tradfri_id”,”dtype”:””,”tradfri_id”:”0″,”hub”:”8059fcc6.22ef7″,”output”:false,”x”:853,”y”:195,”wires”:[]},{“id”:”2925b8e0.63a798″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”warm”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:312,”y”:151,”wires”:[[“e88c5652.38d698”]]},{“id”:”c9e0a605.bc7268″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”off”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:312,”y”:298,”wires”:[[“e88c5652.38d698”]]},{“id”:”e88c5652.38d698″,”type”:”function”,”z”:”faf1d11c.29964″,”name”:””,”func”:”color=String(msg.payload)\n\nswitch (color){\ncase ‘warm’: \n msg.payload = { tradfri_id: 65538, type:’device’, state: ‘on’, color: ‘warm’, brightness: 255}\n break;\ncase ‘cool’: \n msg.payload = { tradfri_id: 65538, type:’device’, state: ‘on’, color: ‘cool’, brightness: 255}\n break;\ncase ‘half’: \n msg.payload = { tradfri_id: 65538, type:’device’, state: ‘on’, color: ‘cool’, brightness: 128}\n break;\ncase ‘off’: \n msg.payload = { tradfri_id: 65538, type:’device’, state: ‘off’}\n break;\n}\n\nreturn msg;”,”outputs”:1,”noerr”:0,”x”:607,”y”:198,”wires”:[[“7a41aec5.1ad98”]]},{“id”:”87ebd098.7894b”,”type”:”tradfri-out”,”z”:”faf1d11c.29964″,”name”:”TRADFRI bulb E27 WS clear 950lm”,”dtype”:”device”,”tradfri_id”:”65537″,”hub”:”8059fcc6.22ef7″,”output”:false,”x”:690,”y”:1018,”wires”:[]},{“id”:”98187db4.72c6″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”65537″,”payloadType”:”json”,”repeat”:””,”crontab”:””,”once”:false,”x”:323,”y”:985,”wires”:[[“87ebd098.7894b”]]},{“id”:”1495e6db.5819b9″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”off”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:284,”y”:758,”wires”:[[“87ebd098.7894b”]]},{“id”:”b88c5b55.05f3e8″,”type”:”tradfri-get”,”z”:”faf1d11c.29964″,”name”:””,”hub”:”8059fcc6.22ef7″,”x”:548,”y”:624,”wires”:[[“2b209f5c.b0e3a”]]},{“id”:”2b209f5c.b0e3a”,”type”:”debug”,”z”:”faf1d11c.29964″,”name”:””,”active”:true,”console”:”false”,”complete”:”payload.on”,”x”:815,”y”:624,”wires”:[]},{“id”:”3b4ed60b.9cc7ca”,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”65537″,”payloadType”:”json”,”repeat”:””,”crontab”:””,”once”:false,”x”:282,”y”:626,”wires”:[[“b88c5b55.05f3e8”]]},{“id”:”d0c91ad7.c54e98″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”cool”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:319,”y”:204,”wires”:[[“e88c5652.38d698”]]},{“id”:”c90e7466.0d3148″,”type”:”inject”,”z”:”faf1d11c.29964″,”name”:””,”topic”:””,”payload”:”half”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:312,”y”:244,”wires”:[[“e88c5652.38d698”]]},{“id”:”7915f32e.2429dc”,”type”:”bigtimer”,”z”:”faf1d11c.29964″,”outtopic”:””,”outpayload1″:””,”outpayload2″:””,”name”:”Big Timer”,”lat”:”47.474161″,”lon”:”7.767258″,”starttime”:5001,”endtime”:”5000″,”startoff”:”1″,”endoff”:0,”offs”:0,”outtext1″:”on”,”outtext2″:”off”,”timeout”:1440,”sun”:true,”mon”:true,”tue”:true,”wed”:true,”thu”:true,”fri”:true,”sat”:true,”jan”:true,”feb”:true,”mar”:true,”apr”:true,”may”:true,”jun”:true,”jul”:true,”aug”:true,”sep”:true,”oct”:true,”nov”:true,”dec”:true,”day1″:0,”month1″:0,”day2″:0,”month2″:0,”day3″:0,”month3″:0,”day4″:0,”month4″:0,”day5″:0,”month5″:0,”d1″:0,”w1″:0,”d2″:0,”w2″:0,”d3″:0,”w3″:0,”d4″:0,”w4″:0,”d5″:0,”w5″:0,”suspend”:false,”random”:false,”repeat”:true,”atstart”:true,”x”:307,”y”:367.5,”wires”:[[“e88c5652.38d698”],[],[]]},{“id”:”8059fcc6.22ef7″,”type”:”tradfri-config”,”z”:””,”name”:”Lab”,”hubip”:”192.168.0.15″,”sid”:”djed8rbxLWVOdUWm”,”coap”:”/home/pi/.node-red/node_modules/node-tradfri-argon/lib/coap-client-raspbian”}]
Directory of CoAP client on Raspberry Pi:
/home/pi/.node-red/node_modules/node-tradfri-argon/lib/
To use the CoAP client, it is best to navigate to its directory:
cd /home/pi/.node-red/node_modules/node-tradfri-argon/lib/
Discover the available lamps (change your IP address and the Security Code from the bottom of your Gateway):
./coap-client-raspbian -m get -u "Client_identity" -k "Security Code" "coaps://192.168.0.15:5684/15001"
Parameters:
ON: 5850:1 OFF: 5850:0 Brightness: 5851: (brightness from 0 to 255) Warm: 5709:33135 5710:27211 Cold: 5709:24930 5710:24684 Normal: 5709:30140 5710:26909
Bulb 1 ON:
./coap-client-raspbian -m put -u "Client_identity" -k "Security Code" -e '{ "3311": [{ "5850": 1 }] }' "coaps://192.168.0.15:5684/15001/65537"
Bulb half:
./coap-client-raspbian -m get -u "Client_identity" -k "djed8rbxLWVOdUWm" -e '{ "3311": [{ "5708": 128 }] }' "coaps://192.168.0.15:5684/15001/65537"
If you want to use this client in a Python program, you can install a so called “Python Wrapper”.
cd ~
sudo apt-get install python-pip
sudo pip install tqdm
git clone https://github.com/sandyjmacdonald/ikea-smartlight
cd ikea-smartlight
It consists of a few python programs and is installed in your home directory.
Navigate into its directory and create this file:
cd
ikea-smartlight
nano tradfri.cfg
and enter
[tradfri]
hubip = 192.168.0.15 (ip address)
securityid = Security Code
Now, you can discover the bulbs with a simple command:
./tradfri-status.py
or switch the lamps:
./tradfri-lights.py -l 65537 -a power -v on ./tradfri-lights.py -l 65537 -a power -v off ./tradfri-lights.py -l 65537 -a brightness -v 50 ./tradfri-lights.py -l 65537 -a color -v warm