// INSERT - SendDeviceToCloudMessagesAsync below here // Async method to send simulated telemetry private static async Task SendDeviceToCloudMessagesAsync() { var sensor = new EnvironmentSensor(); while (true) { var currentTemperature = sensor.ReadTemperature(); var currentHumidity = sensor.ReadHumidity(); var currentPressure = sensor.ReadPressure(); var currentLocation = sensor.ReadLocation(); var messageString = CreateMessageString(currentTemperature, currentHumidity, currentPressure, currentLocation); var message = new Message(Encoding.ASCII.GetBytes(messageString)); // Add a custom application property to the message. // An IoT hub can filter on these properties without access to the message body. message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); // Send the telemetry message await deviceClient.SendEventAsync(message); Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); // Delay before next Telemetry reading await Task.Delay(1000); } }
// INSERT - SendDeviceToCloudMessagesAsync below here // Async method to send simulated telemetry private static async Task SendDeviceToCloudMessagesAsync() { // Create an instance of our sensor var sensor = new EnvironmentSensor(); while (true) { // read data from the sensor var currentTemperature = sensor.ReadTemperature(); var currentHumidity = sensor.ReadHumidity(); var messageString = CreateMessageString(currentTemperature, currentHumidity); // create a byte array from the message string using ASCII encoding var message = new Message(Encoding.ASCII.GetBytes(messageString)); // Add a custom application property to the message. // An IoT hub can filter on these properties without access to the message body. message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); // Send the telemetry message await deviceClient.SendEventAsync(message); Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); // Increased delay for custom alert await Task.Delay(10000); } }
// INSERT SendDeviceToCloudMessagesAsync method below here public static async Task SendDeviceToCloudMessagesAsync() { var sensor = new EnvironmentSensor(); while (true) { var temperature = sensor.GetTemperature(); var humidity = sensor.GetHumidity(); var message = new Message(Encoding.UTF8.GetBytes(CreateMessageString(temperature, humidity))); Console.WriteLine("Sending telemetry."); await deviceClient.SendEventAsync(message); await Task.Delay(1000); } }