mqtt - 将 gcloud 连接到 MQTT 网桥时出现问题
问题描述
我目前正在尝试将 gcloud IoT 项目连接到 MQTT 网桥,以便能够发布遥测数据。我正在使用 Mosquitto 并尝试在mosquitto_pub
我在 gcloud 计算引擎上创建的 VM 实例上运行该命令。我还创建了一个防火墙规则来打开优先级为 0 的端口 8883。我提交了以下命令(我删除了使用的 JWT 并将此处放在代码块中)
mosquitto_pub \
--host mqtt.googleapis.com \
--port 8883 \
--id projects/telemetry-268916/locations/us-central1/registries/iotcore-registry-telemetry/devices/esp32 \
--username unused \
--pw "<my-JWT.>" \
--cafile ./roots.pem \
--tls-version tlsv1.2 \
--protocol-version mqttv311 \
--debug \
--qos 1 \
--topic /devices/esp32/events \
--message "Hello MQTT"
当我运行此命令时 ^ 我收到以下错误:
Client projects/telemetry-268916/locations/us-central1/registries/iotcore-registry telemetry/devices/esp32 sending CONNECT
Client projects/telemetry-268916/locations/us-central1/registries/iotcore-registry-telemetry/devices/esp32 received CONNACK
Connection Refused: not authorised.
Error: The connection was refused.
我到处找,找不到解决这个问题的方法。我正在使用根证书并指定 CA 文件,所以我知道发生了什么——帮助!
解决方案
我怀疑您的 JWT 到期时间太长了(>=24 小时)。
Google Cloud IoT 的 MQTT 网关(记录:令牌的最长生命周期)
要求 JWT <=24h
PROJECT=
REGISTRY=
REGION=
DEVICE=
CLIENT="projects/${PROJECT}/locations/${REGION}/registries/${REGISTRY}/devices/${DEVICE}"
TOPIC="/devices/${DEVICE}/events"
# Using a JWT generator; expiry=24h
PASSWORD=$(\
go-jwt \
--project=${PROJECT} \
--private_key=${KEY} \
--expiry=24h)
docker run \
--interactive --tty \
--volume=${PWD}/roots.pem:/roots.pem \
eclipse-mosquitto:1.6.8 mosquitto_pub \
-h mqtt.googleapis.com -p 8883 \
-i ${CLIENT} \
-u unused -P ${PASSWORD} \
-t ${TOPIC} \
-m "Hello Freddie!" \
--cafile /roots.pem \
--debug \
--qos 1 \
--tls-version tlsv1.2 \
--protocol-version mqttv311
sending CONNECT
received CONNACK (0)
sending PUBLISH (d0, q1, r0, m1, '/devices/.../events', ... (14 bytes))
received PUBACK (Mid: 1, RC:0)
sending DISCONNECT
# Using a JWT generator; expiry=25h
PASSWORD=$(\
go-jwt \
--project=${PROJECT} \
--private_key=${KEY} \
--expiry=25h)
docker run \
--interactive --tty \
--volume=${PWD}/roots.pem:/roots.pem \
eclipse-mosquitto:1.6.8 mosquitto_pub \
-h mqtt.googleapis.com -p 8883 \
-i ${CLIENT} \
-u unused -P ${PASSWORD} \
-t ${TOPIC}
-m "Hello Freddie!" \
--cafile /roots.pem \
--debug \
--qos 1 \
--tls-version tlsv1.2 \
--protocol-version mqttv311
CONNECT
CONNACK (4)
Connection error: Connection Refused: bad user name or password.
DISCONNECT
推荐阅读
- orm - GlassMapper 获取默认上下文 V5
- php - 如何访问本地用户的 Microsoft Graph API 数据
- javascript - 如何等待外部文件功能完成然后继续主程序?
- java - 将 sLine 字符串解析为整数时出错
- regex - 用于捕获函数的 tmLanguage 正则表达式,它可以包含许多函数
- java - 如何使用 java 中的 Apache poi 读取从 Internet 下载的受保护视图中的 .xlsx 文件?
- google-kubernetes-engine - 验证数据时出错:使用 Helm 的 ValidationError(PersistentVolume.spec)
- python - django rest 框架使用 ArrayAgg 和 GROUP BY 进行注释
- google-sheets - Google 承诺可以将 Fusion 表格导入 Google 表格,但如何导入?
- javascript - 如何接受用户输入作为函数的参数?