远程控制/监控 - Azure IoT中心

问题描述 投票:1回答:2

我正在做一个小项目,我正在使用Raspberry PI来监控温度并使用Azure IOT Hub控制LED。温度通过仪表板门户可视化,您也可以在其中控制LED。我已经彻底阅读了文档,但我仍然不确定几件事情:

远程监控:

Raspberry PI目前向我的IoT Hub(Device2Cloud)发送温度,该部分的一切看起来都很好。为了显示从Raspberry PI发送的值,我从NodeJS后端读取事件总线,方式与此示例相同:https://github.com/Azure-Samples/web-apps-node-iot-hub-data-visualization/blob/master/IoThub/iot-hub.js

这是将设备读取到云消息的正确方法吗?

远程控制

这是我非常不确定的部分,我想通过我的仪表板页面中的Cloud2Device通信来控制连接到Raspberry PI的LED。我不太确定如何在我的Node JS后端实现它,我真的找不到任何好的例子。任何意见,将不胜感激。

raspberry-pi cloud device azure-iot-hub
2个回答
0
投票

关于远程监控问题:是的,这将工作,虽然我想指出事件中心SDK for Node仍然是预览(并可能在将来改变一点),所以你应该期待一些怪癖。

关于“远程控制”:为了发送云到设备消息,您应该使用Azure IoT Hub Service SDK for Node,以下是如何将云发送到设备消息的示例(从here复制)

'use strict';

var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;

var connectionString = '[IoT Hub Connection String]';
var targetDevice = '[Target device that will receive the message]';

var client = Client.fromConnectionString(connectionString);

client.open(function (err) {
  if (err) {
    console.error('Could not connect: ' + err.message);
  } else {
    console.log('Client connected');

    // Create a message and send it to the IoT Hub every second
    var data = JSON.stringify({ text : 'foo' });
    var message = new Message(data);
    console.log('Sending message: ' + message.getData());
    client.send(targetDevice, message, function (err) {
      if (err) {
        console.error('Could not send: ' + err.message);
      } else {
        console.log('Message sent');
      }
    });
  }
});

0
投票

您有几个远程控制设备的选项。您应该查看本文(https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-c2d-guidance)以确定哪种选项最适合您的方案。

您可以在此处找到云到设备消息教程:https://docs.microsoft.com/azure/iot-hub/iot-hub-node-node-c2d

你可以在这里找到一个直接的方法教程:https://docs.microsoft.com/azure/iot-hub/iot-hub-node-node-direct-methods

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.