Create a Matter over Thread temperature sensor based on Seeed Studio XIAO MG24

5
(1)

In this tutorial, I will explain how to create a Matter over Thread temperature sensor based on Seeed Studio XIAO MG24 board, and how to integrate it into Home Assistant.

This tutorial is divided into four main parts :

  1. The Matter standard
  2. Thread protocol
  3. Design of a Matter over Thread temperature sensor built on Seeed Studio XIAO MG24
  4. Intregration of this sensor into Home Assistant

The objectives of the Matter standard

Matter has been designed to improve the interoperability and security of connected objects (IoT) used in home automation. Matter’s goal is to simplify the user experience and strengthen trust in connected ecosystems. Matter offers a unified standard that guarantees the interoperability and security of a wide range of connected objects.

The Matter standard was developed by the Connectivity Standards Alliance (CSA) consortium. Its supporters include technology giants such as Apple, Amazon, Google, and Samsung, as well as leaders in the home automation field such as Legrand, Schneider Electric, and Somfy.

Matter simplifies the commissioning of devices from different manufacturers, guarantees their interoperability, and ensures they interact securely. Connected objects compatible with the Matter standard can therefore be used within all home automation solutions, whether proprietary (Somfy TaHoma, Apple HomeKit, Google Home, Amazon Alexa, etc.) or open (Home Assistant, OpenHAB, etc.).

Matter standard layers

Matter is an IPv6-based standard designed to work across multiple network technologies, such as Wi-Fi, Ethernet, and Thread.

The Message Reliability Protocol (MRP) layer in the Matter standard manages message routing within the mesh network, ensuring reliable and efficient communication between devices. It optimizes data paths to minimize latency and power consumption, while ensuring network robustness against node failures. MRP also enables interoperability between different network types, such as Wi-Fi and Thread, by unifying their management.

The Bluetooth Transport Protocol (BTP) layer in the Matter standard uses Bluetooth Low Energy (BLE) to facilitate the device commissioning process. It enables the secure transmission of network credentials required to connect new devices to the Matter network. BTP plays a crucial role in device initialization and configuration, ensuring smooth and secure onboarding without requiring complex user interaction.

The choice of Ethernet, Wi-Fi, or Thread depends on the needs and characteristics of the Matter device.

Ethernet enables stable, reliable, and more secure communications than wireless connections, as they are less susceptible to interception.

Wi-Fi is particularly suited to mobile devices or devices that require flexible placement, such as smartphones or IP cameras.

Due to its low power consumption, Thread is particularly well-suited to battery-powered devices. Furthermore, Thread’s mesh topology improves network range and reliability.

BLE does not integrate the IP layer and is therefore only used during the commissioning process of a new device in the Matter framework.

The Matter data model

To ensure seamless interoperability, Matter defines a data model, which is a hierarchical model of a device’s functionalities.

Many device types are modeled in the Matter standard specifications, including, for example:

  • Lighting
  • Smart plugs and outlets
  • Switches and Controls
  • Sensors
  • Locks
  • Media
  • Household Appliances
  • Energy Devices

Each Matter device is modeled by a specific data model, precisely describing its functionalities (e.g., dimmable lamp), its attributes (e.g., on or off state), and the commands (e.g., turn on or turn off) that can be applied to it.

In the example below, we see the data model for a dimmable lamp. The first endpoint corresponds to the basic functionality, with the manufacturer and product names as attributes. The second endpoint corresponds to the functionality related to the lamp itself (its status, brightness, commands it can receive, etc.).

A device does not transmit its entire data model over the network; it provides crucial information through its descriptor cluster, allowing other devices to understand and interact with its functionality. A client (e.g., an app on a smartphone) uses the information found in the descriptor cluster to model the Matter device (light, switch, pump, thermostat) and its functionality, and display an appropriate user interface.

Matter’s security

Matter does not rely solely on the security of the communication technologies it relies on (IPv6, Ethernet, Wi-Fi, Thread). Instead, the Matter specification places a strong emphasis on security, notably by guaranteeing device authenticity, using strong cryptographic protocols, and ensuring the confidentiality and integrity of communications between Matter devices.

Security is integrated into several aspects of the standard, including the commissioning process, with a rigorous device authentication process. Each Matter device must prove its authenticity before being integrated into the network. This verification relies on manufacturer certificates and ensures that only legitimate devices are authorized, thus strengthening the overall security of the system. Each Matter device receives two certificates. The first, the device certificate, is programmed by the manufacturer before delivery. Wi-Fi or Thread network keys are only provided to the device after this certificate has been verified and authenticated. The second, the operational certificate, is assigned by the commissioner during commissioning. This certificate is used to prove the device’s authorization on the Matter network and to identify it securely.

Commissioning a new Matter device

The device that handles the commissioning process is called a “Commissioner.” The commissioning requester is the new device that must be commissioned into the Matter network.

Matter supports several commissioning methods, depending on whether the device is already connected to the network (e.g., via an Ethernet cable) or requires network credentials for Wi-Fi or Thread. In the latter case, Wi-Fi and Thread device credentials are configured via Bluetooth Low Energy (BLE) before they can be connected to the operational network.

