我目前正在尝试将gcloud IoT项目连接到MQTT桥,以便能够发布遥测数据。我正在使用Mosquitto,并试图在我在gcloud计算引擎上创建的VM实例上运行mosquitto_pub
命令。我还创建了一个防火墙规则,以优先级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文件,所以idk发生了什么-帮助!
答案 0 :(得分:1)
我怀疑这是您的JWT到期日过长(> = 24h)。
Google Cloud IoT的MQTT网关(文档:the maximum lifetime of a token)
要求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