我目前正在使用Windows10 IoT核心和Raspberry PI 2在Azure IoT集线器上进行研发 . 我正在关注该示例以供参考here,以便当房间温度大于25时从IoT集线器向设备发送警报 . 但样品是用于mbed板 .
为此,我为Raspberry Pi开发了一个示例UWP应用程序,它将温度数据发送到物联网中心 . 在Azure中,我创建了一个流分析作业,它将IoT中心作为输入并过滤数据(仅温度大于25度),然后将其发送到输出EventHub . 在这里,我创建了一个工作者角色/ Cloud 服务,它将从EventHub读取数据并将其发送回IoT中心到我用于从raspberry pi发送温度信息的同一个中心 .
我怀疑的是IoT Hub如何区分raspberry pi发送的数据和工作者角色发送的数据?以及如何只收到工作人员角色发送的数据?
因为如果我读取 Cloud 设备消息,我将收到我从raspberry pi发送的数据 .
在这里,我遇到了困难,我尝试了以下代码从IoT Hub读取数据,但是从raspberry 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();
}
如何将来自IoT Hub的警报仅发送回设备,仅通过流分析作业过滤消息?
Update:
当我使用上面提到的代码接收时,我收到来自IoT Hub的所有消息,这些消息由raspberry 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);
}
这就是我的要求,我能够实现它 .
1 回答
物联网中心提供设备与 Cloud 之间的双向非对称通信方式 . 在物联网设备上从 Cloud 端接收消息的过程描述得非常好in this article .
简而言之,尝试使用以下代码从IoT Hub接收 Cloud 到设备消息:
这里
deviceClient
是Microsoft.Azure.Devices.Client.DeviceClient
的实例,您可以像这样创建: