This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from Azure-Samples/drwill/DeviceSimulator
Update simulated device project
- Loading branch information
Showing
4 changed files
with
112 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
// This application uses the Azure IoT Hub device SDK for .NET | ||
// For samples see: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/iothub/device/samples | ||
|
||
using Microsoft.Azure.Devices.Client; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SimulatedDevice | ||
{ | ||
/// <summary> | ||
/// This sample illustrates the very basics of a device app sending telemetry. For a more comprehensive device app sample, please see | ||
/// <see href="https://github.com/Azure-Samples/azure-iot-samples-csharp/tree/master/iot-hub/Samples/device/DeviceReconnectionSample"/>. | ||
/// </summary> | ||
internal class Program | ||
{ | ||
private static DeviceClient s_deviceClient; | ||
|
||
// The device connection string to authenticate the device with your IoT hub. | ||
// Using the Azure CLI: | ||
// az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyDotnetDevice --output table | ||
private static string s_connectionString = "{Your device connection string here}"; | ||
|
||
private static async Task Main(string[] args) | ||
{ | ||
Console.WriteLine("IoT Hub Quickstarts #1 - Simulated device."); | ||
|
||
// This sample accepts the device connection string as a parameter, if present | ||
CheckForConnectionStringArgument(args); | ||
|
||
// Connect to the IoT hub using the MQTT protocol | ||
s_deviceClient = DeviceClient.CreateFromConnectionString(s_connectionString, TransportType.Mqtt); | ||
|
||
// Set up a condition to quit the sample | ||
Console.WriteLine("Press control-C to exit."); | ||
using var cts = new CancellationTokenSource(); | ||
Console.CancelKeyPress += (sender, eventArgs) => | ||
{ | ||
eventArgs.Cancel = true; | ||
cts.Cancel(); | ||
Console.WriteLine("Device simulator exit requested..."); | ||
}; | ||
|
||
// Run the telemetry loop | ||
await SendDeviceToCloudMessagesAsync(cts.Token); | ||
|
||
Console.WriteLine("Device simulator finished."); | ||
} | ||
|
||
private static void CheckForConnectionStringArgument(string[] args) | ||
{ | ||
if (args.Any()) | ||
{ | ||
try | ||
{ | ||
var cs = IotHubConnectionStringBuilder.Create(args.First()); | ||
s_connectionString = cs.ToString(); | ||
} | ||
catch (Exception) { } | ||
} | ||
} | ||
|
||
// Async method to send simulated telemetry | ||
private static async Task SendDeviceToCloudMessagesAsync(CancellationToken ct) | ||
{ | ||
// Initial telemetry values | ||
double minTemperature = 20; | ||
double minHumidity = 60; | ||
var rand = new Random(); | ||
|
||
while (!ct.IsCancellationRequested) | ||
{ | ||
double currentTemperature = minTemperature + rand.NextDouble() * 15; | ||
double currentHumidity = minHumidity + rand.NextDouble() * 20; | ||
|
||
// Create JSON message | ||
string messageBody = JsonSerializer.Serialize( | ||
new | ||
{ | ||
temperature = currentTemperature, | ||
humidity = currentHumidity, | ||
}); | ||
using var message = new Message(Encoding.ASCII.GetBytes(messageBody)) | ||
{ | ||
ContentType = "application/json", | ||
ContentEncoding = "utf-8", | ||
}; | ||
|
||
// 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 s_deviceClient.SendEventAsync(message); | ||
Console.WriteLine($"{DateTime.Now} > Sending message: {messageBody}"); | ||
|
||
await Task.Delay(1000); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters