Location>code7788 >text

DIY Matter Bridge and Smart Lock Simple Linkage in Practice

Popularity:232 ℃/2024-10-14 15:33:52

I. Write in front

In a previous blog postThe Practice of Matter Light Based on Loxin ESP32-C3In this post, we implemented a simple Light routine using Loxin's hardware and SDK solution, and gave a brief introduction to the Matter protocol. Before I start this post, I'm going to talk about Matter again, and in the meantime, I'm going to talk about my own understanding of it, which explains why I've been obsessed with it for a while now.

1.1 Background of Matter

In fact, when it comes to the field of smart home, we should be able to associate a lot of brands that are currently doing well, such as Huawei, Green Meter Aqara, O'Brien in China, APPLE, Google Nest and so on overseas. At the same time, they have their own set of ecological protocols, just like Apple has Homekit, Amazon has alexa, Huawei has Hilink.

The development of these ecologies themselves is relatively mature as of today. But for consumers and manufacturers, they may face some headaches:

(1) For consumers: Having multiple eco-products in your home, such as Xiaomi's lights and sensors, Huawei's door locks, and Apple's audio, means that you'll have to use their respective apps if you want to manage them, and it can be difficult to build home automation in this scenario. Of course, one could argue that introducing a Home Assistant gateway to try and solve these problems would be fine for some Geeks, but it certainly raises the bar high for the average consumer.

(2) For device manufacturers: In view of the fragmented ecosystem, we should consider the future interface environment when designing products. For example, smart products going overseas often need to do Amazon's work with alexa (WWA) certification and Google's Work with Google Assistant (WWGA) certification. If it is a high-end product, it may also need to consider the certification of Apple HomeKit and other third-party ecosystems. This definitely increases the difficulty and cycle time of product development, as well as the cost.

So what's the best way to make a device work seamlessly across several different ecosystems and provide a unified interface for users? Matter was born.

1.2 What problem it solves

Matter is essentially an application-level standard developed by the CSA Consortium (Connectivity Standards Alliance) to create a unified standard for smart home applications to eliminate fragmentation in the smart home market.

The CSA Alliance, on the other hand, was formerly known as the Project Connected Home over IP (CHIP) organization, which was formed in 2019 as a collaboration between Amazon, Apple, Google, Samsung SmartThings, and the Zigbee Alliance. At that time, the organization launched an open source project for CHIP, and after some time to prepare and develop it, it was officially named Matter in 21, and is operated and developed by the CSA Alliance. Going back a bit further, you'll find that the CSA Consortium's predecessor was the Zigbee Consortium.

To put it in relatively official terms, Matter is just an application standard whose transport is based on IPv6-enabled TCP and UDP protocols. Matter does not make protocols for the transport layer, and Matter does not make protocols for the network, but Matter does make protocols for connecting to only three protocols: Thread, Wi-Fi, or Ethernet.

In layman's terms, Matter defines a series of device models and interaction models, for example, smart light is one of the standard device types, which defines a number of operations, such as: on/off cluster can complete the action of switching on/off the light, Level control cluster can complete the action of adjusting the brightness. Color control cluster can do the action of color adjustment. For devices, as long as they follow these standard models, they can be understood and used in an ecosystem that supports the Matter protocol, which breaks down the barriers brought by traditional private protocols.

Now, more and more brands and manufacturers, including Apple, Samsung, Google, Amazon, Xiaomi and others, are joining the alliance. In time, consumers will be able to easily build a connected smart home ecosystem and build their own home automation without worrying about the branding of specific devices, simply by choosing their favorite products with the Matter logo.

1.3 Recent developments

(1) Earlier this year, the Alliance released version 1.3 of Matter, which made some improvements in the following areas:

  • New support for water and energy management devices, including energy management, electric vehicle charging and water resource management.
  • New types of appliances have been added, such as microwaves, ovens, ranges, range hoods and washers and dryers.
  • Enhanced management of media devices and smart homes.
  • Enhanced user experience.
  • Improved development and debugging experience.

The specific content of the specification can be found at the following URL:Matter 1.3 specification released

(2) Apple has lowered the restrictions on Home Hubs in the ios 18 developer release. When trying out the ESP solution the previous year, trying to add a device to the iPhone Home prompted the need to add a hub device first, but now with the latest IOS 18 it's possible to add a Matter device directly.

II. Background to the practice

The Smart Deadbolt product we previously defined and developed is now nearing completion. Whereas in the past we wanted it to support Alexa and WWGA for compatibility with overseas ecosystems, now we see Matter, which may be a new solution.

Especially for device manufacturers, the N certifications that we used to need for different ecosystems have increased the demands on developers because of the different technology stacks. In the past, it might have been challenging to have a team of engineers who understood Alexa Skill development and had some knowledge of Apple's HomeKit. Also, the cost of multiple certifications should not be underestimated. With the advent of Matter, it is now possible to greatly simplify these certification processes and compress costs, while still providing a more consumer-friendly experience, and why not.

