上篇博文中我们将模拟设备注册到 Azure IoT Hub 中:我们得到了设备的唯一标识。
Azure IoT 技术研究系列 2 - 设备注册到 Azure IoT Hub
本文中我们继续深入研究,设备到云、云到设备通信。
1. 在 Azure IoT Hub 中接收模拟设备的消息
读取设备到云消息的 Event Hub 兼容终结点,使用 AMQP 协议。
我们新建一个 Console 控制台工程:IoTServer,添加 Nuget 引用:WindowsAzure.ServiceBus
核心的命名空间:using Microsoft.ServiceBus.Messaging;
核心类:EventHubClient
通过 EventHubClient 创建一个 EventHubReceiver,不间断的接收设备侧的消息。
- 1 static stringconnectionString ="HostName=IoTTest.*******;SharedAccessKeyName=iothubowner;SharedAccessKey=***";
- 2 static stringiotHubD2cEndpoint ="messages/events";
- 3 staticEventHubClient eventHubClient;
ReceiveMessagesFromDeviceAsync 方法:
- 1 /// <summary>
- 2 /// 接收设备侧的消息
- 3 /// </summary>
- 4 /// <param name="partition">分区</param>
- 5 /// <param name="ct">取消标识</param>
- 6 /// <returns>Task</returns>
- 7 private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct) 8 {
- 9
- var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);
- 10
- while (true) 11 {
- 12
- if (ct.IsCancellationRequested) break;
- 13 EventData eventData = await eventHubReceiver.ReceiveAsync();
- 14
- if (eventData == null) continue;
- 15 16 string data = Encoding.UTF8.GetString(eventData.GetBytes());
- 17 Console.WriteLine("Message received. Partition: {0} Data: '{1}'", partition, data);
- 18 19 //防止CPU被占满
- 20 Task.Delay(1).Wait();
- 21
- }
- 22
- }
Main 函数中我们将整个 IoTServer Run 起来:
- 1 static voidMain(string[] args)
- 2 {
- 3Console.WriteLine("Azure IoT Hub 接收消息..., Press Ctrl-C to exit.\n");
- 4eventHubClient =EventHubClient.CreateFromConnectionString(connectionString,iotHubD2cEndpoint); 5
- 6 vard2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
- 7
- 8CancellationTokenSource cts =new CancellationTokenSource();
- 9
- 10System.Console.CancelKeyPress += (s, e) =>11 {
- 12e.Cancel =true;
- 13 cts.Cancel();
- 14Console.WriteLine("Exiting...");
- 15 };
- 16
- 17 vartasks =newList();
- 18 foreach(stringpartitionin d2cPartitions)
- 19 {
- 20 tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
- 21 }
- 22
- 23 Task.WaitAll(tasks.ToArray());
- 24}
2. 模拟设备发送消息到 Azure IoT Hub
我们同样新建一个 Console 控制台工程:Device,用于模拟向 Azure IoT Hub 发送消息。
首先添加 Nuget 引用:Microsoft.Azure.Devices.Client,这个 Nuget 依赖的 Nuget 很多,不要着急,慢慢 Install 吧
核心的命名空间:
using Microsoft.Azure.Devices.Client; using Newtonsoft.Json;
核心类:
Microsoft.Azure.Devices.Client.DeviceClient
模拟设备往 Azure IoT Hub 发消息时,用到了设备的 Key(唯一标识)和 IoT Hub HostName, 上篇博文中提到的主机名:Azure IoT 技术研究系列 2 - 设备注册到 Azure IoT Hub
- 1 static DeviceClient deviceClient;
- 2 static string iotHubUri = "IoTTest.******"; //iot hub hostname
- 3 static string deviceKey = "+jDqO+Nu2g************="; //device key
添加一个循环向 Azure IoT Hub 发送消息的方法:SendDeviceToCloudMessagesAsync,1s 一条消息
- 1 /// <summary>
- 2 /// 循环向Azure IoT Hub发送消息
- 3 /// </summary>
- 4 private static async void SendDeviceToCloudMessagesAsync()
- 5 {
- 6 doubleavgWindSpeed =10;// m/s
- 7Random rand =new Random();
- 8
- 9 while(true)
- 10 {
- 11 //发送遥测数据
- 12 doublecurrentWindSpeed = avgWindSpeed + rand.NextDouble() *4-2;
- 13 vartelemetryDataPoint =new
- 14 {
- 15deviceId ="TeldPile001",
- 16windSpeed = currentWindSpeed
- 17 };
- 18 varmessageString = JsonConvert.SerializeObject(telemetryDataPoint);
- 19 varmessage =new Message(Encoding.ASCII.GetBytes(messageString));
- 20
- 21 await deviceClient.SendEventAsync(message);
- 22Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
- 23
- 24 //1s 一条
- 25 awaitTask.Delay(1000);
- 26 }
- 27}
然后,在 Main 函数中启动模拟设备发送消息:
- 1 static voidMain(string[] args)
- 2 {
- 3Console.WriteLine("模拟设备通信...\n");
- 4deviceClient = DeviceClient.Create(iotHubUri,newDeviceAuthenticationWithRegistrySymmetricKey("TeldPile001", deviceKey), TransportType.Mqtt);
- 5
- 6 SendDeviceToCloudMessagesAsync();
- 7 Console.ReadLine();
- 8}
3. 启动运行测试
在解决方案上设置双启动项目:Device 和 IoTServer
F5 Run:
可以发现,设备侧消息发送、Azure IoT Hub 接收是同步的
我们查看 Azure Portal 中的统计:
总结: 通过这两篇博文,我们研究验证了 Azure IoT Hub 注册设备、设备和云之间的通信,感觉整个 Azure 的 IoT Hub 还是非常好用、易用,比较容易理解和操作,基于 PaaS 层的 IoT Hub,可以做很多有价值的设计和方案。
周国庆
2017/4/18
来源: http://www.cnblogs.com/tianqing/p/6726081.html