2026年6月

Modbus实际上是应用层协议,制定的年代非常早。理论上是不限定物理层的。
最早是从串口协议RS485上发展来的。因为它是可以一主多从,跟RS485的架构刚好相同。现在也支持基于TCP/IP协议,默认端口号是502。但由于502端口在系统保留端口内,所以很多软件是以8502启动的。
Modbus支持的底层协议分为三种。分别是Modbus RTU、Modbus ASCII,Modbus TCP。由于它是上层协议,所以有JavaScript的实现。有两个库,Modbus-Serial和JSModbus。
Modbus分为主机和从机,一个主机最多可以带32个从机。按Client Server架构来分的话,主机Master是Client,从机Slave是Server。
从机分为三级来去读取一个具体的数值。依次是Unit ID、寄存器、地址。
每个从机,也就是Server首先有一个自己的Unit ID。
其次支持四种不同的寄存器。
1,线圈coil,可读可写,布尔类型。
2,离散输入Discrete input,只读布尔类型。
3,输入寄存器Input registers,只读,双字节。
4,保持寄存器Holding registers,可读可写,双字节。
最后,每种寄存器有其自己的地址。地址又分人类可读地址和机器实际地址。人类可读地址以0、1、3、4开头,分别代表线圈和离散输入、输入寄存器、保持寄存器。机器实际地址都是从0开始的。
Modbus的问题就是从机不能主动上报数据,只能主机去轮询。

参考资料:
cnblogs上一篇讲解:https://www.cnblogs.com/iini/p/18009504
st matter测试和演示: https://wiki.st.com/stm32mcu/wiki/Connectivity:Matter_test_and_demonstrate
esp-matter 文档: https://github.com/espressif/esp-matter
csa matter协议: https://csa-iot.org/all-solutions/matter/
官方matter sdk开源: https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/BUILDING.md#prerequisites
开发工具:
esp matter开发需要在linux或者MacOS上安装esp-idf和esp-matter
官方 chip-tool 运行于支持BLE的linux和MacOS上
android上由NXP开发的NXP Matter Tool可以替代chip-tool

配网

Matter的配网又叫commission任命(和provision又不同),过程叫commissioning, 给配网信息的叫任命者commissioner,一般就是controller手机,被任命的叫commissionee,一般就是设备。
matter的设备(不包括设备本身)内有三级结构,分别时node节点->endpoint终端->cluster功能簇,cluster分为server和client,其中server提供attributes, Commands和Events, client可以发起交互命令interactions, 包括 read, write, invoke, subscribe(读/写/调用/订阅)

为了完成配网,commissionee必须提供如下onboarding信息:
16-bit Vendor ID and 16-bit Product ID
12-bit device discriminator
27-bit setup passcode
8-bit Discovery Capabilities Bitmask

通过以上信息可以生成一下两种方式来配网(可以用NXP matter tool来生成):
手动配对码(Manual Pairing Code)
二维码(QR Code)

如果上述配网流程成功,那么设备将得到如下信息:

由fabric ID(64bit)和node ID(64bit)组成的实例名
Node Operational Certificate(NOC)
NOC对应的私钥
Access Control List
操作网络的其他信息
不同的node组成一个group,并用16bit的group id来标识

尝试

我用Esp-matter中example的light项目编译到C3上测试。串口是可以直接回车命令行交互的。输入matter help可以查看。
重要命令:

matter ble adv start
matter config
matter onboardingcodes ble
matter device factoryreset

设备不会一直在ble广播commissioning状态,过段时间会退出。

我是用NXP matter tool完成配网后,生成了一个QR码,然后再google home中扫描,然而google home提示必须有支持matter的hub才能控制,包括音箱(google home/mini, next mini, nest audio), 显示屏(nest hub 1代/2代/max), WLAN(nest wifi pro), 流媒体播放器(google tv streamer)。

使用matter device factoryreset后,我用ESP rainmaker home也配网成功,并且“雨作”配网可以自动发送ssid和密码过去(不知道是怎么获取的)。但是NXP就控制不了了。

根据AI的回复,matter优先使用ble配网,局域网控制,部分高级功能才会用到互联网。
并且从google home情况来看,matter可以适配各自的IOT生态,比如google home就要求购买支持matter的hub,而乐鑫和NXP可以直接加入控制。

DPP

WIFI Easy connect(也叫Device Provisioning Protocol)简称DPP,在idf的examples中有个dpp-enrollee目录就是示例。
乐鑫基本全系支持的。我在C3上试运行是可以的。会从串口打印出一个二维码。接下来就遇到问题了,据AI和乐鑫说,Android 10 以上都默认支持,可是我的手机是Android15(IQOO), 却找不到所说的扫码入口
官方文档
https://github.com/espressif/esp-idf/tree/fb14a3e7/examples/wifi/wifi_easy_connect/dpp-enrollee

