Skip to content

IKEA Trådfr Example

Jim Schaad edited this page Jul 7, 2017 · 2 revisions

Lots of people are looking to talk to the IKEA Trådfr systems. Some of the major concepts needed to do this are covered here. This is not a full fledged example of code to do anything useful.

The first step is to setup all of the DTLS information needed to talk to the base station.

Step #1 - setup the key structure which holds the key for DTLS. I have not found that a key identifier is needed, so that line is commented out below.

OneKey userKey = new OneKey();
userKey.Add(CoseKeyKeys.KeyType, GeneralValues.KeyType_Octet);
userKey.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(Encoding.UTF8.GetBytes("pass phrase")));
// userKey.Add(CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes("user name")));

Step #2 - create the CoapClient object that is going to be used to talk to the server

CoapEndPoint ep = new DTLSClientEndPoint(userKey);
CoapClient client = new CoapClient(new Uri("coaps://ikea.device")) {
    EndPoint = ep
};
ep.Start();    //  Note I hope to make this not needed in the future,
               //  but if you don't do it then it will not work.

Step #3 - Find out what resources are on the server

IEnumerable<WebLink> resources = client.Discover();
foreach (var resource in resources) {
    Console.WriteLine($"Resource = {resource}");
}

Step #4 - Retrieve the current state of a bulb. UriPath is relative to the URI currently set for client.

client.UriPath = "15001/65537";
Response response = client.Get();
Console.WriteLine($"The bulb 15001 is in state {response.PayloadString()}");

Step #5 - Change the state of the bulb

client.Put("{ \"3311\":[{ \"5851\":127}]}");

Step #6 - Just to be polite - stop the DTLS server

client.Endpoint.Stop();
Clone this wiki locally