Update mqtt-gateway-integration.md

This commit is contained in:
Alireza Jahanshahlou 2025-10-12 17:35:23 +03:30 committed by GitHub
parent a20b8fa3a1
commit 032948f8cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,55 +1,55 @@
# MQTT 网关部署教程 # MQTT Gateway Deployment Tutorial
`xiaozhi-esp32-server`项目,可结合虾哥开源的[xiaozhi-mqtt-gateway](https://github.com/78/xiaozhi-mqtt-gateway) 项目进行简单改造,即可实现小智硬件MQTT+UDP连接。 The `xiaozhi-esp32-server` project can be combined with the open source [xiaozhi-mqtt-gateway](https://github.com/78/xiaozhi-mqtt-gateway) project by Xia Ge for simple modification to achieve MQTT+UDP connection for Xiaozhi hardware.
本教程分为三部分,你可以根据你是全模块部署还是单模块部署,选择对应的部分接入MQTT网关: This tutorial is divided into three parts. You can choose the corresponding part to connect to the MQTT gateway based on whether you are deploying a full module or a single module:
- 第一部分:部署MQTT网关 - Part 1: Deploy the MQTT Gateway
- 第二部分:全模块运行实现小智硬件MQTT+UDP连接 - Part 2: Full module operation to realize Xiaozhi hardware MQTT+UDP connection
- 第三部分:单模块运行xiaozhi-server实现小智硬件MQTT+UDP连接 - Part 3: Single module running xiaozhi-server to realize MQTT+UDP connection of Xiaozhi hardware
## 准备阶段 ## Preparation
准备好你的`xiaozhi-server`的`mqtt-websocket`连接地址。在你原来的`websocket地址`基础上,添加`?from=mqtt_gateway`字符,就可以得到`mqtt-websocket`连接地址 Prepare the `mqtt-websocket` connection address of your `xiaozhi-server`. Based on your original `websocket address`, add the `?from=mqtt_gateway` character to get the `mqtt-websocket` connection address.
1、如果你是源码部署,你的`mqtt-websocket`地址是: 1. If you are deploying from source code, your `mqtt-websocket` address is:
``` ```
ws://127.0.0.1:8000/xiaozhi/v1?from=mqtt_gateway ws://127.0.0.1:8000/xiaozhi/v1?from=mqtt_gateway
``` ```
2、如果你是docker部署,你的`mqtt-websocket`地址是 2. If you are deploying with docker, your `mqtt-websocket` address is
``` ```
ws://你宿主机局域网IP:8000/xiaozhi/v1?from=mqtt_gateway ws://your host LAN IP:8000/xiaozhi/v1?from=mqtt_gateway
``` ```
## 重要提示 ## Important Note
如果你是服务器部署,需要确保服务器`1883`、`8884`、`8007`端口都对外开放。`8884`选择的协议类型是`UDP`,其他是`TCP`。 If you are deploying on a server, you need to ensure that ports 1883, 8884, and 8007 are open. For port 8884, select UDP; for the others, select TCP.
如果你是服务器部署,需要确保服务器`1883`、`8884`、`8007`端口都对外开放。`8884`选择的协议类型是`UDP`,其他是`TCP`。 If you are deploying on a server, you need to ensure that ports 1883, 8884, and 8007 are open. For port 8884, select UDP; for the others, select TCP.
如果你是服务器部署,需要确保服务器`1883`、`8884`、`8007`端口都对外开放。`8884`选择的协议类型是`UDP`,其他是`TCP`。 If you are deploying on a server, you need to ensure that ports 1883, 8884, and 8007 are open. For port 8884, select UDP; for the others, select TCP.
## 第一部分:部署MQTT网关 ## Part 1: Deploy the MQTT Gateway
1. 克隆[改造后的xiaozhi-mqtt-gateway项目](https://github.com/xinnan-tech/xiaozhi-mqtt-gateway.git): 1. Clone the modified xiaozhi-mqtt-gateway project:
```bash ```bash
git clone https://ghfast.top/https://github.com/xinnan-tech/xiaozhi-mqtt-gateway.git git clone https://ghfast.top/https://github.com/xinnan-tech/xiaozhi-mqtt-gateway.git
cd xiaozhi-mqtt-gateway cd xiaozhi-mqtt-gateway
``` ```
2. 安装依赖: 2. Install dependencies:
```bash ```bash
npm install npm install
npm install -g pm2 npm install -g pm2
``` ```
3. 配置 `config.json`: 3. Configure `config.json`:
```bash ```bash
cp config/mqtt.json.example config/mqtt.json cp config/mqtt.json.example config/mqtt.json
``` ```
4. 编辑配置文件 config/mqtt.json,把你在`本文准备阶段`的`mqtt-websocket`地址替换到`chat_servers`里。例如源码部署的`xiaozhi-server`就是如下配置: 4. Edit the configuration file config/mqtt.json and replace the `mqtt-websocket` address you used in the `preparation phase of this article` with the `chat_servers`. For example, the `xiaozhi-server` deployed from source code has the following configuration:
``` ```
{ {
"production": { "production": {
"chat_servers": [ "chat_servers": [
@ -69,111 +69,111 @@ cp config/mqtt.json.example config/mqtt.json
} }
} }
``` ```
5. 在项目根目录创建下`.env`文件,并设置以下环境变量: 5. Create a `.env` file in the project root directory and set the following environment variables:
``` ```
PUBLIC_IP=your-ip # 服务器公网IP PUBLIC_IP=your-ip # Server public IP
MQTT_PORT=1883 # MQTT服务器端口 MQTT_PORT=1883 # MQTT server port
UDP_PORT=8884 # UDP服务器端口 UDP_PORT=8884 # UDP server port
API_PORT=8007 # 管理API端口 API_PORT=8007 # Management API port
MQTT_SIGNATURE_KEY=test # MQTT签名密钥 MQTT_SIGNATURE_KEY=test # MQTT signature key
``` ```
请注意`PUBLIC_IP`配置,确保其与实际公网IP一致,如果有域名就填域名。 Please pay attention to the `PUBLIC_IP` configuration and make sure it is consistent with the actual public IP. If there is a domain name, fill in the domain name.
`MQTT_SIGNATURE_KEY` 是用于MQTT连接认证的密钥,最好设置成复杂一点的,最好是设置成8个字符以上且同时包含大小写字母,这个密钥稍后还会用到。 `MQTT_SIGNATURE_KEY` is the key used for MQTT connection authentication. It is best to set it to a complex one, preferably at least 8 characters and containing both uppercase and lowercase letters. This key will be used later.
- 注意不要用简单的密码,比如`123456`、`test`等。 - Be careful not to use simple passwords such as `123456`, `test`, etc.
- 注意不要用简单的密码,比如`123456`、`test`等。 - Be careful not to use simple passwords such as `123456`, `test`, etc.
- 注意不要用简单的密码,比如`123456`、`test`等。 - Be careful not to use simple passwords such as `123456`, `test`, etc.
6. 启动MQTT网关 6. Start the MQTT gateway
``` ```
# 启动服务 # Start the service
pm2 start ecosystem.config.js pm2 start ecosystem.config.js
# 查看日志 # View logs
pm2 logs xz-mqtt pm2 logs xz-mqtt
``` ```
当你看到如下日志,说明MQTT网关启动成功: When you see the following log, it means the MQTT gateway has been started successfully:
``` ```
0|xz-mqtt | 2025-09-11T12:14:48: MQTT 服务器正在监听端口 1883 0|xz-mqtt | 2025-09-11T12:14:48: MQTT server listening on port 1883
0|xz-mqtt | 2025-09-11T12:14:48: UDP 服务器正在监听 x.x.x.x:8884 0|xz-mqtt | 2025-09-11T12:14:48: UDP server listening on xxxx:8884
``` ```
如果需要重启MQTT网关,执行如下命令: If you need to restart the MQTT gateway, execute the following command:
``` ```
pm2 restart xz-mqtt pm2 restart xz-mqtt
``` ```
## 第二部分:全模块运行实现小智硬件MQTT+UDP连接 ## Part 2: Full module operation to realize Xiaozhi hardware MQTT+UDP connection
查看你智控台首页底部的版本号,确认你的智控台版本是否是`0.7.7`及以上版本。如果不是,需要升级智控台。 Check the version number at the bottom of your Smart Console homepage to confirm if your version is 0.7.7 or above. If not, you need to upgrade your Smart Console.
1. 在智控台顶部,点击`参数管理`,搜索`server.mqtt_gateway`,点击编辑,填入你在`.env`文件中设置的`PUBLIC_IP`+`:`+`MQTT_PORT`。类似这样 1. At the top of the smart console, click `Parameter Management`, search for `server.mqtt_gateway`, click Edit, and fill in the `PUBLIC_IP` + `:` + `MQTT_PORT` you set in the `.env` file. Similar to this
``` ```
192.168.0.7:1883 192.168.0.7:1883
``` ```
2. 在智控台顶部,点击`参数管理`,搜索`server.mqtt_signature_key`,点击编辑,填入你在`.env`文件中设置的`MQTT_SIGNATURE_KEY`。 2. At the top of the Smart Console, click Parameter Management, search for server.mqtt_signature_key, click Edit, and enter the MQTT_SIGNATURE_KEY you set in the .env file.
3. 在智控台顶部,点击`参数管理`,搜索`server.udp_gateway`,点击编辑,填入你在`.env`文件中设置的`PUBLIC_IP`+`:`+`UDP_PORT`。类似这样 3. At the top of the smart console, click `Parameter Management`, search for `server.udp_gateway`, click Edit, and fill in the `PUBLIC_IP`+`:`+`UDP_PORT` you set in the `.env` file. Similar to this
``` ```
192.168.0.7:8884 192.168.0.7:8884
``` ```
4. 在智控台顶部,点击`参数管理`,搜索`server.mqtt_manager_api`,点击编辑,填入你在`.env`文件中设置的`PUBLIC_IP`+`:`+`UDP_PORT`。类似这样 4. At the top of the smart console, click `Parameter Management`, search for `server.mqtt_manager_api`, click Edit, and fill in the `PUBLIC_IP`+`:`+`UDP_PORT` you set in the `.env` file. Similar to this
``` ```
192.168.0.7:8007 192.168.0.7:8007
``` ```
上面的配置完成后,你可以使用curl命令,验证你的ota地址是否会下发mqtt配置,把下面的`http://localhost:8002/xiaozhi/ota/`改成你的ota地址 After the above configuration is completed, you can use the curl command to verify whether your OTA address will send the MQTT configuration, and change the following `http://localhost:8002/xiaozhi/ota/` to your OTA address
``` ```
curl 'http://localhost:8002/xiaozhi/ota/' \ curl 'http://localhost:8002/xiaozhi/ota/' \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-H 'Client-Id: 7b94d69a-9808-4c59-9c9b-704333b38aff' \ -H 'Client-Id: 7b94d69a-9808-4c59-9c9b-704333b38aff' \
-H 'Device-Id: 11:22:33:44:55:66' \ -H 'Device-Id: 11:22:33:44:55:66' \
--data-raw $'{\n "application": {\n "version": "1.0.1",\n "elf_sha256": "1"\n },\n "board": {\n "mac": "11:22:33:44:55:66"\n }\n}' --data-raw $'{\n "application": {\n "version": "1.0.1",\n "elf_sha256": "1"\n },\n "board": {\n "mac": "11:22:33:44:55:66"\n }\n}'
``` ```
如果返回的内容包含`mqtt`相关的配置,说明配置成功。类似这样 If the returned content contains `mqtt` related configuration, it means the configuration is successful.
``` ```
{"server_time":{"timestamp":1757567894012,"timeZone":"Asia/Shanghai","timezone_offset":480},"activation":{"code":"460609","message":"http://xiaozhi.server.com\n460609","challenge":"11:22:33:44:55:66"},"firmware":{"version":"1.0.1","url":"http://xiaozhi.server.com:8002/xiaozhi/otaMag/download/NOT_ACTIVATED_FIRMWARE_THIS_IS_A_INVALID_URL"},"websocket":{"url":"ws://192.168.4.23:8000/xiaozhi/v1/"},"mqtt":{"endpoint":"192.168.0.7:1883","client_id":"GID_default@@@11_22_33_44_55_66@@@7b94d69a-9808-4c59-9c9b-704333b38aff","username":"eyJpcCI6IjA6MDowOjA6MDowOjA6MSJ9","password":"Y8XP9xcUhVIN9OmbCHT9ETBiYNE3l3Z07Wk46wV9PE8=","publish_topic":"device-server","subscribe_topic":"devices/p2p/11_22_33_44_55_66"}} {"server_time":{"timestamp":1757567894012,"timeZone":"Asia/Shanghai","timezone_offset":480},"activation":{"code":"460609","message":"http://xiaozhi.server.com\n460609","challen ge":"11:22:33:44:55:66"},"firmware":{"version":"1.0.1","url":"http://xiaozhi.server.com:8002/xiaozhi/otaMag/download/NOT_ACTIVATED_FIRMWARE_THIS_IS_A_INVALID_URL"},"websocket":{ "url":"ws://192.168.4.23:8000/xiaozhi/v1/"},"mqtt":{"endpoint":"192.168.0.7:1883","clien t_id":"GID_default@@@11_22_33_44_55_66@@@7b94d69a-9808-4c59-9c9b-704333b38aff","username" :"eyJpcCI6IjA6MDowOjA6MDowOjA6MSJ9","password":"Y8XP9xcUhVIN9OmbCHT9ETBiYNE3l3Z07Wk46wV9 PE8=","publish_topic":"device-server","subscribe_topic":"devices/p2p/11_22_33_44_55_66"}}
``` ```
由于MQTT信息是需要靠OTA地址下发的,因此只有你保证能正常连接服务器的OTA地址,重启唤醒即可。 Since MQTT information needs to be sent via the OTA address, as long as you can ensure that you can connect to the server's OTA address normally, you can restart and wake it up.
唤醒后留意mqtt-gateway的日志,确认是否有连接成功的日志。 After waking up, pay attention to the mqtt-gateway log to confirm whether there is a successful connection log.
``` ```
pm2 logs xz-mqtt pm2 logs xz-mqtt
``` ```
## 第三部分:全模块运行实现小智硬件MQTT+UDP连接 ## Part 3: Full module operation to realize Xiaozhi hardware MQTT+UDP connection
打开你的`data/.config.yaml`文件,在`server`下找到`mqtt_gateway`填入你在`.env`文件中设置的`PUBLIC_IP`+`:`+`MQTT_PORT`。类似这样 Open your `data/.config.yaml` file, find `mqtt_gateway` under `server` and fill in `PUBLIC_IP`+`:`+`MQTT_PORT` that you set in the `.env` file. Similar to this
``` ```
192.168.0.7:1883 192.168.0.7:1883
``` ```
在`server`下找到`mqtt_signature_key`填入你在`.env`文件中设置的`MQTT_SIGNATURE_KEY`。 Find `mqtt_signature_key` under `server` and fill in the `MQTT_SIGNATURE_KEY` you set in the `.env` file.
在`server`下找到`udp_gateway`填入你在`.env`文件中设置的`PUBLIC_IP`+`:`+`UDP_PORT`。类似这样 Find `udp_gateway` under `server` and fill in `PUBLIC_IP`+`:`+`UDP_PORT` that you set in the `.env` file. Similar to this
``` ```
192.168.0.7:8884 192.168.0.7:8884
``` ```
上面的配置完成后,你可以使用curl命令,验证你的ota地址是否会下发mqtt配置,把下面的`http://localhost:8002/xiaozhi/ota/`改成你的ota地址 After the above configuration is completed, you can use the curl command to verify whether your OTA address will send the MQTT configuration, and change the following `http://localhost:8002/xiaozhi/ota/` to your OTA address
``` ```
curl 'http://localhost:8002/xiaozhi/ota/' \ curl 'http://localhost:8002/xiaozhi/ota/' \
-H 'Device-Id: 11:22:33:44:55:66' \ -H 'Device-Id: 11:22:33:44:55:66' \
--data-raw $'{\n "application": {\n "version": "1.0.1",\n "elf_sha256": "1"\n },\n "board": {\n "mac": "11:22:33:44:55:66"\n }\n}' --data-raw $'{\n "application": {\n "version": "1.0.1",\n "elf_sha256": "1"\n },\n "board": {\n "mac": "11:22:33:44:55:66"\n }\n}'
``` ```
如果返回的内容包含`mqtt`相关的配置,说明配置成功。类似这样 If the returned content contains `mqtt` related configuration, it means the configuration is successful.
``` ```
{"server_time":{"timestamp":1758781561083,"timeZone":"GMT+08:00","timezone_offset":480},"activation":{"code":"527111","message":"http://xiaozhi.server.com\n527111","challenge":"11:22:33:44:55:66"},"firmware":{"version":"1.0.1","url":"http://xiaozhi.server.com:8002/xiaozhi/otaMag/download/NOT_ACTIVATED_FIRMWARE_THIS_IS_A_INVALID_URL"},"websocket":{"url":"ws://192.168.1.15:8000/xiaozhi/v1/"},"mqtt":{"endpoint":"192.168.1.15:1883","client_id":"GID_default@@@11_22_33_44_55_66@@@11_22_33_44_55_66","username":"eyJpcCI6IjE5Mi4xNjguMS4xNSJ9","password":"fjAYs49zTJecWqJ3jBt+kqxVn/x7vkXRAc85ak/va7Y=","publish_topic":"device-server","subscribe_topic":"devices/p2p/11_22_33_44_55_66"}} {"server_time":{"timestamp":1758781561083,"timeZone":"GMT+08:00","timezone_offset":480},"activation":{"code":"527111","message":"http://xiaozhi.server.com\n527111","chall enge":"11:22:33:44:55:66"},"firmware":{"version":"1.0.1","url":"http://xiaozhi.server.com:8002/xiaozhi/otaMag/download/NOT_ACTIVATED_FIRMWARE_THIS_IS_A_INVALID_URL"},"web socket":{"url":"ws://192.168.1.15:8000/xiaozhi/v1/"},"mqtt":{"endpoint":"192.168.1.15 :1883","client_id":"GID_default@@@11_22_33_44_55_66@@@11_22_33_44_55_66","username":" eyJpcCI6IjE5Mi4xNjguMS4xNSJ9","password":"fjAYs49zTJecWqJ3jBt+kqxVn/x7vkXRAc85ak/va7Y =","publish_topic":"device-server","subscribe_topic":"devices/p2p/11_22_33_44_55_66"}}
``` ```
由于MQTT信息是需要靠OTA地址下发的,因此只有你保证能正常连接服务器的OTA地址,重启唤醒即可。 Since MQTT information needs to be sent via the OTA address, as long as you can ensure that you can connect to the server's OTA address normally, you can restart and wake it up.
唤醒后留意mqtt-gateway的日志,确认是否有连接成功的日志。 After waking up, pay attention to the mqtt-gateway log to confirm whether there is a successful connection log.
``` ```
pm2 logs xz-mqtt pm2 logs xz-mqtt
``` ```