To run the example with an Android 10+ device follow below steps -
Compile and flash the example on ESP device, a QR code will appear on your console.
Connect your phone to the network, say named "Example-AP".
Now go to Settings->WiFi & Internet->Wi-Fi->Example-AP->Advanced->Add Device.
Scan QR Code using the scanner, which will make ESP device connect to Example-AP.

然后就此事问了谷歌ai。谷歌说,确实DPP的推广不利,在iOS上根本不被支持。而且苹果不支持WiFi联盟的配网协议,包括上一代配网协议WPS是老传统了。
所以这个网最后也没配起来。
从手机端向设备端传出WiFi信号,走的是专用的一个配网的信道,设备端监听,由手机端发送过去。似乎在二维码中会填写频段和信道,以供手机端发送。

乐鑫的统一配网Provisioning

同样,我在C3上运行起来了,也是通过串口打印了二维码。
通过乐鑫官方的APP ESP BLE Prov也成功配网了。另一个APP ESP Soft AP Prov应该是给没有蓝牙的设备用的。
真的,这个操作略显蛋疼,就是本来通过BluFi已经可以扫描到周围的设备了。统一配网却还需要额外的信道,也就是打印出来二维码,通过摄像头去扫描来传输信息。这整个过程对于生产来说也是很麻烦的。
唯一的好处就是之前我做培训的时候遇到的配网的问题,多个学员同时去做配网的话。配网的话,会扫出来太多的设备,不知道哪个是自己的。不过这种的话也同样可以解决,如果统一配网和DPP可以打印二维码,那我也可以打印,甚至我只需要打印出来MAC地址的尾号就可以。
乐鑫统一配网的示例还不在idf默认的examples里面,而是在 idf-extra-components中, 位置:
https://github.com/espressif/idf-extra-components/tree/master/network_provisioning

所以还是不如BluFi好用。

matter

据某个AI说,matter也支持配网,所以想安装matter环境编译试一下。
然而esp-matter仅支持linux和macos, 我在WSL(Ubuntu 24.04)上安装测试,还要先装idf再装matter,安装matter到编译这一步的时候,就会导致wsl崩溃退出。

Done. Made 13083 targets from 819 files in 14765ms
ninja: Entering directory /home/a/esp/esp-matter/connectedhomeip/connectedhomeip/out/host' [0/49] c++ obj/examples/common/tracing/commandline.TracingCommandLineArgument.cpp.oTerminated a@J:~/esp/esp-matter$ ninja: build stopped: interrupted by user. 发现是ninja使用了所有CPU内核和内存导致系统卡死被系统杀进程了。每个ninja编译线程占用1个CPU和约1.5~2G内存。如果内存不足,需要限制CPU的核心数。否则几个ninja线程之间抢内存和swap虚拟内存,最后一个都没编译。 在用户目录下修改.wslconfig`, 增加:

[wsl2]
processors=2
memory=6GB   

就可以编译过了。
通过后又遇到ubuntu24不允许pip安装系统包的问题:


Installing python dependencies for Matter

Installing requirements from requirements.txt

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.

See /usr/share/doc/python3.12/README.venv for more information.
google ai建议:
設定全域允許 pip 安裝(推薦)在終端機執行以下指令,為你的 Linux 用戶建立 pip 設定檔,允許覆蓋系統套件限制:
```bash
mkdir -p ~/.config/pip
echo -e "[global]\nbreak-system-packages = true" > ~/.config/pip/pip.conf

git repo: https://github.com/espressif/esp-matter
doc: https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#host-setup

文档中确实示例了如何配网,并且演示了三种配网方式:

Use chip-tool in interactive mode to commission the device:

chip-tool interactive start
pairing ble-wifi 0x7283 <ssid> <passphrase> 20202021 3840

In the above commands:
0x7283 is the randomly chosen node_id
20202021 is the setup_passcode
3840 is the discriminator

Above method commissions the device using setup passcode and discriminator. Device can also be commissioned using manual pairing code or QR code.
To Commission the device using manual pairing code 34970112332

pairing code-wifi 0x7283 <ssid> <passphrase> 34970112332

Above default manual pairing code contains following values:
Version: 0
Custom flow: 0 (STANDARD)
Discriminator: 3840
Passcode: 20202021
To commission the device using QR code MT:Y.K9042C00KA0648G00

pairing code-wifi 0x7283 <ssid> <passphrase> MT:Y.K9042C00KA0648G00