0. 前言
关于ESP-Matter,之前已经发布以下两篇博文,建议先了解相关基础概念框架后再着手开发:
【ESP32-Matter】ESP-Matter 介绍
【ESP32-Matter】ESP-Matter SDK 软件开发框架及目录结构介绍
1. 开发环境搭建
1.1 测试工具
- 编译主机采用 Ubuntu 22.04 LTS
- 测试开发板型号为 ESP32C3
- VSCode 用于软件开发
注意:目前乐鑫推荐ubuntu 版本为20.04 和22.04,并且为主机开发,若用虚拟机后面用chip-tool 工具配网调试时会遇到找不到蓝牙设备或者无法调试的情况。
1.2 安装ESP-IDF 和 Matter 环境
1.2.1 安装 ESP-IDF (Espressif IoT Development Framework)
构建示例程序 ESP32C3 模块的工具链为 riscv-esp32-elf
,VSCode 已经预装相应组件,可以命令栏选择。
-
ESP-IDF 依赖环境安装:Linux
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
-
获取IDF 仓库(v4.4.2 或 v4.4.3)
-
Clone ESP-IDF v4.4.2 release (本次测试选择)
git clone --recursive https://github.com/espressif/esp-idf.git cd esp-idf git checkout v4.4.2 git submodule update --init --recursive ./install.sh cd ..
-
Clone ESP-IDF v4.4.3 release
$ git clone -b v4.4.3 --recursive https://github.com/espressif/esp-idf.git $ cd esp-idf $ ./install.sh //To update an existing esp-idf toolchain to v4.4.3: $ cd path/to/esp-idf $ git fetch origin $ git checkout v4.4.3 $ git reset --hard origin/v4.4.3 $ git submodule update --recursive --init $ git clean -fdx $ ./install.sh
-
1.2.2 安装 Matter 环境
-
Matter 依赖环境安装:Linux
sudo apt-get install git gcc g++ 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
-
获取 Matter仓库
(推荐) 由于上游 connectedhomeip 中有很多子模块,克隆 esp-matter 存储库需要一段时间,因此如果你想进行浅克隆,请使用以下命令:
git clone --depth 1 https://github.com/espressif/esp-matter.git cd esp-matter git submodule update --init --depth 1 ./connectedhomeip/connectedhomeip/scripts/checkout_submodules.py --platform esp32 --shallow ./install.sh //安装matter相关依赖 cd ..
要克隆包含所有子模块的 esp-matter 存储库,请使用以下命令:
git clone --recursive https://github.com/espressif/esp-matter.git cd esp-matter ./install.sh cd ..
注意:Matter 安装执行install.sh
脚本时若遇到 相关Python 版本的报错,此时需注意关闭掉主机原有Python 版本,若启动 Anaconda3 工具,需关闭掉以 Matter 依赖安装的Python 版本为主。
1.2.3 配置环境变量
每次打开新终端时都应该这样做
cd esp-idf; . ./export.sh; cd ..
cd esp-matter; . ./export.sh; cd ..
启用 Ccache 以加快 IDF 构建速度。
Ccache 是一个编译器缓存。物质构建非常缓慢,需要花费大量时间。Ccache 缓存以前的编译并在后续构建中加速重新编译。
export IDF_CCACHE_ENABLE=1
此处编写一个脚本处理(根据各自主机配置路径)
#/bin/bash
# matter.sh
EPS_MATTER_PATH="/home/book/work/esp-matter"
if [ $1 -eq 1 ]; then
export IDF_PATH="/home/book/work/esp-idf"
source /home/book/work/esp-idf/export.sh
source $EPS_MATTER_PATH/export.sh
export IDF_CCACHE_ENABLE=1
echo "enter matter dir"
cd $EPS_MATTER_PATH
fi
每次打开客户端执行 source matter.sh 1
命令;
1.2.4 VSCode CMake软件环境配置
由于ESP-Matter 程序代码是基于CMake构建的,因此建议安装CMake
和 CMake Tools
插件, 并按 Ctrl+Shift+P
设置CMake:Configure
配置CMake, 点击该选项系统插件CMake Tools会自动创建项目数据库
2. 测试
2.1 烧录示例固件
测试程序为色温灯,进入 eps-matter/examples/light
目录,选择 IDF 目标。
idf.py set-target esp32c3
(第一次烧写 SDK 时,建议 擦除整个 flash 并重新开始。)idf.py erase_flash
idf.py flash monitor
2.2 调试控制
2.2.1 配网
基于chip-tool 工具配网调试可以用作调试器来调试和控制 Matter 设备, CHIP 工具是 Matter commissioner 的示例实现,用于开发目的。
chip-tool pairing ble-wifi 1234 <ssid> <passphrase> 20202021 3840
在上述命令中:
1234
是随机选择的node_id
20202021
是个setup_passcode
3840
是个discriminator
在烧录完 light
固件后,重新打开一个终端,进入 esp-matter
目录 执行
chip-tool pairing ble-wifi 1234 HiWiFi_2CC364 nekteck2014 20202021 3840
配网过程如下:
2.2.2 控制
light
程序基于chip-tool 的控制命令如下
·Usage:
chip-tool colorcontrol command_name [param1 param2 ...]
+-------------------------------------------------------------------------------------+
| Commands: |
+-------------------------------------------------------------------------------------+
| * command-by-id |
| * move-to-hue |
| * move-hue |
| * step-hue |
| * move-to-saturation |
| * move-saturation |
| * step-saturation |
| * move-to-hue-and-saturation |
| * move-to-color |
| * move-color |
| * step-color |
| * move-to-color-temperature |
| * enhanced-move-to-hue |
| * enhanced-move-hue |
| * enhanced-step-hue |
| * enhanced-move-to-hue-and-saturation |
| * color-loop-set |
| * stop-move-step |
| * move-color-temperature |
| * step-color-temperature |
| * read-by-id |
| * read |
| * write-by-id |
| * write |
| * subscribe-by-id |
| * subscribe |
| * read-event-by-id |
| * subscribe-event-by-id |
+-------------------------------------------------------------------------------------+
测试控制使用指令如下:
chip-tool onoff on 1234 1 //开灯
chip-tool onoff off 1234 1 //关灯
chip-tool levelcontrol move-to-level 150 0 0 0 1234 1 // 调节电平
chip-tool colorcontrol move-to-saturation 200 0 0 0 1234 1 // 设置饱和度
colorcontrol move-to-hue 150 0 0 0 0 1234 1 // 设置色彩
chip-tool 在交互模式下使用时使用 CASE 恢复,而不是为集群控制命令建立 CASE。可以缩短执行时间,从而改善整体体验。
有关芯片工具使用的更多详细信息,请查看https://github.com/project-chip/connectedhomeip/tree/master/examples/chip-tool
chip-tool 工具和设备端关灯的指令交互如下:
ESP-Matter 控制色温灯操作