The M5Paper was one of my luckily few pandemic boredom impulse buying. I planned to use it as a hot key keyboard like a Stram Deck, but this never happened. I only flashed it with the calculator demo example app and multiplied 42 by 1337.
After moving in our new House, I found it in a bow with many other never finished projects when I was furnishing my man cave. I took it up and placed it on my desk for a while, hence I am still thinking this is a cool device due to its ESP32 in combination with its large E-ink display with touch, and it has got a build in magnet for easy mounting like the original Tradfri Buttons. Maybe you want to check this GitHub repo out if you want to see the other features with code example packed in this little white case.
And here I found a new purpose for this lonely M5Paper module, I could use it, to control my IKEA Tradfri Lights and combine the best of two worlds, the simple wireless tiny white Tradfri buttons and the "feature rich" smartphone APP where you can control all devices and scenes and maybe adding some more comfort features, too.
For this project I decided to abandon my Tradfri gateway and replace it with the new Dirigera gateway. The main reason is that IKEA stopped selling the Tradfri gateway, and so the Project would start using a product which reached it's EOL. The Dirigera gateway has some advantages over the Tradfri gateway, too. The Android App is a little bit more polished, Pairing devices is simpler and Light can be paired without a button. And some technical aspects makes communication with the new gateway easier, instead over UDP via COAP which is an HTTP / REST inspired and DTLS encrypted protocol the Dirigera gateway provides a simple REST API over HTTPS.
Let's sort all things out before we start coding, one of my first steps is to check whether a communication between my PC and the Gateway works.
I prefer building some example code before doing all the API communication on the ESP32 in C. So I wrote a basic python client which turn on a simple light.
Luckyly enough, there are already two client libraries for the gateway, one for TypeScript and another one for Java, and both are working! With the two example libraries, building a basic python client was very simple. The most code is the authentication part, which reveals a token in JWT format. The steps are as following, generate a 128 char long random string where the valid chars are [a-zA-Z0-9], generate an SHA 256 hash from the random string and base64 encode it. Send an HTTP GET with the encoded string as code_challenge
to the gateway and remember the code
from the HTPP response, wait for a button press, send the random string (not...