使用Javascript中的MQTT连接到Azure IotHub

时间:2018-08-06 16:18:37

标签: node.js azure mqtt azure-iot-hub sasl

当尝试使用Connection refused: Not authorizedhttps://github.com/mqttjs/MQTT.js)连接到Azure IotHub时,我一直回到MQTT.js

SAS密码是使用Microsoft设备资源管理器(https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer)生成的,因此提供正确的读/写连接详细信息以及将来的到期日期,我认为它是正确和有效的。

我还要求通过rejectUnauthorized: false(根据https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#tlsssl-configuration的MQTT.js文档中的说明)启用TLS / SSL(根据Microsoft Azure文档:https://github.com/mqttjs/MQTT.js#client)。

如何使用第三方MQTT库和SAS令牌通过Javascript连接?

这是Javascript代码段:

var MQTT = require("async-mqtt");

const deviceId = "<PUT_SOMETHING_HERE>";
const iotHubName = "<PUT_SOMETHING_HERE>";

const url = `${iotHubName}.azure-devices.net/${deviceId}/api-version=2016-11-14`;
const iotHubTopic = `devices/${deviceId}/messages/events/`

var client = MQTT.connect(`mqtts://${url}:8883`, {
  username: url,
  password: `SharedAccessSignature sr=${iotHubName}.azure-devices.net&sig=<COMBINATION_OF_PASSWORDS_URL_ENCODED>&se=<EPOCH_EXPIRY>&skn=<ACCESS_POLICY_NAME>`,
  rejectUnauthorized: false, // https://github.com/mqttjs/MQTT.js#client
});

// this is code from the MQTT.js example, but I don't even reach it
async function doStuff() {

    console.log("Starting");
    try {
        await client.publish(iotHubTopic, "It works!");
        // This line doesn't run until the server responds to the publish
        await client.end();
        // This line doesn't run until the client has disconnected without error
        console.log("Done");
    } catch (e){
        // Do something about it!
        console.log("Error while sending a message...");
        console.log(e.stack);
        process.exit();
    }
}

const ceremony = () => {
  return new Promise((resolve, reject) => {
      client.on("connect", doStuff);
      return resolve();
    })
    .then((stuff) => {
      console.log("Done?", stuff);
    })
    .catch((err) => {
      console.log("Err...", err);
      process.exit();
    });
}

ceremony();

输出是这样的:

Done? undefined
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: Connection refused: Not authorized
    at MqttClient._handleConnack (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/lib/client.js:896:15)
    at MqttClient._handlePacket (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/lib/client.js:332:12)
    at work (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/lib/client.js:274:12)
    at Writable.writable._write (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/lib/client.js:284:5)
    at doWrite (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (<PATH_TO_THE_JS_PROJECT>/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at Writable.write (<PATH_TO_THE_JS_PROJECT>/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)

2 个答案:

答案 0 :(得分:3)

是的,Azure IoT中心支持MQTT,您可以使用MQTT协议直接与IoT中心连接,并使用记录的主题名称和主题过滤器将主题发布/订阅为发送/接收消息。我已经修改了上面的代码,并且工作正常。

var MQTT = require("async-mqtt");

const deviceId = "{the device name}";
const iotHubName = "{your iot hub name}";
const userName = `${iotHubName}.azure-devices.net/${deviceId}/api-version=2016-11-14`;
const iotHubTopic = `devices/${deviceId}/messages/events/`;

var client = MQTT.connect(`mqtts://${iotHubName}.azure-devices.net:8883`, {
  keepalive: 10,
  clientId: deviceId,
  protocolId: 'MQTT',
  clean: false,
  protocolVersion: 4,
  reconnectPeriod: 1000,
  connectTimeout: 30 * 1000,
  username: userName,
  password: "{SAS Token}",
  rejectUnauthorized: false, 
});

// this is code from the MQTT.js example, but I don't even reach it
async function doStuff() {

    console.log("Starting");
    try {
        await client.publish(iotHubTopic, "It works!");
        // This line doesn't run until the server responds to the publish
        await client.end();
        // This line doesn't run until the client has disconnected without error
        console.log("Done");
    } catch (e){
        // Do something about it!
        console.log("Error while sending a message...");
        console.log(e.stack);
        process.exit();
    }
}

const ceremony = () => {
  return new Promise((resolve, reject) => {
      client.on("connect", doStuff);
      return resolve();
    })
    .then((stuff) => {
      console.log("Done?", stuff);
    })
    .catch((err) => {
      console.log("Err...", err);
      process.exit();
    });
}

ceremony();

在代码中,您需要注意:

  1. 使用IoT中心中的 DEVICE ID 作为连接选项中的clientId;
  2. 使用 mqtts 协议;
  3. 对于用户名选项,使用{iothubhostname} / {device_id} / api-version = 2016-11-14,其中{iothubhostname}是IoT中心的完整CName。
  4. 对于密码字段,使用SAS令牌。您可以使用Device Explorer获取SAS令牌。 SAS令牌必须使用涉及以下权限的策略生成:“注册表写入”,“服务连接”和“设备连接”(少于这三个的组合可能起作用,但提供“注册表”确定”)是不够的。

答案 1 :(得分:0)

我面临着同样的问题。经过半个小时的尝试,我才知道我的一位同事在Iothub上禁用了我的设备。?