简体   繁体   English

从Azure IoT中心接收警报/命令到设备

[英]Receiving alerts/commands from Azure IoT Hub to device

I am currently doing R & D on Azure IoT hubs using Windows10 IoT core and Raspberry PI 2. I am following the sample for reference here to send alerts to the device from IoT hub when the temperature of a room greater than 25 for example. 我目前正在使用Windows10 IoT核心和Raspberry PI 2在Azure IoT中心上进行研发。我正在跟踪此处的示例,以在房间温度高于25时从IoT中心向设备发送警报。 But the sample is for mbed board. 但是该示例适用于mbed板。

To do this I developed a sample UWP app for Raspberry Pi which will send temperature data to IoT hub. 为此,我为Raspberry Pi开发了一个示例UWP应用程序,它将温度数据发送到IoT中心。 In Azure I created a stream analytics job which will take IoT hub as input and filter the data( only temperature greater than 25 degrees) then send it to output EventHub. 在Azure中,我创建了一个流分析作业,它将以IoT中心作为输入并过滤数据(仅温度大于25度),然后将其发送到输出EventHub。 Here I created a worker role/cloud service which will read data from EventHub and send back to IoT hub to the same one which I am using for sending temperature information from raspberry pi. 在这里,我创建了一个工作者角色/云服务,该服务将从EventHub读取数据,然后将其发送回IoT中心到我用来从树莓派发送温度信息的同一服务中。

Here my doubt is how can IoT Hub differentiate the data sent from raspberry pi and the data sent by worker role? 我的疑问是,IoT中心如何区分树莓派发送的数据和工作者角色发送的数据? and how can I receive only the data sent by worker role? 怎样才能只接收工作者角色发送的数据?

Because if I read cloud to device messages I am receiving the data which I sent from raspberry pi. 因为如果我将云读到设备消息,则会收到从树莓派发送的数据。

Here I got stuck, I tried the below code to read data from IoT Hub but getting all my messages sent from raspberry pi instead worker role messages that is only temperature greater than 25 messages. 在这里,我陷入了困境,我尝试使用以下代码从IoT中心读取数据,但是却使我的所有消息都从树莓派pi发送,而工人角色消息的温度仅高于25条消息。

public async void ReceiveDataFromCloud()
    {


        startingDateTimeUtc = DateTime.UtcNow;
        ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString);
        builder.TransportType = ppatierno.AzureSBLite.Messaging.TransportType.Amqp;

        factory = MessagingFactory.CreateFromConnectionString(ConnectionString);

        client = factory.CreateEventHubClient(eventHubEntity);
        group = client.GetDefaultConsumerGroup();
        receiver = group.CreateReceiver(partitionId.ToString(), startingDateTimeUtc);//startingDateTimeUtc
        for (int i = 0; i <= 0; i++)
        {
            while (true)
            {
                EventData data = receiver.Receive();

                if (data != null)
                {
                    var receiveddata = Encoding.UTF8.GetString(data.GetBytes());

                    //var messageString = JsonConvert.DeserializeObject<ConferenceRooms>(receiveddata);                    

                    Debug.WriteLine("{0} {1} {2}", data.SequenceNumber, data.EnqueuedTimeUtc.ToLocalTime(), Encoding.UTF8.GetString(data.GetBytes()));

                }
                else
                {
                    break;
                }

                await Task.Delay(2000);

            }

        }

        receiver.Close();
        client.Close();
        factory.Close();

    }

How to send alerts from IoT Hub back to devices only the messages filtered by stream analytic job? 如何将警报从IoT中心发送回设备,使其仅将由流分析作业筛选的消息发送回设备?

Update: 更新:

When I use the above mentioned code for receiving I am getting all the messages from IoT Hub which are sent by raspberry pi. 当我使用上述代码进行接收时,我会从IoT中心获取树莓派发送的所有消息。

But when I use the below code for receiving message, I am getting only the messages sent by worker role to IoT Hub. 但是,当我使用下面的代码接收消息时,我仅收到由工作人员角色发送到IoT中心的消息。

while (true)
        {
            Message receivedMessage = await deviceClient.ReceiveAsync();
            if (receivedMessage == null) continue;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
            Console.ResetColor();

            await deviceClient.CompleteAsync(receivedMessage);
        }

This is what my requirement is and I am able to achieve it. 这就是我的要求,我能够实现。

IoT Hub provides bi-directional asymmetrical way of communication between device and the cloud. IoT中心提供设备与云之间的双向非对称通信方式。 The process of receiving messages from the cloud on your IoT device is described quite well in this article . 本文很好地描述了从IoT设备上的云中接收消息的过程。

In short, try to use the following code to receive cloud-to-device messages from IoT Hub: 简而言之,尝试使用以下代码从IoT中心接收云到设备的消息:

while (true)
{
    Message receivedMessage = await deviceClient.ReceiveAsync();
    if (receivedMessage == null) continue;

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
    Console.ResetColor();

    await deviceClient.CompleteAsync(receivedMessage);
}

Here deviceClient is the instance of Microsoft.Azure.Devices.Client.DeviceClient which you create like this: 在这里, deviceClient是您创建的Microsoft.Azure.Devices.Client.DeviceClient的实例,如下所示:

deviceClient = DeviceClient.Create(iotHubUri, 
  newDeviceAuthenticationWithRegistrySymmetricKey("myFirstDevice", deviceKey));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM