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
Johan Hedbrant
25. September 2017 @ 9:34
Dear Andreas,
Thanks for your excellent blogs and videos, they are outstanding in inspiration and pedagogics!
I have some IKEA Tradfri bulbs and remote controls, and want to make a “Good night” button, where I can turn off several groups by clicking only one remote. I figured I could do this with Node-red.
Unfortunately I don’t get the same response from Node-red and tradfri_get as your YouTube #145 at 7:40. When I send an “a” I dont get anything back from tradfri (ie no array etc). If I however send an ID-number, ie 65537, I get an msgid back, but not anything readable (as arrays etc)
Is this a simple solution to this? I want to read the state of the remote GOOD-NIGHT, and if it has changed from on to off, I’ll switch off the bulb groups?
(I can “almost” do this with Scargill’s COAPs and a Python program that is looping code from the wrappers. It however only works for an hour or so, then it hangs due to some misreading etc. Maybe a Node-red flow is more reliable…? 🙂 )
Kind regards
Johan Hedbrant
25. September 2017 @ 15:48
FOUND IT, I think! 🙂
The Node-red module “tradfri get” gives a decent response to the debug module if feeded with a NUMBER instead of a string for the ID-number, eg for the GOOD-NIGHT remote (group). I’ll try this new track! My mistake, sorry for the inconvenience! X-)
KR /j
Joni Räsänen
29. October 2017 @ 13:43
Hi, Nice video! I am wondering could this Python wrapper and CoAP also discover Trådfri remote controllers and can they paired with Trådfri hub like Philips Hue does? I can image multiple use cases for these cheap battery powered controllers with one or more buttons.
admin
11. November 2017 @ 7:07
I never tried it but it should be possible.
Tuomas
30. October 2017 @ 21:46
Awesome video and blog post. As inspiration of this, I bought set of IKEA TRÅDFRI and Raspi3 and started to play around with Node-red. I got my basic flows setup and have my “nice” node-red ui dashboard setup etc…
I got to the point to start playing around with the dimmer and color changes, now I wanted to import your flow to my node-red to see the function you wrote. But here I hit a wall, it seems that if I copy paste your flow, node-red is not allowing me to import it (leaves the greyed out). Could there be a bug in the copy paste above? I tried to do import to both Raspi and my Bluemix node-red, but it is not going in ether one of them.
admin
11. November 2017 @ 7:06
I just tried it and it worked for me. But keep in mind that this flow uses a SQlite node which has to be installed first. I use Peter Scargills script which installs lots of additional nodes.
IoT ja kotiautomaatio – osa 1: Perusteet kasaan – Rintamamiestalon uudet asukit
3. November 2017 @ 19:57
[…] Youtube videoita: http://www.sensorsiot.org/145-ikea-tradfri-hack-with-gateway/ […]
skullie
27. May 2018 @ 16:45
I get this error when getting the status..
/opt/ikea/ikea-smartlight$ ./tradfri-status.py
[ ] Tradfri: acquiring all Tradfri devices, please wait …
4.01
Traceback (most recent call last):
File “./tradfri-status.py”, line 90, in
main()
File “./tradfri-status.py”, line 47, in main
devices = tradfriStatus.tradfri_get_devices(hubip, securityid)
File “/opt/ikea/ikea-smartlight/tradfri/tradfriStatus.py”, line 44, in tradfri_get_devices
return json.loads(result.read().strip(‘\n’))
File “/usr/lib/python2.7/json/__init__.py”, line 339, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 382, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded
Any clue about what might be?
Thanks
PCB
15. January 2019 @ 19:19
You probably either worked it out by now or gave up!
Windoze has helpfully changed the quotes to ‘smart quotes’ , you need to change them all back after you’ve pasted the text.
Christian Tzolov
7. June 2018 @ 22:54
Very insightful post Andreas!
Recently I bought couple of IKEA smart gadgets and have been toying of building an intelligent CoAP Shell (https://github.com/tzolov/coap-shell) including some basic Ikea gateway support as well (https://www.youtube.com/watch?v=zhEGFfCJwTg&feature=youtu.be)
@Andreas will appreciate if you can share your feedback and suggestions about it.
Cheers,
Christian
admin
14. August 2018 @ 13:04
I did not do anything with these lightbulbs since then. So I cannot help.
Ketil
11. August 2018 @ 13:46
I think the problems reported here may be due to IKEA changing their authentication procedure. You can no longer execute commands (i.e. change lighting) directly with the key from the gateway. Instead you must initiate a new user session with a generated key:
coap-client -k $gwkey -u Client_identity coaps://$gwip/15011/9063 -m post -e ‘{ “9090” : “$username” }’
You get a response with the new PSK, and you can now execute commands with your selected username ($username) and this key. (Note 15011, not the usual 15001 in the address).
admin
14. August 2018 @ 13:04
Thank you for your tip!