2016-05-06 78 views
0

我正在使用Windows10 IoT核心和Raspberry PI 2在Azure IoT集线器上执行R & D.我正在按照示例以供参考here以从IoT集线器向设备发送警报例如,当房间的温度大于25℃时。但样本是针对mbed板的。接收Azure IoT Hub到设备的警报/命令

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

这里我的疑问是,物联网Hub如何区分从树莓派发送的数据和工作人员角色发送的数据?以及如何只接收工作角色发送的数据?

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

这里我被卡住了,我尝试了下面的代码从IoT Hub读取数据,但是从树莓派中获取所有消息,而不是温度超过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其通过覆盆子PI发送的消息上述代码。

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

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

回答

0

物联网集线器提供设备和云之间的双向不对称通信方式。在您的物联网设备上从云接收消息的过程描述得非常好in this article

总之,尝试使用下面的代码从物联网中心收到的云到设备的消息:

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

这里deviceClient是你创建了这里面的Microsoft.Azure.Devices.Client.DeviceClient实例:

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

是的,我同意你的意见。你提到的文章很好。我也可以使用我的代码接收来自云端的消息,但在这里我收到了覆盆子pi发送的所有消息。我不想要所有消息,我只想要温度值大于25的消息。为此,我使用流分析,事件中心和工作人员角色将过滤后的消息发送回IoT中心。 – narendramacha

+0

@narendramacha,我认为你的代码的问题可能是你把IoT Hub当作EventHub(这是单向的),因此你得到的是和你一样的消息。使用物联网集线器不应该发生这种情况,这就是为什么我建议您尝试一些更多物联网特定的代码,如文章中的代码。 –

+0

你说的是对的,经过一番练习,我观察到IoT Hub只发送由工作者角色发送的消息(在我的方案中)。当我使用DeviceClient接收方法时,它实际上只在工作角色向IoT Hub发送消息时才接收,而不是由Raspberry Pi发送的消息@Dmitri Soshnikov – narendramacha

相关问题