首页 文章

尝试连接到Azure IoT Hub时,NodeRed崩溃

提问于
浏览
0

我试图部署非常简单的node-red流来使用MQTT向Azure发送消息 . 不幸的是,每次都会崩溃节点红色 .

我在Raspberry Pi 3上运行NodeRed:

16 May 11:29:19 - [info] Node-RED version: v0.18.4
16 May 11:29:19 - [info] Node.js  version: v8.11.1
16 May 11:29:19 - [info] Linux 4.14.34-v7+ arm LE

一直在尝试这两个节点库:

https://github.com/Azure/azure-iot-sdk-node/tree/master/device/node-red https://github.com/rasavant-ms/node-red-contrib-azure/tree/master/iot-hub

节点红色日志:

Connection string :HostName=learning-hub.azure-devices.net;DeviceId=undefined;SharedAccessKey=undefined
16 May 11:16:52 - [error] [azureiothub:Azure IoT Hub] Could not connect: Connection refused: Not authorized
16 May 11:16:52 - [info] [azureiothub:Azure IoT Hub] Connected to Azure IoT Hub.
16 May 11:16:52 - [red] Uncaught Exception:
16 May 11:16:52 - TypeError: Cannot read property 'on' of undefined
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Unit entered failed state.
nodered.service: Failed with result 'exit-code'.
nodered.service: Service hold-off time over, scheduling restart.
Stopped Node-RED graphical event wiring tool..

这是我的简单流程:

NodeRed Flow

以及节点中的信息:

Azure IoT中心

Azure IoT Hub information

DTU智能库Azure

{
  "deviceID": "SensorTag-D2",
  "SAK": "<Primary-Key>",
  "Protocol": "mqtt",
  "Data": {
    "Light": "25"
  }
}

1 回答

  • 0

    从日志和Azure IoT Hub节点信息中,您似乎没有设置Azure IoT Hub的连接字符串 . 您可以通过复制导入流,并在弹出窗口中粘贴以下内容:

    [{"id":"7b108cb1.4807c8","type":"azureiothub","z":"8d630437.6181a8","name":"Azure IoT Hub","protocol":"amqp","x":379.5,"y":89.22000122070312,"wires":[["b79e7772.b39cc8"]]},{"id":"2269b09.2499b5","type":"inject","z":"8d630437.6181a8","name":"Inject data","topic":"","payload":"Hello from device","payloadType":"str","repeat":"","crontab":"","once":false,"x":209.5,"y":196.01998901367187,"wires":[["7b108cb1.4807c8"]]},{"id":"b79e7772.b39cc8","type":"function","z":"8d630437.6181a8","name":"Convert Bytes to String","func":"msg.payload = msg.payload.toString();\nreturn msg;","outputs":1,"noerr":0,"x":614.5,"y":201.57000732421875,"wires":[["a142535d.158f4"]]},{"id":"a142535d.158f4","type":"debug","z":"8d630437.6181a8","name":"","active":true,"console":"false","complete":"false","x":762.5,"y":320.1499938964844,"wires":[]}]
    

    然后双击Azure IoT Hub节点,它将显示连接字符串设置 .
    enter image description here

    请注意,当您想要发送数据时,您应该将注入的Payload设置为字符串格式而不是json .

相关问题