Back to blog

How can embedding the Matter SDK enhance IoT device functionality in real-world applications?

In the realm of embedded systems development, the ESP-IDF (Espressif IoT Development Framework) stands out as a versatile and powerful toolset, empowering developers to create innovative solutions for the Internet of Things (IoT) landscape. One of the most exciting recent developments within the ESP-IDF ecosystem is the integration of Matter, formerly known as Project Connected Home over IP (CHIP).

Matter is an open-source connectivity standard developed by the Connectivity Standards Alliance (formerly the Zigbee Alliance), in collaboration with leading technology companies. It aims to simplify and standardize smart home device communication, ensuring interoperability across various ecosystems. By providing a common language for IoT devices, Matter streamlines development efforts, enhances compatibility, and improves the overall user experience.

The integration of Matter into ESP-IDF marks a significant milestone in the evolution of IoT development. With ESP-IDF being one of the most popular frameworks for developing applications on Espressif’s ESP32 and ESP8266 microcontrollers, the addition of Matter support extends its capabilities even further. Developers now have access to a robust set of tools and libraries for building Matter-compliant IoT devices directly within the ESP-IDF environment.

In this article, we’ll show you an example of Matter implementation in our latest internal project. The first milestone of this project focuses on implementing Matter SDK into a project, opening the commissioning window, adding the device to the mobile app, and controlling an LED from the app.

ESP-Matter is available as a git submodule under https://github.com/espressif/esp-matter.git and can be simply added to a project repository just like other modules, and like them, we have to add it in our CMake file as COMPONENT_REQUIRES. To be able to use this SDK, we have to build it first locally, using the scripts provided inside the module. Similarly to esp-idf, we have to run the install script once, and export script for each terminal session to have access to the required environment variables.

Additionally, we need to add the just installed Matter directories to our main CMake file:

 set(ESP_MATTER_PATH $ENV{ESP_MATTER_PATH})
set(MATTER_SDK_PATH ${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip)
set(ZAP_GENERATED_PATH ${CMAKE_SOURCE_DIR}/main/src/matter/)

if(NOT DEFINED ENV{ESP_MATTER_DEVICE_PATH})
   if("${IDF_TARGET}" STREQUAL "esp32" OR "${IDF_TARGET}" STREQUAL "")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32_devkit_c)
   elseif("${IDF_TARGET}" STREQUAL "esp32c3")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32c3_devkit_m)
   elseif("${IDF_TARGET}" STREQUAL "esp32c2")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32c2_devkit_m)
   elseif("${IDF_TARGET}" STREQUAL "esp32h2")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32h2_devkit_c)
   elseif("${IDF_TARGET}" STREQUAL "esp32s3")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32s3_devkit_c)
   elseif("${IDF_TARGET}" STREQUAL "esp32c6")
       set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32c6_devkit_c)
       add_definitions(-DMATTER_CLOUD_COEXISTANCE)
   else()
       message(FATAL_ERROR "Unsupported IDF_TARGET")
   endif()
endif(NOT DEFINED ENV{ESP_MATTER_DEVICE_PATH})

include($ENV{ESP_MATTER_DEVICE_PATH}/esp_matter_device.cmake)

set(EXTRA_COMPONENT_DIRS ${EXTRA_COMPONENT_DIRS}
   "${MATTER_SDK_PATH}/config/esp32/components"
   "${ESP_MATTER_PATH}/examples/common"
   "${ESP_MATTER_PATH}/components"
   "${ESP_MATTER_PATH}/device_hal/device"
   ${extra_components_dirs_append})

Now we are ready to use matter in our code and the provided in the module examples can be very useful. In our example, we start with the configuration and initialization of Matter.
For that, according to the Matter Data Model,ย  we create a Matter node:

node_t*        node = node::create(&node_config, appAttributeUpdateCb, appIdentificationCb);

Matter endpoint:

 
endpoint_t* endpoint        = on_off_light::create(node, &switch_config, ENDPOINT_FLAG_NONE, nullptr);

and Matter group:

cluster::groups::create(endpoint, &groups_config, CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);

and start the Matter process with:

err = esp_matter::start(appEventCb);

As you can notice, in the above code lines we provided function pointers to callback functions. Those functions are called for every interaction with Matter or whenever an attribute is updated. For example in the function โ€˜appAttributeUpdateCbโ€™ we can add our implementation of LED control whenever the on_off_light endpoint attribute changes.
Now, we have to implement a way to connect the device with a mobile app. We need to open a commissioning window. For that we will need the CommisioningWindowManager:

chip::CommissioningWindowManager& commissionMgr   = chip::Server::GetInstance().GetCommissioningWindowManager();
CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(kTimeoutSeconds, commissioningType);

Our way to connect the device is to use BLE, this can be set using the Menuconfig window.
We can test how it works after we build the application and flashed it on a devboard.ย 

For our Matter Hub, we use the Aeotec Smart Home Hub, with its SmartThings mobile application.
For device provisioning, we will need a QR code, which is normally assigned to a device in the certification process. For development purposes, we can use a universal QR code available in the esp-matter documentation.

In the mobile app, we can scan for nearby devices or directly scan the QR code. When our commissioning window is open, the device scanning should find our board. The app will remind us that the device is not certified. After that, we need to provide WiFi credentials, and a device name and we are ready to go.

We have successfully paired a matter device, which allows us to control a simple on-off light, just like we configured it.

The integration of Matter into ESP-IDF provides developers with a robust framework for building interoperable and secure IoT solutions.ย  Embrace the power of Matter integration with ESP-IDF to create compelling and standardized IoT experiences that drive the advancement of connected ecosystems.

Stay tuned for the next project milestones.


		

Recent entries

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.