|
| 1 | +--- |
| 2 | +beta: true |
| 3 | +title: 'Open Thread Border Router with Nano Matter & ESP32' |
| 4 | +description: 'Learn how to create your own Thread Border Router using OpenThread and Arduino ecosystem products.' |
| 5 | +difficulty: advanced |
| 6 | +compatible-products: [nano-matter] |
| 7 | +tags: |
| 8 | + - OTBR |
| 9 | + - Thread |
| 10 | + - Matter |
| 11 | + - Linux |
| 12 | +author: 'Martino Facchin, Leonardo Cavagnis and Christopher Méndez' |
| 13 | +hardware: |
| 14 | + - hardware/03.nano/boards/nano-matter |
| 15 | + - hardware/03.nano/boards/nano-esp32 |
| 16 | +--- |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +Thread is a low-power, wireless mesh networking protocol for smart homes and Internet of Things (IoT) devices. A Thread Border Router serves as a bridge between the Thread network and the wider Internet or local networks, allowing devices within the Thread network to communicate with external systems. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +Matter devices can use Thread as their primary communication method, especially for low-power devices such as sensors, light bulbs, and door locks. These devices use the Thread protocol and leverage Matter's application layer for interoperability. |
| 25 | + |
| 26 | +### OpenThread Border Router |
| 27 | + |
| 28 | +An OpenThread Border Router (OTBR) consists of a **Matter Controller** and a **Radio Co-Processor** (RCP): |
| 29 | + |
| 30 | +- The *Matter Controller* is essential for managing devices using the Matter protocol, which ensures interoperability between nodes. It handles: commissioning, communication and network management. |
| 31 | +- The *Radio Co-Processor* (RCP) is dedicated to handling Thread network communications, improving efficiency by offloading radio communication tasks. |
| 32 | + |
| 33 | +The **Nano Matter** is the **RCP**, connected to the **Nano ESP32** (the Matter Controller) via serial port. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Goals |
| 38 | + |
| 39 | +This tutorial's main objective is to guide you through the build and configuration of an OpenThread Border Router. This router will allow you to deploy a Matter network over Thread and integrate Matter devices into your Smart Home system. |
| 40 | + |
| 41 | +- Create an OTBR using Arduino products. |
| 42 | +- Leverage the Nano Matter as a Radio Co-Processor (RCP). |
| 43 | +- Use the Nano ESP32 as a Matter Controller. |
| 44 | +- Integrate a smart outlet based on the Nano Matter to your network. |
| 45 | + |
| 46 | +## Hardware and Software Requirements |
| 47 | + |
| 48 | +### Hardware Requirements |
| 49 | + |
| 50 | +- [Nano Matter](https://store.arduino.cc/products/nano-matter) (x2) |
| 51 | +- [Nano ESP32](https://store.arduino.cc/products/nano-esp32) (x1) |
| 52 | +- Linux Computer (Laptop/PC) (x1) |
| 53 | +- [USB-C® cable](https://store.arduino.cc/products/usb-cable2in1-type-c) (x1) |
| 54 | + |
| 55 | +### Software Requirements |
| 56 | + |
| 57 | +- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) |
| 58 | +- [Simplicity Studio](https://www.silabs.com/developers/simplicity-studio) |
| 59 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 60 | + |
| 61 | +## Setting up the OTBR |
| 62 | + |
| 63 | +### The RCP: Nano Matter |
| 64 | + |
| 65 | +This section outlines the steps to build the RCP firmware for the Nano Matter. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +#### Create a New Project |
| 70 | + |
| 71 | +- Download Simplicity Studio. Silicon Labs provides this IDE, which is designed to simplify the development process for Silicon Labs hardware platforms. Download latest version [here](https://www.silabs.com/developers/simplicity-studio). |
| 72 | + |
| 73 | +- Open Simplicity Studio and create a new project by clicking **File > New > Silicon Labs Project Wizard**. |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +- Set the following project configurations and click on *Next*. |
| 78 | + |
| 79 | +| **Field** | **Setting** | |
| 80 | +| :-----------: | :--------------------------------------------------: | |
| 81 | +| Target Boards | xGM240S 10 dBm Module Radio Board (BRD4318A Rev A04) | |
| 82 | +| Target Device | MGM240SD22VNA | |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +- In the *Examples* tab search for the **OpenThread - RCP** example, select it and click on *Next*. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +- In the *Configuration* tab give the project a name and click on *Finish*. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +#### Change the Serial Port Pinout |
| 95 | + |
| 96 | +The default pinout is **PA8 > TX** and **PA9 > RX**, but we need to change it to **PA4 > TX** and **PA5 > RX**, so they correspond to the **D0** and **D1** of the standard Nano layout. |
| 97 | + |
| 98 | +- Delete the default `USART0` setting: |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +- Add the new setting: |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +- Save your project with the new configurations. |
| 107 | + |
| 108 | +#### Disable USART Flow Control |
| 109 | + |
| 110 | +- Open the `<project-folder>/config/sl_uartdrv_usart_vcom_config.h` file. |
| 111 | + |
| 112 | +- Modify the `SL_UARTDRV_USART_VCOM_FLOW_CONTROL_TYPE` variable as follows: |
| 113 | +``` |
| 114 | +#define SL_UARTDRV_USART_VCOM_FLOW_CONTROL_TYPE uartdrvFlowControlNone |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +#### Build and Flash the Project |
| 120 | + |
| 121 | +- In the upper menu navigate to **Project > Build Project** to compile and build the RCP firmware. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +- In your project directory navigate to the `/GNU ARM v12.2.1 - Default/` folder, right click on it and open a **Command Line**. |
| 126 | + |
| 127 | +Use the following command to flash the firmware to the Nano Matter, make sure to modify the `<username>` and `<project name>` with yours: |
| 128 | + |
| 129 | +```bash |
| 130 | +/Users/<your-username>/AppData/Local/Arduino15/packages/SiliconLabs/tools/openocd/0.12.0-arduino1-static/bin/openocd -d2 -s /Users/<your-username>/AppData/Local/Arduino15/packages/SiliconLabs/tools/openocd/0.12.0-arduino1-static/share/openocd/scripts/ -f interface/cmsis-dap.cfg -f target/efm32s2_g23.cfg -c "init; reset_config srst_nogate; reset halt; program {<project-name>.hex}; reset; exit" |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +***The __Openocd__ tool directory may vary according to your OS, modify the command above respectively.*** |
| 136 | + |
| 137 | +### The Matter Controller: Nano ESP32 |
| 138 | + |
| 139 | +This section outlines the steps to build the Matter Controller firmware for the Arduino Nano ESP32. |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +To set up the environment for the ESP32 firmware development use the following commands on a **Linux computer**. |
| 144 | + |
| 145 | +- Install dependencies (Ubuntu and Debian): |
| 146 | + |
| 147 | +```bash |
| 148 | +sudo apt-get install git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 |
| 149 | +``` |
| 150 | + |
| 151 | +***Search for the correct commands [here](https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/get-started/linux-macos-setup.html#for-linux-users) if you use a different Linux distribution.*** |
| 152 | + |
| 153 | +- Install the **ESP-IDF**: |
| 154 | + |
| 155 | +```bash |
| 156 | +git clone -b v5.1.2 --recursive https://github.com/espressif/esp-idf.git |
| 157 | +cd esp-idf |
| 158 | +./install.sh |
| 159 | +. ./export.sh |
| 160 | +``` |
| 161 | + |
| 162 | +- Clone the ESP Thread Border Router example repository: |
| 163 | + |
| 164 | +```bash |
| 165 | +git clone -b v1.0 --recursive https://github.com/espressif/esp-thread-br.git |
| 166 | +cd /esp-thread-br/examples/basic_thread_border_router/ |
| 167 | +``` |
| 168 | +- For the `sdkconfig` file to be generated, and we can later modify it with our Nano ESP32 custom settings, set the device target from the command line: |
| 169 | + |
| 170 | +```bash |
| 171 | +idf.py set-target esp32s3 |
| 172 | +``` |
| 173 | + |
| 174 | +Using a **code editor**, open the `sdkconfig` file located in `esp-thread-br/examples/basic_thread_border_router/sdkconfig` and do the following modifications: |
| 175 | + |
| 176 | +- Update the Wi-Fi credentials with your network `SSID` and `Password`: |
| 177 | + |
| 178 | +```bash |
| 179 | +CONFIG_EXAMPLE_WIFI_SSID="<your-wifi-ssid>" |
| 180 | +CONFIG_EXAMPLE_WIFI_PASSWORD="<your-wifi-password>" |
| 181 | +``` |
| 182 | + |
| 183 | +- Modify the Serial pinout so it matches with the Arduino Nano layout: |
| 184 | + |
| 185 | +```bash |
| 186 | +CONFIG_PIN_TO_RCP_TX=43 |
| 187 | +CONFIG_PIN_TO_RCP_RX=44 |
| 188 | +``` |
| 189 | + |
| 190 | +- Disable the RCP firmware auto-update verifying this parameter is not set: |
| 191 | + |
| 192 | +```bash |
| 193 | +# CONFIG_OPENTHREAD_BR_AUTO_UPDATE_RCP=y |
| 194 | +``` |
| 195 | + |
| 196 | +- Enable the OpenThread webserver: |
| 197 | + |
| 198 | +```bash |
| 199 | +CONFIG_OPENTHREAD_BR_START_WEB=y |
| 200 | +``` |
| 201 | + |
| 202 | +- Enable OpenThread commissioner and joiner: |
| 203 | + |
| 204 | +```bash |
| 205 | +CONFIG_OPENTHREAD_COMMISSIONER=y |
| 206 | +CONFIG_OPENTHREAD_JOINER=y |
| 207 | +``` |
| 208 | + |
| 209 | +- Navigate to `esp-thread-br/examples/basic_thread_border_router/main/esp_ot_config.h` |
| 210 | + |
| 211 | +- Modify the Serial port baud rate to `115200`, the result should look like this: |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +- Navigate to `esp-thread-br/examples/common/thread_border_router/src/border_router_launch.c`. |
| 216 | + |
| 217 | +- Disable the ESP RCP update process by commenting the following line of the `border_router_launch.c` file, the result should look like this: |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +- Go back to the `basic_thread_border_router` example folder and build the firmware: |
| 222 | + |
| 223 | +```bash |
| 224 | +cd esp-thread-br/examples/basic_thread_border_router |
| 225 | +idf.py build |
| 226 | +``` |
| 227 | + |
| 228 | +- When the build is completed, flash the Nano ESP32: |
| 229 | + |
| 230 | +```bash |
| 231 | +idf.py -p /dev/ttyACM0 flash monitor |
| 232 | +``` |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +***Do not run the `idf.py set-target esp32s3` command again to avoid overriding the customized configurations.*** |
| 237 | + |
| 238 | +### OTBR Assembly |
| 239 | + |
| 240 | +After configuring and flashing the Nano Matter and Nano ESP32 separately, it is time to connect them. As they communicate through a serial connection, you can stack them using breakout headers or jumper wires as follows: |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | +***It doesn't matter which board is on top.*** |
| 246 | + |
| 247 | +### CHIP Tool |
| 248 | + |
| 249 | +**CHIP Tool** is a command-line tool for *commissioning*, *controlling*, and *managing* **Matter** devices within a Matter network. |
| 250 | + |
| 251 | +Due to its high demand for storage space and computational power it must be executed on a separate, more powerful device, such as a Unix laptop (macOS or Linux), that is connected to the same Wi-Fi network. |
| 252 | + |
| 253 | +This third device will handle the complex tasks, while the Nano ESP32 acts as the Matter Controller in the network. |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | +#### Configure the CHIP Tool |
| 258 | + |
| 259 | +- On a Linux system, clone the CHIP Tool repository: |
| 260 | + |
| 261 | +```bash |
| 262 | +git clone --recurse-submodules https://github.com/project-chip/connectedhomeip.git |
| 263 | +``` |
| 264 | + |
| 265 | +- Install prerequisites: |
| 266 | + |
| 267 | +Before building, you must install a few OS specific dependencies. |
| 268 | + |
| 269 | +```bash |
| 270 | +sudo apt-get install git gcc g++ pkg-config libssl-dev libdbus-1-dev \ |
| 271 | + libglib2.0-dev libavahi-client-dev ninja-build python3-venv python3-dev \ |
| 272 | + python3-pip unzip libgirepository1.0-dev libcairo2-dev libreadline-dev \ |
| 273 | + default-jre |
| 274 | +``` |
| 275 | + |
| 276 | +- Open a command prompt in the `connectedhomeip` directory and run the following command: |
| 277 | + |
| 278 | +```bash |
| 279 | +cd connectedhomeip |
| 280 | +scripts/examples/gn_build_example.sh examples/chip-tool out/debug |
| 281 | +``` |
| 282 | + |
| 283 | + |
| 284 | +- To check if the CHIP Tool runs correctly, execute the following command: |
| 285 | + |
| 286 | +```bash |
| 287 | +./out/debug/chip-tool |
| 288 | +``` |
| 289 | +As a result, the CHIP Tool starts and prints all available commands. |
| 290 | + |
| 291 | + |
| 292 | + |
| 293 | +## The Use Case: Smart Outlet |
| 294 | + |
| 295 | +This section provides an example of commissioning and communication over Matter between an end-device node and the OTBR. |
| 296 | + |
| 297 | +### Setup |
| 298 | + |
| 299 | +- **OTBR**: Nano ESP32 + Nano Matter |
| 300 | +- **CHIP Tool**: running on a Linux PC (with Bluetooth capabilities) |
| 301 | +- **End-device**: Nano Matter or other boards compatible with the Silabs Arduino core |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | +### End-Device Configuration |
| 306 | + |
| 307 | +As the *end-device* we are going to use a Nano Matter configured as **Smart Outlet**. |
| 308 | + |
| 309 | +- Ensure the Silicon Labs boards package is installed in the Arduino IDE 2. |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | +- Go to **File > Examples > Matter > matter_on_off_outlet** and flash the example to the Nano Matter. |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | +***By default the example will use the built-in LED as an output, you can easily update the code so it controls a GPIO to activate an external relay.*** |
| 318 | + |
| 319 | +- Once flashed, open the serial terminal and reset the board. Take note of the **manual pairing code**: |
| 320 | + |
| 321 | + |
| 322 | + |
| 323 | +For example: |
| 324 | + |
| 325 | +```bash |
| 326 | +34970112332 |
| 327 | +``` |
| 328 | + |
| 329 | +### Getting Thread Network Credentials |
| 330 | + |
| 331 | +Connect through USB to the Nano ESP32 and run the following command from the Arduino IDE Serial Monitor: |
| 332 | + |
| 333 | +`dataset active -x` |
| 334 | + |
| 335 | +***This command will display the active Thread network dataset as a hexadecimal string.*** |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | +For example: |
| 340 | + |
| 341 | +```bash |
| 342 | +0e080000000000010000000300000f35060004001fffe00208dead00beef00cafe0708fd000db800a00000051000112233445566778899aabbccddeeff030e4f70656e5468726561642d455350010212340410104810e2315100afd6bc9215a6bfac530c0402a0f7f8 |
| 343 | +``` |
| 344 | + |
| 345 | +### Matter Commissioning |
| 346 | + |
| 347 | +Commissioning refers to setting up and integrating a new device into the Matter network. |
| 348 | + |
| 349 | +In this case, commissioning will occur via **Bluetooth**, where the laptop or PC with the CHIP Tool installed will communicate with the end device using a Bluetooth connection. |
| 350 | + |
| 351 | +- Open the terminal on the system where the **CHIP Tool** is running, and execute the following command: |
| 352 | + |
| 353 | +```bash |
| 354 | +./out/debug/chip-tool pairing code-thread <node-id> hex:<thread-network-dataset> <end-device-pairing-code> |
| 355 | +``` |
| 356 | + |
| 357 | +1. Replace `node-id` with the unique identifier for the device you are pairing (you can choose it freely according to your preference). |
| 358 | +2. Replace `thread-network-dataset` with the hexadecimal string representing the Thread network dataset. |
| 359 | +3. Replace `end-device-pairing-code` with the manual pairing code for the end device. |
| 360 | + |
| 361 | +Here is an example using the previously gathered parameters: |
| 362 | + |
| 363 | +```bash |
| 364 | +./out/debug/chip-tool pairing code-thread 1 hex:0e080000000000010000000300000f35060004001fffe00208dead00beef00cafe0708fd000db800a00000051000112233445566778899aabbccddeeff030e4f70656e5468726561642d455350010212340410104810e2315100afd6bc9215a6bfac530c0402a0f7f8 34970112332 |
| 365 | +``` |
| 366 | + |
| 367 | +If commissioning phase works fine, on the end-device serial monitor you will get the following: |
| 368 | + |
| 369 | + |
| 370 | + |
| 371 | +Now we are ready to control the Smart Outlet from the CHIP Tool system. |
| 372 | + |
| 373 | +### Final Result (Testing) |
| 374 | + |
| 375 | +To control the Smart Outlet use the following command format: |
| 376 | + |
| 377 | +```bash |
| 378 | +./out/debug/chip-tool <cluster-name> <command> <node-id> <endpoint-id> |
| 379 | +# formatted command |
| 380 | +./out/debug/chip-tool onoff toggle 1 0x03 |
| 381 | +``` |
| 382 | + |
| 383 | +- `onoff`: This specifies that the command pertains to the On/Off cluster, which controls the device's power state (on/off). |
| 384 | +- `toggle`: This command switches the current state of the device. |
| 385 | +- `1`: This is the Node ID of the device receiving the command. |
| 386 | +- `0x03`: This is the endpoint ID of the device (fixed to 0x03 for Silicon Labs device). |
| 387 | + |
| 388 | +Every time you run the command, the Smart Outlet toggles, turning the connected load on or off. |
| 389 | + |
| 390 | + |
| 391 | + |
| 392 | +## Conclusion |
| 393 | + |
| 394 | +This tutorial showed how to create an OpenThread Border Router using the Arduino Nano Matter and the Nano ESP32 alongside a Linux computer. With this solution you can easily integrate and control Matter Accessory Devices for Smart Homes or Industries. |
| 395 | + |
| 396 | +### Next Steps |
| 397 | + |
| 398 | +Now that you know how to create your own Thread Border Router, you can continue developing a custom platform or application that uses the CHIP Tool API to control Matter end devices from a mobile app or web platform with user-friendly dashboards and interfaces. |
| 399 | + |
0 commit comments