首页 > 解决方案 > 将 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 文件,所以我知道发生了什么——帮助!

标签: mqttiotgcloudgoogle-cloud-pubsubmosquitto

解决方案


我怀疑您的 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

推荐阅读