as days pass by — A software Matter device that Alexa can talk to

By admin
What’s-a matter you? Hey!

Gotta no respect?

What-a you t’ink you do, why you look-a so sad?

It’s-a not so bad, it’s-a nice-a place

Shaddap Your Face
PERSON

,

Joe Dolce
PERSON

(If you just want to know about how to make your own Matter device in software and don’t want to read a whole story to get to that point, then

first
ORDINAL

of all that’s, I mean, that’s fine, it’s not a problem, you go ahead, I’m not hurt at all, definitely not, and

secondly
ORDINAL

skip down to Part

4
CARDINAL

or check out the

Github
ORG

repo. But, I mean, really? You’re that busy? You might wanna find a way to chill out a bit. Have a cup of tea. Enjoy life. It’s better, I promise.)

Part the

first
ORDINAL

: the kingdom of the blind

I’ve got new window blinds in my flat. They’re pretty cool; ivory and purple, we’re all very pleased. They’re from

Hillarys
PERSON

, who don’t get a link because although the actual blinds are great, their customer service is… less so, so I’m loath to recommend them. But

one
CARDINAL

of the neat things about them is that they’re electrically operated. There’s a remote control; press the up or down button and they go up and down. Some people would be like a fascinated small child with this technology and spend

half
CARDINAL

of the first day just pressing the buttons and giggling, but not me, no no.


One
CARDINAL

of the questions I was asked when speccing them out was: do you want them to be controllable by

Alexa
PERSON

? And I thought: well, yes, I would like that. Being able to command my blinds to open or close is

one
CARDINAL

more step on the quest to be a

James Bond
PERSON

villain. All I need after that is a monorail and a big picture on the wall which revolves to show my world domination map and I’m golden.

But… I do not like or trust the

IoT
CARDINAL

industry. I’m not entirely against the whole concept — I mean, I have an

Amazon Echo
ORG

, after all. It gets used for cooking timers, my shopping list, and music, but it doesn’t get used for

IoT
CARDINAL

stuff because I don’t have any. This is, basically, because the whole IoT industry is full of grifters and thieves and I do not trust them with my data or with still being around in

two years
DATE

. The whole idea that I speak to a thing in my flat, that thing routes my command up to some server run by an

IoT
CARDINAL

vendor, and then the

IoT
CARDINAL

vendor’s server speaks to another thing in my flat to make it do work… this is a stupid plan. It’s the vendor forcibly inserting themselves as a middle man for literally no reason other than to exploit me later. Because they’ll get bored or bought in

a few years
DATE

and they’ll shut down the servers, with a sorry-not-sorry email about how it’s “no longer viable to support” a thing I paid for and therefore they’re turning it into a brick, but I can buy a replacement for the low low cost of… anyway, I’m not doing it. No. I don’t want some random vendor’s server to punch a hole into my internal network, I don’t want my stuff to stop working if they turn their servers off or they go down, and the whole concept is hugely irritating.

(You might be thinking: why do you have an

Echo
LOC

at all, then? That’s got all the same problems! And you’re not wrong. But I trust

Amazon
ORG

to still be around for a while. Trusting them with my data is another thing, of course, but I’m already on board with that… although I entirely sympathise with people who choose to not do so either from distrust or from objections to their crappy employment or sales practices.)

Anyway, I looked up these blinds and the

Alexa
PERSON

integration they do comes via a company called

Somfy
ORG

. Their APIs seem semi-reasonably documented and I’ve heard nothing specifically bad about them as a company… but I still don’t like the idea. If I were

Matthew Garrett
PERSON

then I would probably find joy in reverse-engineering whatever the protocol is and making it work for me, but I’m not as clever as he is. And they’ll get bored or bought: I’m not sure I trust them to keep their servers running for

years
DATE

and years. Maybe I’d be OK with a thing that required an internet connection to someone else’s server and only let me fiddle with that to the extent that I am given permission for, if I’d only expect to keep that thing for a short time, but these are window blinds. How often do you change your window blinds? I expect these will still be here in

twenty years
DATE

! Do I expect these servers to also be there that long? Hell no. So, I’m not doing that.

Part the

second
ORDINAL

: keep government local

I do actually have

one
CARDINAL

“IoT” device in my flat, though. It’s a remotely-controllable wall socket. It’s from a company called

LocalBytes
ORG

, and it’s basically a “smart plug”: it’s a cylinder that plugs into a wall socket, and has another socket on the other side of it, rather like one of those mini gangplug cube things.

It contains a little microcontroller and wifi chip, and it runs software called

Tasmota
ORG

. And it’s entirely locally controlled; you get a

Tasmota
ORG

app (or talk the documented protocol yourself from code) which can send the plug a command to turn on and off (and also a bunch more complex stuff such as turning on at a specific time), and it involves no internet servers at all. I can’t get screwed by you turning off your servers (or failing to secure them properly) if there aren’t any. Now, I would not recommend these

Tasmota
ORG

devices to a normal person; the app is unreliable and pretends it can’t find the device some of the time, and the configuration API is obscure and weird. But I am, as has been said by kings and queens, not a normal person. I’m a computer bloke. So I am OK with this, and I’d be OK with something similar to control my blinds; something that runs locally and accepts commands from

Alexa
ORG

and then talks to the blinds to open and close them.

Now, the bit that actually talks to the blinds, I haven’t started working on yet. As far as I can tell from reading, the remote control works on a standard “smart home” frequency of

433MHz
CARDINAL

, and there are loads of little dongles and boards that plug into USB or

Raspberry Pi GPIO
PRODUCT

pins which can talk that. I’ll get to that eventually; once I have a working thing, making it talk

433MHz
CARDINAL

is step

2
CARDINAL

. Step

1
CARDINAL

is to make something that I control and which

Alexa
PERSON

can talk to, but which doesn’t require servers on the internet to make it work. This rules out writing a custom

Alexa
PRODUCT

skill; I can do that, and have, but you can’t write a skill which makes the actual

Echo
LOC

in my flat do network connections. The connection comes from

Amazon
ORG

’s servers, which means I’d have to put my little device on the internet, which I don’t want to do. The concern of the servers going away doesn’t apply here — if

Amazon
ORG

’s servers go away, my

Echo
LOC

stops working anyway and all of this is moot — but I do not want to punch a hole into my internal network from outside, and I shouldn’t have to. This is

one
CARDINAL

thing in my house talking to another thing in my house.

This problem used to be unsolvable. And then, just like in the beginning of all things, someone invented

Matter
PERSON

.

Part the

third
ORDINAL

: does it

Matter
PERSON

?

It is, obviously, stupid that every device invents its own protocol to talk to it, and none of it’s documented, and none of it’s standardised, and everything gets reinvented from scratch by programmers who clearly have their minds on lunchtime. So, equally obviously, the way to solve this is to have all the biggest tech companies in the world collaboratively agree on a standard way to do things.

I can hear you laughing from here.

This has, equally equally obviously, resulted in a disastrous

nine
CARDINAL

-way collision of corporate speak, absurd specifications, paywalls for things, requirements to sign up as a “partner” before you’re told anything, and documentation by press release. But to my surprise it has actually resulted in something! The

Matter
PERSON

specifications have basically everybody on board —

Amazon
ORG

,

Apple
ORG

,

Google
ORG

, most of the big players in the device world — and have finally started to actually appear in real shipping products rather than only in conference talks. The

Amazon Echo
ORG

supports

Matter
PERSON

back to the

3rd
ORDINAL

gen Dot (there’s a list of

Matter
PERSON

-supporting

Echo
LOC

devices here). If you’ve got a

Matter
PERSON

-supporting thingy, then it’ll have the

Matter
PERSON

logo on it:

and then you can say “Alexa, discover devices” and your

Echo
LOC

will search and then say “I have found a

Matter
PERSON

device!”. You then use the

Alexa
ORG

app on your phone to pair it, either by scanning a QR code or typing in a code, both of which should be on the device itself.

Now,

Matter
PERSON

is a big corporate spec and wants to deal with all sorts of complicated and detailed edge cases. In particular, there is a standard problem with an IoT device in your house, which is that you can’t talk to it until it’s on the wifi, but you can’t put it on the wifi without talking to it. This normally involves the device pretending to be a wifi access point, and you connecting to it with a mobile app you have to install, but

Matter
PERSON

attempts to improve this; a

Matter
PERSON

device can potentially exchange data over wifi, over

