首页 > 解决方案 > NodeRed 在尝试连接到 Azure IoT Hub 时崩溃

问题描述

我尝试部署非常简单的节点红色流以使用 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 集线器

节点红色日志:

   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..

这是我的简单流程:

节点红流

以及节点中的信息:

Azure 物联网中心

Azure IoT 中心信息

DTU 智能库 Azure

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

标签: azureraspberry-pi3node-redazure-iot-hub

解决方案


从日志和 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 节点,它将显示连接字符串设置。 在此处输入图像描述

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


推荐阅读