首页 > 解决方案 > 如何使用 c# 设置遥测频率以向物联网设备发送消息

问题描述

我有以下代码成功地将数据发送到物联网设备。现在我想设置遥测时间跨度以连续发送数据如何配置任何指导?

  private static async void SendDeviceToCloudMessagesAsync(string deviceid , string deviceKey)
            {
                deviceClient = DeviceClient.Create("hostname", new DeviceAuthenticationWithRegistrySymmetricKey(deviceid, deviceKey), Microsoft.Azure.Devices.Client.TransportType.Mqtt);

                double minTemperature = 20;
                double minHumidity = 60;
                int messageId = 1;
                Random rand = new Random();
                while (true)
                {
                    double currentTemperature = minTemperature + rand.NextDouble() * 15;
                    double currentHumidity = minHumidity + rand.NextDouble() * 20;
                    var telemetryDataPoint = new
                    {
                        messageId = messageId++,
                        deviceId = deviceid,
                        temperature = currentTemperature,
                        humidity = currentHumidity
                    };
                    var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                    var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));
                    message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");
                    await deviceClient.SendEventAsync(message);
                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
                    await Task.Delay(1000);
                }
            }

基本上我在这里使用while (true)了我想设置的而不是这个

1.从每个设备输入格式发送遥测数据的频率是 HH MM SS

2.多长时间模拟运行 inout 格式是 HH MM SS UI 屏幕

标签: c#.netazureazure-iot-hub

解决方案


推荐阅读