Bluetooth
ORG

, over some extra network thing called

Thread
PERSON

that I don’t understand, over ethernet, the works. A lot of the setup and spec for

Matter
PERSON

involves dealing with all this.

I, personally, for myself, for this device, do not care about this. If I were making a device that would be sold to real people, I’d implement all this stuff. But since it’s just me, I’m OK with requiring that my window blind device is already on my wifi, and getting it that way is my problem, before I try detecting it with Alexa.

So, I need a way to make a

Matter
PERSON

device; it only has to deal with wifi. The

Matter
PERSON

specification describes how to talk

Matter
PERSON

, but there’s a lot of detail in there. Surely someone has already implemented this stuff?

Well, they have… but it’s not great. The @project-chip/connectedhomeip Github repository, named back in

the days
DATE

when

Matter
PERSON

was still called “project CHIP” before

the Branding People
ORG

got their hands on it, is the reference implementation. And it’s a nightmare. It’s all in C, it’s huge (I checked out the repo and all its submodules and ran out of disk space; to get the repo and all sub-repos and then build it, you’d better have

30
CARDINAL

GB or so free), compiling it all is a massive headache, it contains code for all sorts of different single-board computers to make them

Matter
PERSON

devices, and I couldn’t make head nor tail of it. I’m sure you need all this if you’re looking to build a device and ship

millions
CARDINAL

of them to people in Best Buy and

Currys
PERSON

, but I ain’t. Normally, people write stuff like this in

Python
ORG

, but in looking around all I could find was a

Python
ORG

library designed to work with

Home
ORG

Assistant, which is not what I wanted (I want to make a device, not a controller) and which required the CHIP SDK anyway, the big complicated nightmare thing mentioned above. I resigned myself to having to write a very noddy implementation of enough of

Matter
PERSON

’s pairing and communications stuff in Python to get a device up and running, and bitched about having to do this on social media. And then

Alan Bell
PERSON

, hero of the revolution, said: have you tried matter.js?

I had not. So I gave it a look.

Part the

fourth
ORDINAL

: yes it Matters!


Matter.js
CARDINAL

is an implementation of the

Matter
PERSON

suite of protocols in

JavaScript
ORG

. It does not build on the huge complicated

Project CHIP SDK
ORG

; it’s its own thing. And it just works.

If you want to build a Matter device in

JavaScript
ORG

, take a look at the

Matter.js Node
PRODUCT

examples and they work fine. You can do that right now.

I did something a little tiny bit different. The

Matter.js
CARDINAL

stuff is not actually JS; it’s

TypeScript
ORG

. I don’t like

TypeScript
ORG

and so I wanted a plain JS thing. So I did a simple reimplementation of the simplest

Matter.js
CARDINAL

example in plain JS, and made it even simpler; the original correctly lets you pass in a bunch of details on the command line to configure it, but I’m not bothered about that and wanted to keep it as simple as possible. So, the simplest possible “virtual” Matter device implemented in

JavaScript
ORG

. It’s a lightbulb; Alexa, and

Matter
PERSON

, do support multiple types of devices (including window blind controllers!) but

Matter.js
CARDINAL

only does lightbulbs and sockets at the moment. I’m probably going to build and contribute the window blind controller device type, if someone else doesn’t get there

first
ORDINAL

. Anyway, let’s get this code running! Use is pretty easy for a developer:

$ git clone

https://github.com/stuartlangridge/simple-js-matter-device.git Cloning
PERSON

into ‘simple-js-matter-device’ … remote: Enumerating objects:

11
CARDINAL

, done . remote: Counting objects:

100 %
PERCENT

(

11
CARDINAL

/11 ) , done . remote: Compressing objects:

100 %
PERCENT

(

10
CARDINAL

/10 ) , done . remote: Total

11
CARDINAL

( delta 0 ) , reused

7
CARDINAL

( delta 0 ) , pack-reused 0 Receiving objects:

100 %
PERCENT

(

11
CARDINAL

/11 ) ,

25
CARDINAL

.16 KiB | 1 .48 MiB/s, done . $ cd simple-js-matter-device $ npm install added

90
CARDINAL

packages, and audited

91
CARDINAL

packages in

5s 5
CARDINAL

packages are looking for funding run ` npm fund ` for details found