2.1 About Bridge

Since version 1.0, the Matter Bridge has served as a product category within the standard. Its task is to translate "external" protocols and technologies so that Matter can understand them. This is an important prerequisite for the success of the smart home standard, as not everyone who has purchased a device will want to discard it and replace it with a new Matter-compatible device.

(1) The official description of the bridge

Non-Matter devices are exposed to nodes on the fabric as bridge devices.Matter nodes can communicate with both (local) Matter devices and bridge devices (switching between Matter and other protocols via the Matter bridge).

For example, use CHIP's ZAP tool to build a Bridge device for Matter:

(2) This adopted program:

As we know, in last year's Yale Assure Lock program, they supported a pluggable Matter module to adapt to external ecosystems. From the analysis, this module also functions as a Bridge and communicates with the Lock device via the serial port to respond to external commands.

This practice refers to this form, but because the Lock is already a finished device, it does not realize serial port access on the lock body. Instead, it communicates with Lock's private protocol via Bluetooth, as shown below:

Simply put, the Bridge is actually mapped as a virtual Matter device, and the Matter commands are converted into device commands via private protocols and Bluetooth.

Why two CCKs (Nordic development boards) are used here and the exact process will be explained in Part III.

2.2 Development platforms

(1) Software environment: VSCODE + nRF Connect SDK, where:

  • The toolChains version is v2.6.1.

  • The nRF Connect SDK version is v2.6.1.

  • For more information on how to install this SDK, please refer to the official documentation:Developing nRF Connect SDK (NCS)/Zephyr Applications

(2) Development board:

  • Two Nordic CCK development boards. The CCK boards contain two chips, the nRF5340 and the nRF7002. The nRF7002/nRF7001 are Wi-Fi 6 co-op ICs, they only run the Wi-Fi related MAC layer, the rest of the Matter still runs on the nRF5340.

(3) Ecological applications and accessories:

  • Alexa Echo 4 Japanese version.

  • Samsung Overseas Edition cell phone.

  • Alexa App。

III. Process documentation

3.1 Configuring the development environment

(1) First of all, you have installed Microsoft Visual Studio Code locally, then open it and go to "Extensions" and search for "nRF Connect for VS Code Extension Pack " to install. When this extension is installed, other required nRF plugins will also be installed automatically.

(2) Then click the plugin icon, enter and see Manage SDKs, choose to install the nRF Connect SDK. currently the latest version is 2.6.1, default choose the latest version. The latest version is 2.6.1, so the default is to choose the latest version. It's worth noting that the SDK is officially placed on Github's servers, so you need to have some scientific methods, or else it's prone to download failures.

(3) Next, install Toolchain, you can also install it through Vscode. Select the corresponding version of Toolchain according to the version of nRF Connect SDK you have just selected, and the one installed here is version 2.6.1.

(4) Continue to install the necessary Python scripts. Although most of the Python scripts are already pre-installed in the nRF Connect SDK, there are some Python scripts in the Matter module that are not yet installed. In order to prevent the local Python environment of our computer from interfering with the installation, we recommend using the VS code terminal to execute the commands, which ensures that the environment of the command line is the environment set by the nRF Connect SDK Toolchain. The command reference is as follows:

pip3 install -r C:/ncs/v2.6.1/modules/lib/matter/scripts/setup/

This will automatically fill in the missing Python packages for the nRF Connect SDK Toolchain.

(5) Follow the official guidelines and finally install a ZAP tool. Go to the home directory of the modules\lib\matter repository, then execute the following command and add the root directory of the installation to the operating system variables.

python scripts/setup/nrfconnect/get_zap.py -l C:\Nordic\Tools\zap-win-x64 -o

3.2 Development of a private Bluetooth protocol

Create a new project and write a simple business program with reference to the official Bluetooth central routine. Specify the Bluetooth address of a target Lock and try to establish a Bluetooth connection with it. After the connection is established, pass the authentication information to it according to the security protocol, and get the return from Lock through nodify. If the authentication passes, the connection is maintained and waits to receive Bluetooth commands. Otherwise, the authentication fails and the Bluetooth connection is released.

(1) Given a Bluetooth MAC of our target device:

D8:5F:77:26:1A:0A

(2) When the program starts running, turn on Bluetooth and turn on scanning:

static void start_scan(void)
{
	int err ;

	bool ble_status = bt_is_ready;

	printk("ble status is %s\n", ble_status ? "true" : "false");

	struct bt_le_scan_param scan_param = {
		.type      = BT_LE_SCAN_TYPE_ACTIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
		.window     = BT_GAP_SCAN_FAST_WINDOW,
	};

	err = bt_le_scan_start(&scan_param, device_found);

	if (err) {
		printk("Scanning failed to start (err %0d)\n", err);
		return;
	}

	printk("Scanning successfully started\n");
}

