0

我目前正在使用 Windows10 IoT 核心和 Raspberry PI 2 在 Azure IoT 集线器上进行研发。例如,当房间的温度高于 25 度时,我正在按照此处参考的示例从 IoT 集线器向设备发送警报。但该示例适用于 mbed 板。

为此,我为 Raspberry Pi 开发了一个示例 UWP 应用程序,它将温度数据发送到 IoT 中心。在 Azure 中,我创建了一个流分析作业,它将 IoT 中心作为输入并过滤数据(仅温度大于 25 度),然后将其发送到输出 EventHub。在这里,我创建了一个工作角色/云服务,它将从 EventHub 读取数据并将其发送回 IoT 中心,并发送回我用于从树莓派发送温度信息的同一中心。

这里我的疑问是 IoT Hub 如何区分从树莓派发送的数据和从工作角色发送的数据?我怎样才能只接收工人角色发送的数据?

因为如果我读取云到设备消息,我将收到从树莓派发送的数据。

在这里我卡住了,我尝试使用下面的代码从 IoT 中心读取数据,但我的所有消息都是从树莓派发送的,而不是仅温度大于 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();

    }

如何仅将通过流分析作业过滤的消息从 IoT 中心发送回设备?

更新:

当我使用上述代码进行接收时,我会从 IoT 中心获取所有由树莓派发送的消息。

但是,当我使用下面的代码接收消息时,我只会收到工作角色发送到 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);
        }

这就是我的要求,我能够实现它。

4

1 回答 1

0

IoT Hub 提供了设备和云之间的双向非对称通信方式。本文很好地描述了在 IoT 设备上从云接收消息的过程。

简而言之,尝试使用以下代码从 IoT Hub 接收云到设备的消息:

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);
}

deviceClientMicrosoft.Azure.Devices.Client.DeviceClient您这样创建的实例:

deviceClient = DeviceClient.Create(iotHubUri, 
  newDeviceAuthenticationWithRegistrySymmetricKey("myFirstDevice", deviceKey));
于 2016-05-06T15:37:20.497 回答