我按照下面的教程将我的树莓派 3 连接到 Google IOT Core。我在 Google 控制台上设置了 Google Core IOT 部分 OK,并且对 raspberry pi 部分执行了所有步骤,但是,根据下面的错误消息,连接总是被拒绝。
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 和 region 都检查正确。
我确信它必须非常简单,但这已经让我沮丧了一个星期。我已经在互联网上搜索过,但是我尝试过的方法都会导致同样的错误。有没有人可以帮忙?