Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
  • Groups
    • Research Collaboration and Enablement
    • DesignStart
    • Education Hub
    • Innovation
    • Open Source Software and Platforms
  • Forums
    • AI and ML forum
    • Architectures and Processors forum
    • Arm Development Platforms forum
    • Arm Development Studio forum
    • Arm Virtual Hardware forum
    • Automotive forum
    • Compilers and Libraries forum
    • Graphics, Gaming, and VR forum
    • High Performance Computing (HPC) forum
    • Infrastructure Solutions forum
    • Internet of Things (IoT) forum
    • Keil forum
    • Morello Forum
    • Operating Systems forum
    • SoC Design and Simulation forum
    • 中文社区论区
  • Blogs
    • AI and ML blog
    • Announcements
    • Architectures and Processors blog
    • Automotive blog
    • Graphics, Gaming, and VR blog
    • High Performance Computing (HPC) blog
    • Infrastructure Solutions blog
    • Innovation blog
    • Internet of Things (IoT) blog
    • Operating Systems blog
    • Research Articles
    • SoC Design and Simulation blog
    • Tools, Software and IDEs blog
    • 中文社区博客
  • Support
    • Arm Support Services
    • Documentation
    • Downloads
    • Training
    • Arm Approved program
    • Arm Design Reviews
  • Community Help
  • More
  • Cancel
Arm Community blogs
Arm Community blogs
Internet of Things (IoT) blog Build a Matter home automation service using Raspberry Pi, Arm Virtual Hardware and Python
  • Blogs
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
More blogs in Arm Community blogs
  • AI and ML blog

  • Announcements

  • Architectures and Processors blog

  • Automotive blog

  • Embedded blog

  • Graphics, Gaming, and VR blog

  • High Performance Computing (HPC) blog

  • Infrastructure Solutions blog

  • Internet of Things (IoT) blog

  • Operating Systems blog

  • SoC Design and Simulation blog

  • Tools, Software and IDEs blog

Tags
  • Raspberry Pi
  • Smart Homes
  • Cortex-A
  • Cortex-M
  • python
  • Internet of Things (IoT)
  • Arm Virtual Hardware
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

Build a Matter home automation service using Raspberry Pi, Arm Virtual Hardware and Python

Sandeep Mistry
Sandeep Mistry
November 17, 2022
6 minute read time.

In October of 2022, the Connectivity Standards Alliance announced the release of the Matter 1.0 specification and SDK. With this release, anyone building on Arm will be able to produce and sell home automation services. The combination of the Matter protocol and Arm standards such as PSA certified, Project Cassini, and Project Centauri removes the fragmentation of devices from different suppliers. The race is on as Matter compliant devices have already been introduced into the market. The expectation is that a few billion Matter compliant devices will be sold over the next few years.

The next question is how to open this opportunity to all developers besides the established embedded experts. Python is one way to do so. The number of Python developers is increasing 20% per year and was estimated to be approximately 10 million in 2021. All we need now is a platform to run Matter and Python. What better choice than the ubiquitous Raspberry Pi Model B.

Spoiler alert, the output of this exercise is a Matter lighting home automation service created using Python. The service runs on a Raspberry Pi or a virtual Raspberry Pi in the cloud through Arm Virtual Hardware (AVH) as the same code runs on both as is. The service controls a LED that is connected to a Raspberry Pi and control is over the IP network.

To understand more about Matter and the Arm Virtual Hardware and its benefits, you can read my previous blog in this series here.

Building a smart home device

What you need

Lighting device

  • Raspberry Pi 4 board
  • MicroSD card, 16GB or greater in size
  • USB C Power supply
  • Ethernet cable
  • Breadboard
  • LED
  • 1k ohm resistor
  • Jumper wires

Device controller

For the physical board

  • Raspberry Pi 4 board
  • MicroSD card, 16GB or greater in size
  • USB C Power supply
  • Ethernet cable

For the Arm Virtual Hardware Raspberry Pi 4 board

  • Register to access the Arm Virtual Hardware private beta
  • Already registered? Login here

The first implementation will run the light service between two Raspberry Pi boards.

building a smart home device with physical Raspberry Pi

Setting up the Lighting device

1. Use a breadboard and the jumper wires to connect the resistor and LED to the Raspberry Pi 4 board on GPIO17. Further information: https://www.raspberrypi.com/documentation/computers/os.html#gpio-pinout)

Raspberry Pi Breadboard with LED

2. Insert the microSD card into the microSD card reader and connect it to your PC.