0 vulnerabilities $
MONEY

node sil.mjs

2023
CARDINAL

-10-06 11 :

38:24.231
CARDINAL

INFO Device node-matter

▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ █ ▄▄▄▄▄ ██
ORG



PERSON

▄▀

▄█ ▄▄▄▄▄ █ █ █ █ █▄
ORG

▀▄▀▄█ █ █ █ █ █

▄▄▄█ █
ORG

█ ▀▀▄█ █

▄▄▄█ █ █
ORG


▄▄▄▄▄▄▄█ █
ORG



▄█▄▄▄▄▄▄▄█ █ ▄ █▄ ▄▄▄█ █ ▄
ORG

▀██ █▀█ ██▀ █▀▄█▄ █


PERSON

▀▄█


PERSON

█▀█ ██▄█

▄▄█▄▄█▄
ORG

▄▀


PERSON

▄ █▄▀▄▀█ █ ▄▄▄▄▄ █

▄█▄█▄▄ ▄ █ █ █ █ █ █▄▄ █▄ ▄
ORG



PERSON

▄ ▀██ █ █

▄▄▄█ █
ORG

▀█▄█


PERSON

█ ▀ █ █▄▄▄▄▄▄▄█▄███▄██▄██▄█▄█


PERSON

▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

2023
CARDINAL

-10-06 11 :

38:24.330
CARDINAL

INFO Device QR Code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data = MT:Y.K90-Q000KA0648G00

2023
CARDINAL

-10-06 11 :

38:24.330
CARDINAL

INFO Device Manual pairing code:

34970112332
CARDINAL

We’ve git cloned the repository, done npm install to install the dependencies, and run it. That starts up our virtual device, and also prints the QR code and manual pairing code used for pairing. Job done; your device now exists.

Next, we teach

Alexa
PERSON

about it. Exercise your most commanding voice and say “Alexa, discover new devices!” Your Echo will tell you it’s doing it, and then after

a few seconds
TIME

of searching, tell you that it’s found

one
CARDINAL

Matter device and send you off to the devices section of the

Alexa
ORG

app on your phone to connect it up; I also got a notification on my phone telling me the same thing.

Hit that notification and you end up on the

Devices
ORG

screen, where it’ll tell you to “Connect your Matter Light Bulb”:

(For some reason, my device seems to present as

two
CARDINAL

separate devices; a light bulb and an unnamed “Matter device”. I don’t know why. It seems that you can pick either.)

Choose to connect to this

Matter
PERSON

device, and you get a screen called “Control your Matter device with

Alexa
ORG

”, which is exactly what we want to do:

When asked if your device has a

Matter
PERSON

logo, say yes (it would do if it were a real

Matter
PERSON

device, of course):

Then you have to scan the QR code. Usefully, the device has printed out a QR code in the

Terminal
LOC

where you ran it, above; scan that. If that didn’t work for some reason, it has also printed a URL for that QR code which you can follow to show the QR in the browser, and if that doesn’t work either, you can enter the numeric code it printed (the “manual pairing code) instead:


Alexa
PERSON

will then claim to be “Looking for your device” (and the stuff in the

Terminal
ORG

will print a bunch of logging things about “Pase server: Received pairing request from udp://192.168.1.103:5541” and whatnot):

and then, all else being good, you’ll get a screen telling you your light is connected! It’s called “

Kryogenix Light
WORK_OF_ART

” in this screen, and “Stuart Light” elsewhere; you can see those strings in sil.mjs and can customise them to your heart’s content.

You should now have a “Lights” section in the

Devices
ORG

screen of your

Alexa
ORG

app. You can use this to turn the “light” on and off from the

Alexa
ORG

app, or you can say “

Alexa
PERSON

, turn

Stuart Light
PERSON

on” to do it by voice. When you do that, the

Terminal
ORG

should print !!!!!!!!!!!!!!!! GOT COMMAND true (or false ), which means it’s calling the onOffListener we defined in sil.mjs. You can customise this to do whatever you’d like!

And that’s it working. That’s a software device, which you can pair with

Alexa
ORG

and customise how you choose; it doesn’t require you to write an Alexa skill, and the device does not need to be accessible from the internet. That’s just what I want.

Now I suppose I have to make it do something useful…