A QR code is typically provided with a Matter device. It contains several pieces of information, including the manufacturer and product identifiers, as well as a password. This password is used by the commissioner (usually a smartphone), which will establish the BLE connection with the Matter device and commission it into the Matter network.

Example of a QR code supplied with a Matter device and used for its commissioning on the Matter network

Commissioning can also be done with a manual 11-digit pairing code provided by the Matter device. This must be entered into the app, and then follows the same process as QR code commissioning.

Commissioning QR Code content

A QR Code is a graphical representation of information (payload) that depends on its use. The content of a QR Code used as a Matter commission code is defined by MT: and contains the following information:

  • Vendor ID: Product manufacturer.
  • Product ID: Product identifier.
  • Commission flow: Instructs the commissioning software how to configure the product (0: Standard, 1: User-Intent, 2: Custom).
  • Discovery capabilities: This number tells your home automation app how to find the device (0: SoftAP, 1: BLE, 2: OnNetwork).
  • Discriminator: A unique number to distinguish between similar devices.
  • PIN: This is an 8-digit numeric password.

You can use the following Python program to encode or decode a Matter commission QR Code: https://github.com/project-chip/connectedhomeip/tree/master/src/setup_payload/python

Thread is a low-power wireless communication protocol designed for home networks and connected devices. It uses the 2.4 GHz frequency band and supports IPv6, allowing easy interoperability with other standards. Thread’s main benefits are its low power consumption and its ability to create reliable and secure mesh networks, ideal for smart home environments.

Like Zigbee, Thread is particularly well-suited to battery-powered devices (unlike Wi-Fi). As a reminder, Matter works on Thread, Ethernet, or Wi-Fi, but not on Zigbee.

A Thread border router connects a Thread network to other IP networks, such as Wi-Fi or Ethernet.

Topology of a Thread network and connection to over network through a Thread Border Router

In our tutorial, the Raspberry Pi equipped with the Sonoff ZBDongle-E will act as the Thread Border Router, while the Seeed Studio XIAO MG24 will function as a Thread device.

Thread network architecture used for this tutorial

Flashing Thread firmware on the Sonoff ZBDongle-E dongle

The Sonoff ZBDongle-E dongle comes standard with firmware that only allows it to communicate with Zigbee devices. You must therefore flash new firmware so that it can communicate with Thread radio protocol.

The website https://darkxst.github.io/silabs-firmware-builder allows you to flash the dongle directly from your Chrome browser.

After inserting the dongle into your PC, click CONNECT and change the firmware by selecting OpenThread firmware.

Connection between Thread and Matter

After this short introduction to Matter and Thread, you now understand that Thread and Matter serve different purposes and operate at different layers of the technology stack. To recap:

Thread:

  • Thread is a low-power, wireless mesh networking protocol designed for connected home devices. It provides a reliable and secure way for devices to communicate with each other and the internet.
  • Thread creates a local network that allows devices to talk to each other even if the internet goes down.

Matter:

  • Matter is an application-layer protocol that sits on top of networking protocols like Thread, Wi-Fi, and Ethernet. It aims to simplify and unify the smart home ecosystem by ensuring that devices from different manufacturers can work together seamlessly.
  • Matter defines how devices communicate and interact at the application level, focusing on interoperability, security, and ease of use.

Connection between Thread and Matter:

  • Matter can use Thread as one of its underlying network protocols. This means that devices using the Matter protocol can communicate over a Thread network.
  • The combination of Matter and Thread allows for a robust, secure, and interoperable smart home ecosystem where devices can communicate locally and efficiently.
Example of smart home network topology
Source : https://www.threadgroup.org/BUILT-FOR-IOT/Smart-Home

The Seeed Studio XIAO MG24 board is based on Silicon Labs EFR32MG24 wireless SoC. This SOC is ideal for mesh IoT wireless connectivity using Matter & Thread for smart home, lighting, and building automation products.

To make it accessible to everyone, we will program our XIAO MG24-based sensor using the Arduino IDE.

First step is to install the Silicon Labs board manager in Arduino IDE.

Then, you can select the Seeed Studio XIAO MG24 board.

Arduino sketch