3. Using Raspberry Pi Imager tool to flash a microSD card with

a. Click the “Choose OS” button, then click on “Raspberry Pi OS (other)” -> “Raspberry Pi OS Lite (64-bit)”

Raspberry Pi OS Lite

b. Click the “Gear” icon in the bottom corner

Raspberry Pi OS

c. Enter a name and set the username and password for SSH

Raspberry Pi Imager

d. Click the “Choose Storage” button and select the entry for the microSD card

e. Click the “Write” button

4. Insert the microSD card into the Raspberry Pi, connect the Ethernet cable to your network, and then connect power supply to the board to turn it on

5. SSH in to the Raspberry Pi using <username>@matter-light.local

6. Install build dependencies

sudo apt-get update

sudo apt-get install git gcc g++ python3 pkg-config libssl-dev libdbus-1-dev libglib2.0-dev libavahi-client-dev ninja-build python3-venv python3-dev python3-pip unzip libgirepository1.0-dev libcairo2-dev libreadline-dev

7. Clone Matter repo and submodules

git clone https://github.com/project-chip/connectedhomeip.git

cd connectedhomeip

git checkout db49235c39635582ea522929f9905af03e3114c7

./scripts/checkout_submodules.py --shallow --platform linux

8. Bootstrap the build

./scripts/build/gn_bootstrap.sh

source scripts/activate.sh

9. Build the Python and install the wheel

./scripts/build_python_device.sh --chip_detail_logging true

10. Activate Python virtual environment

source ./out/python_env/bin/activate

11. Enable GPIO permissions

sudo usermod -a -G gpio <username>

12. Install the gpiozero package (recommended from here).

pip3 install gpiozero

13. Create a new file named lighting.py with the following:

from chip.server import (
    GetLibraryHandle,
    NativeLibraryHandleMethodArguments,
    PostAttributeChangeCallback,
)

from chip.exceptions import ChipStackError

import sys
import os

from gpiozero import LED

led = LED(17)

@PostAttributeChangeCallback
def attributeChangeCallback(
    endpoint: int,
    clusterId: int,
    attributeId: int,
    xx_type: int,
    size: int,
    value: bytes,
):
    if endpoint == 1:
        if clusterId == 6 and attributeId == 0:
            if len(value) >= 1 and value[0] == 1:
                print("[PY] light on")
                led.on()
            else:
                print("[PY] light off")
                led.off()

chipLib = GetLibraryHandle(attributeChangeCallback)

input('Press enter to quit')

sys.exit(0)

14. Start the application

python3 lighting.py

To reset the device config:

rm /tmp/chip_*

Setting up the device controller (physical) device

1. Repeat steps 2-8 above, but using the name “matter-controller.local” in step 3c and 5.

2. Build and install the Device Controller wheel:

./scripts/build_python.sh -d true -i separate

3. Activate the Python virtual environment

source ./out/python_env/bin/activate

Using Arm Virtual Hardware as the device controller device

In this section, the Raspberry Pi is replaced with an AVH Raspberry Pi. The same Python service code will run as above. The difference is that one can create 100 smart hubs as such and run 100 tests in parallel if necessary as opposed to creating a board farm.

Building a smart home device with Arm Virtual Hardware

1. Create new virtual Raspberry Pi device on AVH with the Raspberry Pi OS Lite OS option.

2. Using the console interface run steps 6-8 from “Setting up the Lighting device” section

3. Run steps 2-3 from “Setting up the Device Controller (physical) device” section

4. Download the AVH VPN profile to Lighting device

5. Install openvpn on lighting device

sudo apt-get install openvpn

6. Start a new SSH session and login using: <username>@matter-light.local

7. Start VPN connection

sudo openvpn --config 'app.avh.arm.com VPN - Default Project.ovpn'

8. Restart the Python lighting app in original SSH session.

Control

1. On Device Controller device run (make sure you are in git project root!)

chip-repl

2. Discover commissionable devices

devices = devCtrl.DiscoverCommissionableNodes(filter=3840, stopOnFirst=True, timeoutSecond=20)

3. Commission the device setting node id to 233 and using code 20202021 

devices[0].Commission(233, 20202021)

4. Turn the light on:

await devCtrl.SendCommand(233, 1, Clusters.OnOff.Commands.On())

5. Turn off the light: 

await devCtrl.SendCommand(233, 1, Clusters.OnOff.Commands.Off())

6. Toggle the light:

await devCtrl.SendCommand(233, 1, Clusters.OnOff.Commands.Toggle())

Conclusion

The age of true home automation starts now. The Matter protocol removes the last barrier for deployment. Arm and our ecosystem deliver the IP, tools, software services, and security frameworks to best address the wildly diverse nature of smart home devices. As such, Arm is the platform on which Matter-compliant devices will emerge first. There are many Arm-based platforms on the market today for developers to get started with, such as the Raspberry Pi and the NXP i.Mx8M based boards. The platform software is usually bundled with the hardware. The Matter SDK is open source from the Connectivity Standards Alliance (CSA). Any developer simply must create the automation service on top of the Matter platform. There is no limit to what is possible. If one can imagine it, then it can be created and deployed.

smart home hub software stack

It only takes a few minutes to prepare a Matter platform and to start creating a home automation service using Python. Arm has even made the starting point so much easier by offering the Raspberry Pi on Arm Virtual Hardware. Developers can get started even when they do not yet have the hardware on their desk.

In an ideal scenario, anyone can create and test the service on a virtual Raspberry Pi and then deploy the code worldwide to a real Raspberry Pi because AVH and the real Raspberry Pi are binary compatible. The IoT runs on Arm, and we have a responsibility to create greater opportunities for innovation and scale by continually raising the bar on performance, simplified development, and software reuse. As such, we introduced Arm Virtual Hardware, a transformative offering, to enable software development in the cloud to empower developers to capture emerging market opportunities.

Explore what Arm Virtual Hardware can do for your team then:

  • Explore resources and information
  • Explore the API that is available to access AVH functionality through scripts

Get started using this example by registering for the Arm Virtual Hardware private beta:

Register for Arm Virtual Hardware

Anonymous
  • turbosree
    Offline turbosree 11 days ago in reply to turbosree

    The 'gn' binary can be build using the google page: https://gn.googlesource.com/gn/. This fixed the issue.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • turbosree
    Offline turbosree 18 days ago

    Executing ./scripts/build/gn_bootstrap.sh give me the following error:

    Setting up Python environment.....Traceback (most recent call last):
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/env_setup.py", line 795, in <module>
    sys.exit(main())
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/env_setup.py", line 787, in main
    return EnvSetup(**vars(parse())).setup()
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/env_setup.py", line 457, in setup
    result = step(spin)
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/env_setup.py", line 606, in virtualenv
    if not virtualenv_setup.install(
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py", line 342, in install
    install_packages(gn_target)
    File "/home/pi/connectedhomeip/third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py", line 310, in install_packages
    subprocess.check_call(gn_cmd,
    File "/usr/lib/python3.9/subprocess.py", line 368, in check_call
    retcode = call(*popenargs, **kwargs)
    File "/usr/lib/python3.9/subprocess.py", line 349, in call
    with Popen(*popenargs, **kwargs) as p:
    File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File "/usr/lib/python3.9/subprocess.py", line 1823, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: 'gn'

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Sandeep Mistry
    Offline Sandeep Mistry 21 days ago in reply to HybridRobotics

    Hi @HybridRobotics, would you be able to share the output from the build?

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Sandeep Mistry
    Offline Sandeep Mistry 21 days ago in reply to SpencerWF

    Hi @SpencerWF, There have been some upstream changes in the Matter repo, I've updated the guide to pin to a specific commit: git checkout db49235c39635582ea522929f9905af03e3114c7

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • HybridRobotics
    Offline HybridRobotics 24 days ago

    I am trying to get this working with a Raspberry Pi 400 and a Raspberry Pi 4/4GB. I am getting a file not found error at step 10 of the matter-light part of this guide. I searched for a .out directory in the tree and there is no such directory.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
>
Internet of Things (IoT) blog
  • A bare-metal programming guide

    Sergey Lyubka
    Sergey Lyubka
    Get started with the Arm bare metal programming with only GCC compiler, text editor, and a datasheet. From blinky to an embedded Web device dashboard.
    • March 15, 2023
  • Arm takes Embedded Software Development to the next level with introduction of Keil MDK Version 6

    Reinhard Keil
    Reinhard Keil
    Keil MDK Version 6 delivers new features that are optimized for the entire Cortex-M and Ethos-U processor portfolio.
    • March 9, 2023
  • Cortex-M85: Enabling safety and boosting flexibility and performance even higher

    Dimos Rossidis
    Dimos Rossidis
    This blog outlines new updates to the Arm Cortex-M85 CPU to improve safety, flexibility and performance for IoT and automotive markets.
    • March 8, 2023