我按照下面的教程将树莓派3连接到Google IOT Core。我在Google控制台上将Google Core IOT部件设置为OK,并且对树莓派部件执行了所有步骤,但是,始终会根据以下错误消息拒绝连接。
error { Error: Connection refused: Bad username or password
at MqttClient._handleConnack (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/lib/client.js:920:15)
at MqttClient._handlePacket (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/lib/client.js:350:12)
at work (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/lib/client.js:292:12)
at Writable.writable._write (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/lib/client.js:302:5)
at doWrite (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/node_modules/readable-
stream/lib/_stream_writable.js:428:64)
at writeOrBuffer (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/node_modules/readable-
stream/lib/_stream_writable.js:417:5)
at Writable.write (/home/pi/Desktop/Google-IoT-
Device/node_modules/mqtt/node_modules/readable-
stream/lib/_stream_writable.js:334:11)
at TLSSocket.ondata (_stream_readable.js:639:20)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7) code: 4 }
close
教程链接: https://hub.packtpub.com/build-google-cloud-iot-application/#comment-53421
这是我的index.js文件的顶部:
var fs = require('fs');
var jwt = require('jsonwebtoken');
var mqtt = require('mqtt');
var rpiDhtSensor = require('rpi-dht-sensor');
var dht = new rpiDhtSensor.DHT11(2); // `2` => GPIO2
var projectId = 'nifty-*******-******';
var cloudRegion = 'us-central1';
var registryId = 'device-registry';
var deviceId = 'raspberrypi';
var mqttHost = 'mqtt.googleapis.com';
var mqttPort = 8883;
var privateKeyFile = '../certs/rsa_private.pem';
var algorithm = 'RS256';
var messageType = 'state'; // or event
var mqttClientId = 'projects/' + projectId + '/locations/' + cloudRegion +
'/registries/' + registryId + '/devices/' + deviceId;
var mqttTopic = '/devices/' + deviceId + '/' + messageType;
var connectionArgs = {
host: mqttHost,
port: mqttPort,
clientId: mqttClientId,
username: 'unused',
password: createJwt(projectId, privateKeyFile, algorithm),
protocol: 'mqtts',
secureProtocol: 'TLSv1_2_method'
};
本教程未说明有关下载Google根CA证书的任何内容,因此我遵循了本教程: https://raspberrypi.stackexchange.com/questions/76419/entrusted-certificates-installation
我还通过在Google上执行以下操作来检查连接路由是否正常,并且一切都检查正常: https://cloud.google.com/iot/docs/troubleshooting
projectID,registryID,deviceID和区域都检查正确。
我确信它必须非常简单,但这已经让我沮丧了一个星期。我已经拖网了,但是我尝试过的所有操作都会导致相同的错误。 有没有人可以帮助您?
答案 0 :(得分:2)
要三重检查的内容:
wget https://pki.google.com/roots.pem
,即可从Google获取当前的roots.pem。不要再向您扔其他教程了,但我实际上只是发布了blog post with really detailed info on this with step-by-step,主要是因为其他教程有漏洞或过时的信息。
另一个注意事项:我看到您正在使用state
MQTT主题进行发送,没错,但是在注释中您列出了event
。是events
。因此,如果您尝试发送到event
,也会失败。
答案 1 :(得分:0)
我已经解决了同样的问题,减少了为密码创建json令牌的有效期限
答案 2 :(得分:0)
对我来说,这是一个简单的疏忽,即等号(=)和区域名称之间的空格。代码是:
(失败)之前....
node cloudiot_mqtt_example_nodejs.js mqttDeviceDemo \
--projectId=myproject \
--cloudRegion= us-central1 \
--registryId=1234 \
--deviceId=test-device \
--privateKeyFile=./cert/rsa_private.pem \
--numMessages=25 \
--algorithm=RS256
之后(错误消息 “错误错误:连接被拒绝:用户名或密码错误) ....
--cloudRegion=us-central1 \
另请参阅此处的示例: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/iot/mqtt_example