The following program is reading temperature and humidity on an BME280 sensor connected via I2C interface to the XIAO MG24 board. You will need to install BME280 library (https://github.com/finitespace/BME280) if you use this sensor.

In this sketch, the microcontroler reads data from temperature sensor, send it to Home Assistant via Matter over Thread and enter in sleep mode for 10 minutes.

C++
/*
  Matter temperature and humidity sensor designed for Seeed Studio XIAO MG24

  This code is based on example provided by Tamas Jozsi from Silicon Labs
  https://github.com/SiliconLabs/arduino/blob/main/libraries/Matter/examples/matter_sensor_multiple/matter_sensor_multiple.ino

  https://tutoduino.fr/
*/

#include <Matter.h>
#include <MatterTemperature.h>
#include <MatterHumidity.h>

#include "ArduinoLowPower.h"

#include <BME280I2C.h>
#include <Wire.h>

BME280I2C sensor;
float humidity;
float temperature;
float pressure;

MatterHumidity matter_humidity_sensor;
MatterTemperature matter_temp_sensor;

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("Matter over Thread temperature and humidity sensor based on Seeed Studio XIAO MG24 board");

  // Start temperature sensor
  Wire.begin();
  while (!sensor.begin()) {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }

  Serial.println("BME280 Temperature and humidity sensor detected");
  sensor.read(pressure, temperature, humidity);
  Serial.printf("Temperature: %.02f\n", temperature);
  Serial.printf("Humidity: %.02f\n", humidity);
  Serial.printf("Pressure: %.02f\n", pressure);

  // Start Matter
  Matter.begin();
  matter_temp_sensor.begin();
  matter_humidity_sensor.begin();

  matter_temp_sensor.set_device_name("XIAO_MG24");
  matter_temp_sensor.set_vendor_name("Seeed_Studio");
  matter_temp_sensor.set_product_name("Sensor_by_Tutoduino");


  if (!Matter.isDeviceCommissioned()) {
    Serial.println("Matter device is not commissioned");
    Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
    Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str());
    Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str());
  }

  while (!Matter.isDeviceCommissioned()) {
    delay(200);
  }

  Serial.println("Matter device is commissioned, waiting for Thread network...");

  while (!Matter.isDeviceThreadConnected()) {
    delay(200);
  }

  Serial.println("Device is connected to Thread network");

  Serial.println("Waiting for Matter device discovery...");

  while (!matter_temp_sensor.is_online() || !matter_humidity_sensor.is_online()) {
    delay(200);
  }

  Serial.println("Matter device is now online");
}

void loop() {

  // Delay to operate after sleep mode exit
  delay(1000);

  // Get temperature and humidity from sensor
  sensor.read(pressure, temperature, humidity);
  Serial.printf("Temperature: %.02f\n", temperature);
  Serial.printf("Humidity: %.02f\n", humidity);
  Serial.printf("Pressure: %.02f\n", pressure);

  // Set temperature and humidity to Matter
  matter_temp_sensor.set_measured_value_celsius(temperature);
  matter_humidity_sensor.set_measured_value(humidity);

  // Going to sleep mode for 10 minutes (600 s = 600.000 ms)
  LowPower.sleep(600000);
}

Once this example is uploaded to the Xiao MG24, the manual pairing code and a link to view the commissioning QR Code is displayed in the serial monitor of Arduino IDE.

URL of QR Code as displayed in the Serial Monitor of Arduino IDE

Open the URL displayed in the serial monitor with your browser; it allows you to view the QR Code that will be used by the Matter commissioning application.

Commissioning QR Code generated by the CHIP projet website

Installing Home Assistant on Raspberry Pi

Install Home Assistant on a microSD card with Raspberry Pi Imager by following the procedure described at https://www.home-assistant.io/installation/raspberrypi/. Please note: You must use a microSD card with at least 32 GB of storage.

A few minutes after connecting the Raspberry Pi, you will be able to access Home Assistant from a browser on your computer at the following URL: http://homeassistant.local:8123/

When you first use Home Assistant, you must create your smart home, a user, and your location (useful for weather forecasts, for example):

If everything went well, Home Assistant will display an overview page with your username and the local weather (at this point we will ignore the devices it has already discovered):

The first step is to add the Matter Server add-on in Home Assistant and start it. If the Matter integration is not automatically added, add it.

Second step is to add OpenThread Border Router add-on. This add-on allows you to create or join a Thread network and turn Home Assistant into a Thread Border Router.

Configure the following options and then start the add-on module:

  • Select the serial port to which the Sonoff dongle is connected
  • Deselect hardware flow control and automatically flash firmware.

Once the OpenThread Border Router add-on is started, the Thread service should be automaticall added and configured. If not, do it manually.

You should see in the configuration of Thread service the configured Thread network. It should be set automatically as prefered network. If not automically done, set it as prefered network.

Adding a new Matter device to Home Assistant

To add a Matter device to Home Assistant, you need to install the Home Assistant app on your smartphone. The smartphone acts as the Matter commissioner, responsible for adding and securely configuring new devices in a Matter network. The Raspberry Pi is the Matter controller, which will manage Matter devices once added to the Matter network.

Matter over Thread network architecture used in this tutorial

To commission your Matter over Thread device, your smartphone must have the credentials for your Thread network. To sync these credentials between Home Assistant and you Smartphone, follow this menu path on your smartphone: Settings -> Companion App -> Troubleshooting -> Sync Thread Credentials.

To add a new Matter device to your Home Automation system, select ‘Add Matter Device‘ and scan the QR code that comes with the device. In this tutorial, the QR code is generated by the CHIP website using the URL provided by the XIAO MG24 in the Serial Monitor.

You should now be able to see your temperature sensor in Home Assistant.

Enjoy!

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?