0

I have following code which sends data to iot device successfully. Now i want to set telemetry timespan to send data continuously how to configure that any guidance ?

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

basically here i have used while (true) instead of this i want to set

1.how often to send telemetry from each device input format is HH MM SS

2.How long simulation will run inout format is HH MM SS UI screen

Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    Are you just asking for how to schedule this with a recurring iteration? ie. [`System.Threading.Timer`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.7.2) ? – Igor Oct 16 '18 at 17:57
  • this is my web application mvc and i have UI also to setup the parameters like this so yes how to set up the schedule. UI screen https://learn.microsoft.com/en-us/azure/iot-accelerators/media/quickstart-device-simulation-deploy/solutiondashboard-expanded.png#lightbox – Neo Oct 16 '18 at 18:02
  • I don't think you can schedule a task *in* an asp.net app (that includes MVC). See also [Best way to run scheduled tasks](https://stackoverflow.com/q/542804/1260204). You would generally do this in a windows service or similar depending on your hosting solution. – Igor Oct 16 '18 at 18:05
  • oh i have hosted this web app into azurewebsites i think i can create a webjob but how exactly the above code i can configure i dint understand it. thanks in advance – Neo Oct 16 '18 at 18:11
  • Probably you are interesting about the IoT Hub quotas and throttling, so the following document described it: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-quotas-throttling – Roman Kiss Oct 16 '18 at 22:15
  • also, I do recommend to read an Azure Device Twin concept & features: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins – Roman Kiss Oct 16 '18 at 22:18
  • thanks a lot for doc link but any other blog you recommend for implementation ? – Neo Oct 17 '18 at 04:56
  • have a look at this tutorial: https://learn.microsoft.com/en-us/azure/iot-hub/tutorial-device-twins – Roman Kiss Oct 17 '18 at 20:14

0 Answers0