# Matter: Start from 0
###### tags: `CHIP` `Matter` `Firmware` `Engineer`
## What's this
Well, I heard about this project(Connect Home over IP/Matter) in an interview two years ago and I have followed this project since last year.
As a engineer, I am kind of new to (IoT) embedded system. So, part of the reason I want to study this project is simply to learn how other engineers do their job and how they design the firmware.
Matter is a relative new protocl, so it's really hard to find other resource except the official website/[github](https://github.com/project-chip/connectedhomeip). That's why I decided to shared what I have learned.
Again, I was a software engineer and just became a firmware engineer recently. So, if you are an experienced firmware engineer, these notes may not help you too much. But if you are also new to embedded system and interested in this project, you will find something helpful here(I hope).
Anyway, this will be a series and it will be more like a blog than a note. You are welcom to leave a comment anytime at any post.
## Build and Run
:::warning
Because they are still developing at this very right moment. So everying may go wrong in this note, keep that in your mind.
:::
### My setup
* Python controller/commissioner running on mac(macOS 11.2.3)
* Openthread rounter running on raspberry pi 2
* Matter device running on [Thunderboard Sense 2](https://www.silabs.com/development-tools/thunderboard/thunderboard-sense-two-kit)
> Actually, there are a lot of other platforms in the Matter examples, but Thunderboard Sense 2 is much cheaper than other EVK.
The first step is easy, clone the project form [github](https://github.com/project-chip/connectedhomeip), and the documents we need are in [doc](https://github.com/project-chip/connectedhomeip/tree/master/docs/guides).
#### The python controller/comissioner
I just don't have any android device on my hand, so I decided to use python version. And it's quite easy to build, just follow [python_chip_controller_building](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/python_chip_controller_building.md).
But you may want to add sudo for this command
`scripts/build_python.sh -m platform`
#### The openthread router
Yes, this protocol needs a router(either openthread or wifi). I use openthread instead of wifi simply because I want to play around openthread.
Here is the doc you can use to build up the rounter:
[openthread_border_router_pi.md](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/openthread_border_router_pi.md)
Make sure you are using the correct version of raspberry pi OS becuase of this statement in the [Code Lab](https://openthread.io/codelabs/openthread-border-router#1)
> instead of using the latest Raspberry Pi OS in the tool, download 2021-05-07-raspios-buster-armhf-lite by yourself
I use Thunderboard Sense 2 as NCP. Please check this [doc](https://github.com/openthread/ot-efr32/blob/main/src/README.md) to build up the NCP.
:::info
The opentrehad is also updating time to time. So if you pull the new version Matter, you may want to check the openthread version too.
:::
#### Matter device
I start with the lock-app example, you could just pick one your like.
All the example have documents to help you to build/compile.
### Run
It's time to run. Again, follow this [doc](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/python_chip_controller_building.md). But be careful about this command:
`connect -ble 3840 20202021 1234`
On efr32 platform, they change this pin code(the second parameter) time to time. You can find the pincode in the BUILD.gn file at the root of the example. Like this:

If you give it the wrong pin code, the PASE handsake session won't be able to calaulate the correct key. And the log will tell you it has the wrong key instead of wrong pin code. Like this:

:::info
I got this error log a while ago, it may have different message now
:::
If everything is going well, you will see this kind of log from the device side:
> Device completed Rendezvous process
Now we have a securiyt channel over BLE, so we can start commission process. At this very moment(2022/02/19, with the git commit hash #14845), it seems like they remove the commands for commission. So I just put the commands I use for commission here:
```shell=
zcl NetworkCommissioning AddOrUpdateThreadNetwork 31 0 0 operationalDataset=hex:<Thread network credentials> breadcrumb=0
zcl NetworkCommissioning ConnectNetwork 31 0 0 networkID=hex:<Ext PANID> breadcrumb=0
resolve <node id>
```
:::info
The 31 is the node ID I used for this example, you must change that to the node ID you use. And you may keep the 2 zeros after 31 and the breadcrumb=0 for now.
:::
:::info
Again, they removed the command for checking the Ext PANID. You can type "dataset" in the openthread command line to check the Ext PANID. Check this [doc](https://github.com/openthread/openthread/blob/main/src/cli/README_DATASET.md)
:::
I believe you could always find the more information by the help of the zcl command. But it will be nice to have examples. Also, if you are using the efr32 lock example, you will find the same example in the [readme](https://github.com/project-chip/connectedhomeip/tree/master/examples/lock-app/efr32).
After the commission, you could find this kind of log to show you that the device has been committed to the openthread network.
>[DL] Thread Unicast Addresses: ...
[DL] LwIP Thread interface addresses updated ...
[DL] OnSrpClientNotification: Last requested operation completed successfully
And if you use the zcl command(toggle/on/off) in the readme, you can find this sort of log on the device side:
>[ZCL] On/Off set value: 1 2
[ZCL] Toggle on/off from 0 to 1
### Run Again
If you made some changes to the example and wanted to run again, you may find out you would be stuck at the resolve command. The thing is, there are bunch of public/private keys and credentials stored in some where. And the matter device and the border router will keep these keys/credentials in the flash but the python controller will kepp these data in the RAM. So the keys/credentials will be gone when you quit the python command line tool.
So, whenever you want to re-run the example, you have to start all over again, that is, from the very first ble-connect command.
I will show you how to change that by simply storing the data in a file next time.