(3) Then, filter out those Bluetooth devices that are too far away from us through device discovery and filter out the target devices from them and try to establish a Bluetooth connection.

static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
			 struct net_buf_simple *ad)
{
	char addr_str[BT_ADDR_LE_STR_LEN];
	int err ;

	if (default_conn) {
		return;
	}

	if (type != BT_GAP_ADV_TYPE_ADV_IND &&
	    type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
		return;
	}

	bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
 
	if (addr -> type != BT_ADDR_LE_PUBLIC){
		return ;
	}

	/* filter the  target bluetooth address */
	if (!strstr(addr_str, lock_ble_mac)){

		return ;
	}

	printk("Device found: %s (RSSI %d)\n", addr_str, rssi);

	/* connect only to devices in close proximity */

	if (rssi < -70) {
		return;
	}


	if (bt_le_scan_stop()) {
		return;
	}

	struct bt_conn_le_create_param create_param = {
		.options = BT_CONN_LE_OPT_NONE,
		.interval = BT_GAP_SCAN_FAST_INTERVAL,
		.window = BT_GAP_SCAN_FAST_WINDOW
	};

	struct bt_le_conn_param conn_param = {
		.interval_min = BT_GAP_INIT_CONN_INT_MIN,
		.interval_max = BT_GAP_INIT_CONN_INT_MAX,
		.latency = 0,
		.timeout = 400
	};


	err = bt_conn_le_create(addr, &create_param,
				&conn_param, &default_conn);
	if (err) {
		printk("Create conn to %s failed (%d)\n", addr_str, err);
		start_scan();
	}
	
}

(4) If the connection is not established successfully, it will restart scanning and retry the connection. If a Bluetooth connection can be established with the target device, then it enters into the service discovery process. According to the security protocol, in this step we will perform a series of authentication actions on the specified Service and Characteristic. After the authentication is completed, the connection enters a stable state and waits to receive the next command. At the same time, define two methods to realize the lock/unlock operation of the specified Lock. Since this part involves product security and internal protocols, we won't go into too much detail.

(5) Finally, define two functions to call the lock and unlock threads:

void unlock_thread_func(void){
	k_thread_create(&unlock_thread, unlock_stack_area, sizeof(unlock_stack_area),
	unlock_func, NULL,NULL,NULL,5,0,K_NO_WAIT);
	k_thread_start(&unlock_thread);
	return;

}

void lock_thread_func(void){
	k_thread_create(&lock_thread, lock_stack_area, sizeof(lock_stack_area),
	lock_func, NULL,NULL,NULL,5,0,K_NO_WAIT);
	k_thread_start(&lock_thread);
	return;
}

3.3 Creating Serial Communication Between Two CCK Development Boards

Q: Why do I need two development boards?

Answer:Because when we write our own private Bluetooth service in the official DEMO, Zephyr will prompt the Bluetooth conflict problem. Because we want to quickly verify and do not want to modify too much Matter code, coupled with the fact that our product staff is not a professional embedded developer after all, the mastery of RTOS is not skilled enough, so simply through the form of the serial port to let the two development boards do their own work, sort of bypass this problem. However, in the subsequent formal program, using a multi-protocol MCU / SOC with sufficient performance and support for Matter Wi-Fi and BLE should be able to solve the problem.

3.4 Modify and burn the official Lock Demo.

(1) Modify to add support for serial ports.

CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=n
CONFIG_RTT_CONSOLE=y
CONFIG_UART_ASYNC_API=y

(2) In the zcl_callback file, add a customized serial port service to notify another CCK when the Lock status changes.

(3) Execute the compile-burn action.

3.5 Development Considerations

When developing Matter, C++ code is used and when developing the Bluetooth business, C code is used. This means that both languages may be present in a project, which is a prominent feature of the Matter application and therefore a complex and challenging task.

IV. Demonstration of effect

Here is mainly to try Amazon's ability to support Matter access, open the official Amazon Alexa application, register an account, and then select Add Device in the upper right corner. In the Shortcuts area you can see that there is a Matter logo:

Once inside, enter the Matter Numeric Code for the piece of CCK that acts as Matter's Virtual Lock, and turn on your phone's Bluetooth (you'll need to bind your Alexa Echo 4 speaker to your account beforehand and make sure it's configured and working). The App will then try to establish communication with the Matter device and pass the verification information.

The process will be long, so you need to be patient and follow the prompts from the Alexa App. If the configuration is complete, you can see the Lock device you just added on the Home page.

4.1 Controlling Lock via Amazon APP

4.2 Voice Control of Lock via Alexa Echo